@x402-lens/express 0.0.0-stage → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +96 -3
- package/dist/index.cjs +641 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +172 -0
- package/dist/index.d.ts +172 -0
- package/dist/index.js +636 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 x402-lens contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,96 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# `@x402-lens/express`
|
|
2
|
+
|
|
3
|
+
Express integration for [x402-lens](../../README.md): x402 v2 payments with
|
|
4
|
+
observability that explains itself.
|
|
5
|
+
|
|
6
|
+
This is a thin layer over `@x402/express`. The official middleware owns the
|
|
7
|
+
protocol; this package attaches events, a readable 402 body, and findings.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import express from "express";
|
|
11
|
+
import { paymentLens } from "@x402-lens/express";
|
|
12
|
+
|
|
13
|
+
const app = express();
|
|
14
|
+
const lens = paymentLens({ routes: { "GET /weather": "$0.001" } });
|
|
15
|
+
lens.on(event => console.log(event.type, event.route, event.network));
|
|
16
|
+
|
|
17
|
+
app.use(lens.middleware);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Configuration comes from `X402_FACILITATOR_URL`, `X402_NETWORK`, and
|
|
21
|
+
`X402_PAY_TO` unless passed explicitly. None of them has a default. Use
|
|
22
|
+
`paymentLensAsync` to also read `x402.config.json`.
|
|
23
|
+
|
|
24
|
+
## Options
|
|
25
|
+
|
|
26
|
+
| Option | Default | Meaning |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| `routes` | required | `{ "GET /weather": "$0.001" }` or `RouteInput[]` |
|
|
29
|
+
| `facilitatorUrl`, `network`, `payTo` | env | Required, no defaults |
|
|
30
|
+
| `networks` | `[network]` | Advertise one `accepts[]` entry per network |
|
|
31
|
+
| `payTo` as a map | — | One address per network (`{ "eip155:84532": "0x…" }`) |
|
|
32
|
+
| `sinks` | console | `{ console, jsonlPath, onEvent }` |
|
|
33
|
+
| `extraSinks` | — | Additional `LensSink` implementations |
|
|
34
|
+
| `readableBody` | `true` | Mirror the challenge into the 402 JSON body |
|
|
35
|
+
| `cors` | `false` | Expose payment headers for browser buyers |
|
|
36
|
+
| `grant` | — | `(request) => boolean \| string` to allow or deny unpaid |
|
|
37
|
+
| `facilitatorClient` | — | Replace the SDK HTTP client entirely |
|
|
38
|
+
| `routesConfig` | — | Override the `RoutesConfig` handed to `@x402/express` |
|
|
39
|
+
|
|
40
|
+
## Handles
|
|
41
|
+
|
|
42
|
+
`paymentLens()` returns `middleware`, the underlying `server` and `httpServer`
|
|
43
|
+
(for custom hooks or schemes on networks this package does not register),
|
|
44
|
+
`config`, `ready`, `on(listener)`, and `close()`.
|
|
45
|
+
|
|
46
|
+
Await `lens.ready` before accepting traffic so a dead facilitator fails the
|
|
47
|
+
deploy instead of the first buyer's request:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const lens = paymentLens({ /* … */ });
|
|
51
|
+
await lens.ready;
|
|
52
|
+
app.use(lens.middleware);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## CORS
|
|
56
|
+
|
|
57
|
+
Off by default: an app with its own CORS layer should own it. When a browser
|
|
58
|
+
buys from a different origin, none of the three payment headers is
|
|
59
|
+
CORS-safelisted, so payments fail silently. `x402-lens inspect` detects this.
|
|
60
|
+
|
|
61
|
+
Use `cors: { origin: "https://app.example.com" }` (or `"*"`) so
|
|
62
|
+
`Access-Control-Allow-Origin` is set together with the payment expose/allow
|
|
63
|
+
headers. `cors: true` only sets the payment expose/allow headers — it does
|
|
64
|
+
**not** set `Access-Control-Allow-Origin`; browser buyers still fail unless
|
|
65
|
+
your own CORS middleware supplies it.
|
|
66
|
+
|
|
67
|
+
## Several networks on one route
|
|
68
|
+
|
|
69
|
+
`networks: ["eip155:84532", "solana:…"]` advertises one `accepts[]` entry per
|
|
70
|
+
chain, so a buyer pays on whichever chain holds funds. Each entry carries its
|
|
71
|
+
own `payTo`: an address exists on one chain only, so mixing an EVM chain with
|
|
72
|
+
Solana needs a map, and the package refuses to advertise an EVM address on
|
|
73
|
+
Solana.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
paymentLens({
|
|
77
|
+
networks: ["eip155:84532", "solana:EtWT…"],
|
|
78
|
+
payTo: { "eip155:84532": "0x2096…", "solana:EtWT…": "9WzD…" },
|
|
79
|
+
routes: { "GET /weather": "$0.001" },
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
A `"$"` price resolves each chain's own token and decimals, which means the
|
|
84
|
+
atomic amounts differ by design. An explicit `{ asset, amount }` price is
|
|
85
|
+
therefore rejected when more than one network is configured.
|
|
86
|
+
|
|
87
|
+
The SDK validates every configured network against the facilitator at startup
|
|
88
|
+
and exits the process if one is missing. Run `x402-lens doctor` in CI.
|
|
89
|
+
|
|
90
|
+
## Free access
|
|
91
|
+
|
|
92
|
+
`grant` runs before verification on each protected request. Return `true` for
|
|
93
|
+
access without payment, or a string to deny with a 403. The buyer always sees
|
|
94
|
+
`{ error: "access_denied" }`; the string is only on the `access.denied` event.
|
|
95
|
+
`request.config` is the matched route (price, scheme, metadata) when known.
|
|
96
|
+
Both decisions emit events, so a bypass is visible rather than silent.
|