@relai-fi/x402 0.5.8 → 0.5.9
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/README.md +104 -0
- package/dist/client.cjs +535 -9
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +64 -3
- package/dist/client.d.ts +64 -3
- package/dist/client.js +535 -9
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +812 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +810 -34
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +540 -10
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +7 -2
- package/dist/react/index.d.ts +7 -2
- package/dist/react/index.js +540 -10
- package/dist/react/index.js.map +1 -1
- package/dist/server.cjs +273 -25
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +17 -2
- package/dist/server.d.ts +17 -2
- package/dist/server.js +273 -25
- package/dist/server.js.map +1 -1
- package/dist/{types-C-2GNyMh.d.cts → types-CWtUxi3l.d.cts} +12 -1
- package/dist/{types-C-2GNyMh.d.ts → types-CWtUxi3l.d.ts} +12 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,6 +62,15 @@ const client = createX402Client({
|
|
|
62
62
|
solana: solanaWallet, // @solana/wallet-adapter compatible
|
|
63
63
|
evm: evmWallet, // wagmi/viem compatible
|
|
64
64
|
},
|
|
65
|
+
integritas: {
|
|
66
|
+
enabled: true,
|
|
67
|
+
flow: 'single', // or 'dual'
|
|
68
|
+
},
|
|
69
|
+
relayWs: {
|
|
70
|
+
enabled: true,
|
|
71
|
+
// optional: explicit WS endpoint
|
|
72
|
+
// wsUrl: 'wss://api.relai.fi/api/ws/relay',
|
|
73
|
+
},
|
|
65
74
|
});
|
|
66
75
|
|
|
67
76
|
// 402 responses are handled automatically
|
|
@@ -69,6 +78,71 @@ const response = await client.fetch('https://api.example.com/protected');
|
|
|
69
78
|
const data = await response.json();
|
|
70
79
|
```
|
|
71
80
|
|
|
81
|
+
### Integritas (client)
|
|
82
|
+
|
|
83
|
+
`createX402Client` can set Integritas headers automatically for every request.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const client = createX402Client({
|
|
87
|
+
wallets: { evm: evmWallet },
|
|
88
|
+
integritas: {
|
|
89
|
+
enabled: true,
|
|
90
|
+
flow: 'single',
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Sends:
|
|
95
|
+
// X-Integritas: true
|
|
96
|
+
// X-Integritas-Flow: single
|
|
97
|
+
await client.fetch('https://api.relai.fi/relay/<apiId>/v1/chat/completions');
|
|
98
|
+
|
|
99
|
+
// Per-request override
|
|
100
|
+
await client.fetch('https://api.relai.fi/relay/<apiId>/v1/chat/completions', {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
x402: {
|
|
103
|
+
integritas: { enabled: true, flow: 'dual' },
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### WebSocket relay transport (optional)
|
|
109
|
+
|
|
110
|
+
If your protected API is behind a relay URL like `https://api.relai.fi/relay/:apiId/...`
|
|
111
|
+
or a whitelabel relay URL like `https://<whitelabel>.x402.fi/...`,
|
|
112
|
+
the SDK can use the Relay WebSocket transport automatically.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const client = createX402Client({
|
|
116
|
+
wallets: {
|
|
117
|
+
evm: evmWallet,
|
|
118
|
+
},
|
|
119
|
+
relayWs: {
|
|
120
|
+
enabled: true,
|
|
121
|
+
preflightTimeoutMs: 5000,
|
|
122
|
+
paymentTimeoutMs: 10000,
|
|
123
|
+
fallbackToHttp: true,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Pass your standard relay HTTP URL (apiId-based or whitelabel-based).
|
|
128
|
+
// SDK handles WS preflight + paid retry internally.
|
|
129
|
+
await client.fetch('https://api.relai.fi/relay/1769629274857/v1/chat/completions', {
|
|
130
|
+
method: 'POST',
|
|
131
|
+
headers: { 'Content-Type': 'application/json' },
|
|
132
|
+
body: JSON.stringify({ messages: [{ role: 'user', content: 'Hello' }] }),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Whitelabel relay URL is also supported.
|
|
136
|
+
await client.fetch('https://tgmetrics.x402.fi/projects?page=1', {
|
|
137
|
+
method: 'GET',
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
For Node.js runtimes without global `WebSocket`, provide `relayWs.webSocketFactory`.
|
|
142
|
+
|
|
143
|
+
If the relay returns multiple `accepts` options for one request, the SDK automatically
|
|
144
|
+
falls back to standard HTTP x402 flow for that call.
|
|
145
|
+
|
|
72
146
|
### React Hook
|
|
73
147
|
|
|
74
148
|
Works with [`@solana/wallet-adapter-react`](https://github.com/anza-xyz/wallet-adapter) and [`wagmi`](https://wagmi.sh/):
|
|
@@ -177,6 +251,8 @@ Creates a fetch wrapper that automatically handles 402 Payment Required response
|
|
|
177
251
|
| Option | Type | Default | Description |
|
|
178
252
|
|--------|------|---------|-------------|
|
|
179
253
|
| `wallets` | `{ solana?, evm? }` | `{}` | Wallet adapters for each chain |
|
|
254
|
+
| `relayWs` | `X402RelayWsConfig` | `undefined` | Optional WS transport for relay URLs |
|
|
255
|
+
| `integritas` | `boolean \| X402IntegritasConfig` | `undefined` | Automatically set Integritas headers |
|
|
180
256
|
| `facilitatorUrl` | `string` | RelAI facilitator | Custom facilitator endpoint |
|
|
181
257
|
| `preferredNetwork` | `RelaiNetwork` | — | Prefer this network when multiple `accepts` |
|
|
182
258
|
| `solanaRpcUrl` | `string` | `https://api.mainnet-beta.solana.com` | Solana RPC (use Helius/Quicknode for production) |
|
|
@@ -184,6 +260,24 @@ Creates a fetch wrapper that automatically handles 402 Payment Required response
|
|
|
184
260
|
| `maxAmountAtomic` | `string` | — | Safety cap on payment amount |
|
|
185
261
|
| `verbose` | `boolean` | `false` | Log payment flow to console |
|
|
186
262
|
|
|
263
|
+
**`integritas` options:**
|
|
264
|
+
|
|
265
|
+
| Option | Type | Default | Description |
|
|
266
|
+
|--------|------|---------|-------------|
|
|
267
|
+
| `enabled` | `boolean` | `true` when object is provided | Adds `X-Integritas: true` |
|
|
268
|
+
| `flow` | `'single' \| 'dual'` | — | Adds `X-Integritas-Flow` |
|
|
269
|
+
|
|
270
|
+
**`relayWs` options:**
|
|
271
|
+
|
|
272
|
+
| Option | Type | Default | Description |
|
|
273
|
+
|--------|------|---------|-------------|
|
|
274
|
+
| `enabled` | `boolean` | `false` | Enable WS transport for relay URLs |
|
|
275
|
+
| `wsUrl` | `string` | derived from relay host | Explicit WebSocket relay endpoint |
|
|
276
|
+
| `preflightTimeoutMs` | `number` | `5000` | Timeout for WS preflight request |
|
|
277
|
+
| `paymentTimeoutMs` | `number` | `10000` | Timeout for paid WS retry |
|
|
278
|
+
| `fallbackToHttp` | `boolean` | `true` | Fall back to standard HTTP flow if WS fails |
|
|
279
|
+
| `webSocketFactory` | `(url) => WebSocketLike` | runtime WebSocket | Custom WS factory for Node.js/server runtimes |
|
|
280
|
+
|
|
187
281
|
**Wallet interfaces:**
|
|
188
282
|
|
|
189
283
|
```typescript
|
|
@@ -245,6 +339,10 @@ app.get('/api/data', relai.protect({
|
|
|
245
339
|
payTo: '0xYourWallet',
|
|
246
340
|
price: 0.01, // $0.01 USDC
|
|
247
341
|
description: 'Premium data access',
|
|
342
|
+
integritas: {
|
|
343
|
+
enabled: true,
|
|
344
|
+
flow: 'single',
|
|
345
|
+
},
|
|
248
346
|
}), (req, res) => {
|
|
249
347
|
// req.payment = { verified, transactionId, payer, network, amount }
|
|
250
348
|
res.json({ data: 'Protected content', payment: req.payment });
|
|
@@ -270,6 +368,12 @@ app.get('/api/solana-data', relai.protect({
|
|
|
270
368
|
3. Server calls RelAI facilitator `/settle` → gas sponsored by RelAI
|
|
271
369
|
4. Settlement success → `PAYMENT-RESPONSE` header set, `req.payment` populated, `next()` called
|
|
272
370
|
|
|
371
|
+
**Integritas on server protect:**
|
|
372
|
+
|
|
373
|
+
- `integritas: true` enables Integritas metadata for the endpoint.
|
|
374
|
+
- `integritas: { enabled: true, flow: 'single' }` sets default flow.
|
|
375
|
+
- Buyer headers (`X-Integritas`, `X-Integritas-Flow`) override defaults per request.
|
|
376
|
+
|
|
273
377
|
**`req.payment` fields:**
|
|
274
378
|
| Field | Type | Description |
|
|
275
379
|
|-------|------|-------------|
|