@relai-fi/x402 0.5.39 → 0.6.0-rc.0
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 +380 -21
- package/dist/index.cjs +75 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +74 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins.cjs +21467 -45
- package/dist/plugins.cjs.map +1 -1
- package/dist/plugins.d.cts +2 -1
- package/dist/plugins.d.ts +2 -1
- package/dist/plugins.js +21477 -33
- package/dist/plugins.js.map +1 -1
- package/dist/relay-feedback.cjs +86 -0
- package/dist/relay-feedback.cjs.map +1 -0
- package/dist/relay-feedback.d.cts +63 -0
- package/dist/relay-feedback.d.ts +63 -0
- package/dist/relay-feedback.js +61 -0
- package/dist/relay-feedback.js.map +1 -0
- package/dist/server-Dr3JOA0-.d.ts +713 -0
- package/dist/server-t9nKvoKl.d.cts +713 -0
- package/dist/server.cjs +15 -0
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +2 -1
- package/dist/server.d.ts +2 -1
- package/dist/server.js +15 -0
- package/dist/server.js.map +1 -1
- package/package.json +10 -1
- package/dist/server-CaSmhDnd.d.ts +0 -312
- package/dist/server-CyfEHW9D.d.cts +0 -312
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import { a as RelaiNetwork } from './types-Y9ni5XwY.cjs';
|
|
2
|
-
|
|
3
|
-
interface PluginContext {
|
|
4
|
-
/** Network for this endpoint */
|
|
5
|
-
network: RelaiNetwork;
|
|
6
|
-
/** Price in USD */
|
|
7
|
-
price: number;
|
|
8
|
-
/** Request path */
|
|
9
|
-
path: string;
|
|
10
|
-
/** HTTP method */
|
|
11
|
-
method: string;
|
|
12
|
-
}
|
|
13
|
-
interface PluginResult {
|
|
14
|
-
/** If true, skip payment and serve content for free */
|
|
15
|
-
skip?: boolean;
|
|
16
|
-
/** Extra response headers to set */
|
|
17
|
-
headers?: Record<string, string>;
|
|
18
|
-
/** Metadata attached to req.pluginMeta */
|
|
19
|
-
meta?: Record<string, unknown>;
|
|
20
|
-
}
|
|
21
|
-
interface RelaiPlugin {
|
|
22
|
-
/** Unique plugin name */
|
|
23
|
-
name: string;
|
|
24
|
-
/**
|
|
25
|
-
* Called before the 402 payment check.
|
|
26
|
-
* Return { skip: true } to bypass payment entirely.
|
|
27
|
-
*/
|
|
28
|
-
beforePaymentCheck?(req: any, ctx: PluginContext): Promise<PluginResult>;
|
|
29
|
-
/**
|
|
30
|
-
* Called after a successful payment settlement.
|
|
31
|
-
* Use for analytics, logging, webhooks, etc.
|
|
32
|
-
*/
|
|
33
|
-
afterSettled?(req: any, result: SettleResult, ctx: PluginContext): Promise<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Called once when the Relai instance initializes (server start).
|
|
36
|
-
* Use to sync config to RelAI backend or validate credentials.
|
|
37
|
-
*/
|
|
38
|
-
onInit?(): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Called before sending the 402 response. Allows plugins to add
|
|
41
|
-
* extensions or modify the response body (e.g. bridge info).
|
|
42
|
-
*/
|
|
43
|
-
enrich402Response?(response: any, ctx: PluginContext): any;
|
|
44
|
-
}
|
|
45
|
-
interface FreeTierPluginConfig {
|
|
46
|
-
/** Service key (sk_live_...) for syncing with RelAI backend. If omitted, runs in local in-memory mode. */
|
|
47
|
-
serviceKey?: string;
|
|
48
|
-
/** Max free calls per buyer per period */
|
|
49
|
-
perBuyerLimit: number;
|
|
50
|
-
/** Reset period for per-buyer counters */
|
|
51
|
-
resetPeriod?: 'none' | 'daily' | 'monthly';
|
|
52
|
-
/** Optional global cap across all buyers */
|
|
53
|
-
globalCap?: number;
|
|
54
|
-
/** Specific paths to apply free tier to (default: '*' = all) */
|
|
55
|
-
paths?: string[];
|
|
56
|
-
/** Override RelAI API base URL (default: https://api.relai.fi) */
|
|
57
|
-
baseUrl?: string;
|
|
58
|
-
/** Cache TTL in ms for check results (default: 5000) */
|
|
59
|
-
cacheTtlMs?: number;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Free Tier plugin - gives buyers a number of free API calls
|
|
63
|
-
* before requiring payment.
|
|
64
|
-
*
|
|
65
|
-
* State is stored in the RelAI backend, keyed by your service key.
|
|
66
|
-
* Config can be set here (SDK-side) or overridden in the relai.fi dashboard.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```typescript
|
|
70
|
-
* import Relai from '@relai-fi/x402/server';
|
|
71
|
-
* import { freeTier } from '@relai-fi/x402/plugins';
|
|
72
|
-
*
|
|
73
|
-
* const relai = new Relai({
|
|
74
|
-
* network: 'base',
|
|
75
|
-
* plugins: [
|
|
76
|
-
* freeTier({
|
|
77
|
-
* serviceKey: process.env.RELAI_SERVICE_KEY!,
|
|
78
|
-
* perBuyerLimit: 10,
|
|
79
|
-
* resetPeriod: 'daily',
|
|
80
|
-
* }),
|
|
81
|
-
* ],
|
|
82
|
-
* });
|
|
83
|
-
*
|
|
84
|
-
* app.get('/api/data', relai.protect({
|
|
85
|
-
* payTo: '0xYourWallet',
|
|
86
|
-
* price: 0.01,
|
|
87
|
-
* }), (req, res) => {
|
|
88
|
-
* res.json({ data: 'paid content' });
|
|
89
|
-
* });
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
interface BridgePluginConfig {
|
|
93
|
-
/** Service key (sk_live_...) for tracking bridge usage per API owner */
|
|
94
|
-
serviceKey?: string;
|
|
95
|
-
/** RelAI API base URL (default: https://api.relai.fi) */
|
|
96
|
-
baseUrl?: string;
|
|
97
|
-
/** Override settle endpoint (auto-discovered from /bridge/info if not set) */
|
|
98
|
-
settleEndpoint?: string;
|
|
99
|
-
/** Override supported source chains (auto-discovered if not set) */
|
|
100
|
-
supportedSourceChains?: string[];
|
|
101
|
-
/** Override supported source assets (auto-discovered if not set) */
|
|
102
|
-
supportedSourceAssets?: string[];
|
|
103
|
-
/** Override bridge payTo map: { [caip2]: address } */
|
|
104
|
-
payTo?: Record<string, string>;
|
|
105
|
-
/** Override Solana fee payer address (auto-discovered if not set) */
|
|
106
|
-
feePayerSvm?: string;
|
|
107
|
-
/** Override payment facilitator URL */
|
|
108
|
-
paymentFacilitator?: string;
|
|
109
|
-
/** Bridge fee in basis points (default: auto-discovered) */
|
|
110
|
-
feeBps?: number;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Bridge plugin - enables cross-chain payments via the RelAI bridge.
|
|
114
|
-
*
|
|
115
|
-
* When a buyer's wallet is on a different chain than the merchant accepts,
|
|
116
|
-
* the client SDK can automatically route the payment through the bridge.
|
|
117
|
-
* This plugin adds `extensions.bridge` to the 402 response with all the
|
|
118
|
-
* info the client needs to execute a cross-chain payment.
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* ```typescript
|
|
122
|
-
* import Relai from '@relai-fi/x402/server';
|
|
123
|
-
* import { bridge } from '@relai-fi/x402/plugins';
|
|
124
|
-
*
|
|
125
|
-
* const relai = new Relai({
|
|
126
|
-
* network: 'skale-base',
|
|
127
|
-
* plugins: [
|
|
128
|
-
* bridge(), // auto-discovers from https://api.relai.fi
|
|
129
|
-
* ],
|
|
130
|
-
* });
|
|
131
|
-
*
|
|
132
|
-
* // Buyer on Solana can now pay for a SKALE endpoint
|
|
133
|
-
* app.get('/api/data', relai.protect({
|
|
134
|
-
* payTo: '0xYourWallet',
|
|
135
|
-
* price: 0.05,
|
|
136
|
-
* }), (req, res) => {
|
|
137
|
-
* res.json({ data: 'paid content' });
|
|
138
|
-
* });
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
declare function bridge(config?: BridgePluginConfig): RelaiPlugin;
|
|
142
|
-
/**
|
|
143
|
-
* Extended plugin interface with data export for free tier.
|
|
144
|
-
*/
|
|
145
|
-
interface FreeTierPlugin extends RelaiPlugin {
|
|
146
|
-
/** Export all in-memory usage data (local mode only). Returns null when using cloud mode. */
|
|
147
|
-
getUsageData(): FreeTierUsageExport | null;
|
|
148
|
-
/** Whether plugin is running in local (in-memory) or cloud (RelAI backend) mode. */
|
|
149
|
-
readonly mode: 'local' | 'cloud';
|
|
150
|
-
}
|
|
151
|
-
interface FreeTierUsageExport {
|
|
152
|
-
mode: 'local';
|
|
153
|
-
config: {
|
|
154
|
-
perBuyerLimit: number;
|
|
155
|
-
resetPeriod: string;
|
|
156
|
-
globalCap: number | null;
|
|
157
|
-
paths: string[];
|
|
158
|
-
};
|
|
159
|
-
/** Per-buyer usage entries */
|
|
160
|
-
usage: Array<{
|
|
161
|
-
buyerId: string;
|
|
162
|
-
path: string;
|
|
163
|
-
count: number;
|
|
164
|
-
periodStart: string;
|
|
165
|
-
lastCall: string;
|
|
166
|
-
}>;
|
|
167
|
-
globalCount: number;
|
|
168
|
-
exportedAt: string;
|
|
169
|
-
}
|
|
170
|
-
declare function freeTier(config: FreeTierPluginConfig): FreeTierPlugin;
|
|
171
|
-
|
|
172
|
-
interface RelaiServerConfig {
|
|
173
|
-
/** Network to accept payments on */
|
|
174
|
-
network: RelaiNetwork;
|
|
175
|
-
/** RelAI facilitator URL (default: https://facilitator.x402.fi) */
|
|
176
|
-
facilitatorUrl?: string;
|
|
177
|
-
/** Plugins to extend protect() behavior (e.g. freeTier, rateLimit) */
|
|
178
|
-
plugins?: RelaiPlugin[];
|
|
179
|
-
}
|
|
180
|
-
type DynamicPrice = number | ((req: any) => number | Promise<number>);
|
|
181
|
-
type RelaiIntegritasFlow = 'single' | 'dual';
|
|
182
|
-
interface RelaiIntegritasOptions {
|
|
183
|
-
/** Enable Integritas by default for this endpoint */
|
|
184
|
-
enabled?: boolean;
|
|
185
|
-
/** Default flow when Integritas is enabled */
|
|
186
|
-
flow?: RelaiIntegritasFlow;
|
|
187
|
-
}
|
|
188
|
-
interface ProtectOptions {
|
|
189
|
-
/** Price in USD (e.g., 0.01 for 1 cent) */
|
|
190
|
-
price: DynamicPrice;
|
|
191
|
-
/** Optional token asset address for networks supporting multiple tokens */
|
|
192
|
-
asset?: string;
|
|
193
|
-
/** Wallet address to receive payments, or stripePayTo() for Stripe settlement */
|
|
194
|
-
payTo: string | StripePayTo;
|
|
195
|
-
/** Description shown to payer */
|
|
196
|
-
description?: string;
|
|
197
|
-
/** MIME type of the response (default: application/json) */
|
|
198
|
-
mimeType?: string;
|
|
199
|
-
/** Maximum timeout in seconds (default: 60) */
|
|
200
|
-
maxTimeoutSeconds?: number;
|
|
201
|
-
/** Integritas options (or simple boolean enable flag) */
|
|
202
|
-
integritas?: boolean | RelaiIntegritasOptions;
|
|
203
|
-
/** Override network for this endpoint */
|
|
204
|
-
network?: RelaiNetwork;
|
|
205
|
-
/** Override feePayer address (Solana only) — bypasses facilitator /supported fetch */
|
|
206
|
-
feePayer?: string;
|
|
207
|
-
/** Custom validation after payment is settled */
|
|
208
|
-
customRules?: (req: any) => boolean | Promise<boolean>;
|
|
209
|
-
/** Callback when 402 is returned (no payment provided) */
|
|
210
|
-
onPaymentRequired?: (req: any, info: {
|
|
211
|
-
price: number;
|
|
212
|
-
network: RelaiNetwork;
|
|
213
|
-
}) => void;
|
|
214
|
-
/** Callback when payment is settled successfully */
|
|
215
|
-
onPaymentSettled?: (req: any, result: SettleResult) => void;
|
|
216
|
-
/** Callback on error */
|
|
217
|
-
onError?: (req: any, error: unknown) => void;
|
|
218
|
-
}
|
|
219
|
-
interface SettleResult {
|
|
220
|
-
success: boolean;
|
|
221
|
-
transaction?: string;
|
|
222
|
-
payer?: string;
|
|
223
|
-
network?: string;
|
|
224
|
-
splitTransfers?: Record<string, unknown>;
|
|
225
|
-
integritasFeePaid?: boolean;
|
|
226
|
-
provenance?: Record<string, unknown>;
|
|
227
|
-
integritas?: Record<string, unknown>;
|
|
228
|
-
error?: string;
|
|
229
|
-
errorReason?: string;
|
|
230
|
-
}
|
|
231
|
-
interface PaymentInfo {
|
|
232
|
-
verified: boolean;
|
|
233
|
-
transactionId?: string;
|
|
234
|
-
payer?: string;
|
|
235
|
-
network: RelaiNetwork;
|
|
236
|
-
amount: number;
|
|
237
|
-
}
|
|
238
|
-
/** Config returned by stripePayTo() - used by protect() to create Stripe deposit addresses */
|
|
239
|
-
interface StripePayTo {
|
|
240
|
-
readonly __brand: 'stripePayTo';
|
|
241
|
-
readonly secretKey: string;
|
|
242
|
-
/** Stripe crypto deposits network (default: 'base') */
|
|
243
|
-
readonly stripeNetwork: string;
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Create a Stripe pay-to configuration for x402 payments.
|
|
247
|
-
* Payments settle as USD in your Stripe Dashboard - no crypto knowledge required.
|
|
248
|
-
*
|
|
249
|
-
* Stripe creates a fresh PaymentIntent + deposit address per request.
|
|
250
|
-
* Network is auto-set to Base (Stripe settles USDC on Base).
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```typescript
|
|
254
|
-
* import Relai, { stripePayTo } from '@relai-fi/x402/server';
|
|
255
|
-
*
|
|
256
|
-
* const relai = new Relai({ network: 'base' });
|
|
257
|
-
*
|
|
258
|
-
* app.get('/api/data', relai.protect({
|
|
259
|
-
* price: 0.01,
|
|
260
|
-
* payTo: stripePayTo(process.env.STRIPE_SECRET_KEY!),
|
|
261
|
-
* }), (req, res) => {
|
|
262
|
-
* res.json({ data: 'paid content' });
|
|
263
|
-
* });
|
|
264
|
-
* ```
|
|
265
|
-
*/
|
|
266
|
-
declare function stripePayTo(stripeSecretKey: string, options?: {
|
|
267
|
-
network?: string;
|
|
268
|
-
}): StripePayTo;
|
|
269
|
-
/**
|
|
270
|
-
* Server-side SDK for protecting Express endpoints with x402 micropayments.
|
|
271
|
-
* Settles payments through the RelAI facilitator (zero gas fees for users).
|
|
272
|
-
*
|
|
273
|
-
* Supports: Solana, Base, Avalanche, SKALE Base, SKALE Base Sepolia, SKALE BITE, Polygon, and Ethereum.
|
|
274
|
-
*
|
|
275
|
-
* @example
|
|
276
|
-
* ```typescript
|
|
277
|
-
* import Relai from '@relai-fi/x402/server';
|
|
278
|
-
*
|
|
279
|
-
* const relai = new Relai({ network: 'base' });
|
|
280
|
-
*
|
|
281
|
-
* app.get('/api/data', relai.protect({
|
|
282
|
-
* payTo: '0xYourWallet',
|
|
283
|
-
* price: 0.01, // $0.01 USDC
|
|
284
|
-
* }), (req, res) => {
|
|
285
|
-
* res.json({ data: 'Protected content', payment: req.payment });
|
|
286
|
-
* });
|
|
287
|
-
* ```
|
|
288
|
-
*/
|
|
289
|
-
declare class Relai {
|
|
290
|
-
private network;
|
|
291
|
-
private facilitatorUrl;
|
|
292
|
-
private feePayerCache;
|
|
293
|
-
private plugins;
|
|
294
|
-
private pluginInitPromise;
|
|
295
|
-
constructor(config: RelaiServerConfig);
|
|
296
|
-
private runPluginInit;
|
|
297
|
-
/**
|
|
298
|
-
* Get feePayer address for a network (cached)
|
|
299
|
-
*/
|
|
300
|
-
private getFeePayer;
|
|
301
|
-
/**
|
|
302
|
-
* Express middleware to protect an endpoint with x402 micropayments.
|
|
303
|
-
*
|
|
304
|
-
* Flow:
|
|
305
|
-
* 1. No payment header → returns 402 with payment requirements
|
|
306
|
-
* 2. Payment header present → calls RelAI facilitator `/settle`
|
|
307
|
-
* 3. Settlement success → sets `PAYMENT-RESPONSE` header, attaches `req.payment`, calls `next()`
|
|
308
|
-
*/
|
|
309
|
-
protect(options: ProtectOptions): (req: any, res: any, next: any) => Promise<any>;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
export { type BridgePluginConfig as B, type DynamicPrice as D, type FreeTierPluginConfig as F, type ProtectOptions as P, Relai as R, type SettleResult as S, type RelaiServerConfig as a, type PaymentInfo as b, type StripePayTo as c, type RelaiIntegritasFlow as d, type RelaiIntegritasOptions as e, type RelaiPlugin as f, type PluginContext as g, type PluginResult as h, bridge as i, type FreeTierPlugin as j, type FreeTierUsageExport as k, freeTier as l, stripePayTo as s };
|