x402-engineer 0.1.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/AGENT.md +102 -0
- package/README.md +43 -0
- package/dist/cli.cjs +137 -0
- package/package.json +51 -0
- package/skills/stellar-dev/SKILL.md +146 -0
- package/skills/stellar-dev/advanced-patterns.md +188 -0
- package/skills/stellar-dev/api-rpc-horizon.md +521 -0
- package/skills/stellar-dev/common-pitfalls.md +510 -0
- package/skills/stellar-dev/contracts-soroban.md +565 -0
- package/skills/stellar-dev/ecosystem.md +430 -0
- package/skills/stellar-dev/frontend-stellar-sdk.md +651 -0
- package/skills/stellar-dev/resources.md +306 -0
- package/skills/stellar-dev/security.md +491 -0
- package/skills/stellar-dev/standards-reference.md +94 -0
- package/skills/stellar-dev/stellar-assets.md +419 -0
- package/skills/stellar-dev/testing.md +786 -0
- package/skills/stellar-dev/zk-proofs.md +136 -0
- package/skills/x402-add-paywall/SKILL.md +208 -0
- package/skills/x402-add-paywall/references/patterns.md +132 -0
- package/skills/x402-debug/SKILL.md +92 -0
- package/skills/x402-debug/references/checklist.md +146 -0
- package/skills/x402-explain/SKILL.md +136 -0
- package/skills/x402-init/SKILL.md +129 -0
- package/skills/x402-init/templates/env-example.md +17 -0
- package/skills/x402-init/templates/express/config.ts.md +29 -0
- package/skills/x402-init/templates/express/server.ts.md +30 -0
- package/skills/x402-init/templates/fastify/adapter.ts.md +66 -0
- package/skills/x402-init/templates/fastify/config.ts.md +29 -0
- package/skills/x402-init/templates/fastify/server.ts.md +90 -0
- package/skills/x402-init/templates/hono/config.ts.md +29 -0
- package/skills/x402-init/templates/hono/server.ts.md +31 -0
- package/skills/x402-init/templates/next-app-router/config.ts.md +29 -0
- package/skills/x402-init/templates/next-app-router/server.ts.md +31 -0
- package/skills/x402-stellar/SKILL.md +139 -0
- package/skills/x402-stellar/references/api.md +237 -0
- package/skills/x402-stellar/references/patterns.md +276 -0
- package/skills/x402-stellar/references/setup.md +138 -0
- package/skills/x402-stellar/scripts/check-deps.js +218 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Route Config Template
|
|
2
|
+
|
|
3
|
+
```typescript
|
|
4
|
+
import type { RoutesConfig } from "@x402/core/server";
|
|
5
|
+
|
|
6
|
+
export const SERVER_ADDRESS = process.env.SERVER_STELLAR_ADDRESS!;
|
|
7
|
+
export const FACILITATOR_URL =
|
|
8
|
+
process.env.FACILITATOR_URL ||
|
|
9
|
+
"https://channels.openzeppelin.com/x402/testnet";
|
|
10
|
+
export const FACILITATOR_API_KEY = process.env.FACILITATOR_API_KEY!;
|
|
11
|
+
|
|
12
|
+
export const PRICE = "$0.001";
|
|
13
|
+
export const NETWORK = "stellar:testnet";
|
|
14
|
+
|
|
15
|
+
export const routesConfig: RoutesConfig = {
|
|
16
|
+
// "GET /api/example": {
|
|
17
|
+
// accepts: [
|
|
18
|
+
// {
|
|
19
|
+
// scheme: "exact",
|
|
20
|
+
// price: PRICE,
|
|
21
|
+
// network: NETWORK,
|
|
22
|
+
// payTo: SERVER_ADDRESS,
|
|
23
|
+
// },
|
|
24
|
+
// ],
|
|
25
|
+
// description: "Example protected endpoint",
|
|
26
|
+
// mimeType: "application/json",
|
|
27
|
+
// },
|
|
28
|
+
};
|
|
29
|
+
```
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Hono Server Template
|
|
2
|
+
|
|
3
|
+
Uses the official `@x402/hono` package with `paymentMiddleware`.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { paymentMiddleware } from "@x402/hono";
|
|
7
|
+
import { x402ResourceServer } from "@x402/core/server";
|
|
8
|
+
import { HTTPFacilitatorClient } from "@x402/core/server";
|
|
9
|
+
import { ExactStellarScheme } from "@x402/stellar/exact/server";
|
|
10
|
+
import {
|
|
11
|
+
routesConfig,
|
|
12
|
+
FACILITATOR_URL,
|
|
13
|
+
FACILITATOR_API_KEY,
|
|
14
|
+
NETWORK,
|
|
15
|
+
} from "./config";
|
|
16
|
+
|
|
17
|
+
const facilitatorClient = new HTTPFacilitatorClient({
|
|
18
|
+
url: FACILITATOR_URL,
|
|
19
|
+
createAuthHeaders: async () => {
|
|
20
|
+
const headers = { Authorization: `Bearer ${FACILITATOR_API_KEY}` };
|
|
21
|
+
return { verify: headers, settle: headers, supported: headers };
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const resourceServer = new x402ResourceServer(facilitatorClient).register(
|
|
26
|
+
NETWORK,
|
|
27
|
+
new ExactStellarScheme()
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export const x402Middleware = paymentMiddleware(routesConfig, resourceServer);
|
|
31
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Route Config Template
|
|
2
|
+
|
|
3
|
+
```typescript
|
|
4
|
+
import type { RoutesConfig } from "@x402/core/server";
|
|
5
|
+
|
|
6
|
+
export const SERVER_ADDRESS = process.env.SERVER_STELLAR_ADDRESS!;
|
|
7
|
+
export const FACILITATOR_URL =
|
|
8
|
+
process.env.FACILITATOR_URL ||
|
|
9
|
+
"https://channels.openzeppelin.com/x402/testnet";
|
|
10
|
+
export const FACILITATOR_API_KEY = process.env.FACILITATOR_API_KEY!;
|
|
11
|
+
|
|
12
|
+
export const PRICE = "$0.001";
|
|
13
|
+
export const NETWORK = "stellar:testnet";
|
|
14
|
+
|
|
15
|
+
export const routesConfig: RoutesConfig = {
|
|
16
|
+
// "GET /api/example": {
|
|
17
|
+
// accepts: [
|
|
18
|
+
// {
|
|
19
|
+
// scheme: "exact",
|
|
20
|
+
// price: PRICE,
|
|
21
|
+
// network: NETWORK,
|
|
22
|
+
// payTo: SERVER_ADDRESS,
|
|
23
|
+
// },
|
|
24
|
+
// ],
|
|
25
|
+
// description: "Example protected endpoint",
|
|
26
|
+
// mimeType: "application/json",
|
|
27
|
+
// },
|
|
28
|
+
};
|
|
29
|
+
```
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Next.js App Router Server Template
|
|
2
|
+
|
|
3
|
+
Uses the official `@x402/next` package with `withX402` wrapper.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { withX402 } from "@x402/next";
|
|
7
|
+
import { x402ResourceServer } from "@x402/core/server";
|
|
8
|
+
import { HTTPFacilitatorClient } from "@x402/core/server";
|
|
9
|
+
import { ExactStellarScheme } from "@x402/stellar/exact/server";
|
|
10
|
+
import {
|
|
11
|
+
routesConfig,
|
|
12
|
+
FACILITATOR_URL,
|
|
13
|
+
FACILITATOR_API_KEY,
|
|
14
|
+
NETWORK,
|
|
15
|
+
} from "./config";
|
|
16
|
+
|
|
17
|
+
const facilitatorClient = new HTTPFacilitatorClient({
|
|
18
|
+
url: FACILITATOR_URL,
|
|
19
|
+
createAuthHeaders: async () => {
|
|
20
|
+
const headers = { Authorization: `Bearer ${FACILITATOR_API_KEY}` };
|
|
21
|
+
return { verify: headers, settle: headers, supported: headers };
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const resourceServer = new x402ResourceServer(facilitatorClient).register(
|
|
26
|
+
NETWORK,
|
|
27
|
+
new ExactStellarScheme()
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export { withX402, routesConfig, resourceServer };
|
|
31
|
+
```
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: x402-stellar
|
|
3
|
+
description: >
|
|
4
|
+
Build pay-per-request APIs on Stellar using x402 protocol — HTTP 402 micropayments
|
|
5
|
+
with USDC. Activates on /x402:init, /x402:add-paywall, /x402:explain, /x402:debug
|
|
6
|
+
commands and when "x402, micropayment, paywall, pay per request, 402 payment,
|
|
7
|
+
USDC API, stellar payments, agent economy, monetize API, pay-per-use" mentioned.
|
|
8
|
+
version: 1.0.0
|
|
9
|
+
author: franco-stellar
|
|
10
|
+
mcp-servers: []
|
|
11
|
+
allowed-tools: [Read, Write, Edit, Bash, Grep, Glob]
|
|
12
|
+
tags: [stellar, x402, payments, web3, api, micropayments]
|
|
13
|
+
triggers:
|
|
14
|
+
- /x402:init
|
|
15
|
+
- /x402:add-paywall
|
|
16
|
+
- /x402:explain
|
|
17
|
+
- /x402:debug
|
|
18
|
+
- x402
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
# x402-stellar
|
|
22
|
+
|
|
23
|
+
> You are a **Stellar Payment Infrastructure Engineer** who has deployed x402 paywalls
|
|
24
|
+
> to production, integrated with the OpenZeppelin facilitator, and debugged 402 response
|
|
25
|
+
> headers across dozens of services. You speak from direct experience building
|
|
26
|
+
> pay-per-request APIs on Stellar with USDC micropayments.
|
|
27
|
+
>
|
|
28
|
+
> **Expertise:** x402 protocol, Stellar SDK, USDC payments, Express middleware, payment
|
|
29
|
+
> signing, facilitator integration, testnet/mainnet deployment.
|
|
30
|
+
>
|
|
31
|
+
> **Battle Scars:** missing facilitator API key causing silent 500s, wrong network
|
|
32
|
+
> passphrase rejecting valid payments, clients not handling 402 responses correctly,
|
|
33
|
+
> USDC trustline missing on the receiving account causing settlement failures,
|
|
34
|
+
> confusing server vs client ExactStellarScheme imports.
|
|
35
|
+
>
|
|
36
|
+
> **Contrarian Opinions:**
|
|
37
|
+
> - Don't use API keys when you can use x402 — let the protocol handle auth and billing.
|
|
38
|
+
> - Don't build billing systems when USDC handles it — the blockchain IS your ledger.
|
|
39
|
+
> - Don't gate content with subscriptions when micropayments per request are fairer.
|
|
40
|
+
|
|
41
|
+
## Workflow
|
|
42
|
+
|
|
43
|
+
### Step 1 — Detect Project Type
|
|
44
|
+
|
|
45
|
+
Check the existing project structure to determine the framework in use.
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Check for Express, Fastify, or other frameworks
|
|
49
|
+
cat package.json 2>/dev/null | grep -E '"express"|"fastify"|"hono"|"koa"'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
If no project exists, scaffold with Express (the only framework with official x402 middleware).
|
|
53
|
+
|
|
54
|
+
### Step 2 — Install Dependencies
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm install @x402/express @x402/stellar @x402/core @stellar/stellar-sdk dotenv
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
For TypeScript projects, also install:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm install -D typescript @types/express @types/node tsx
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Step 3 — Configure Environment
|
|
67
|
+
|
|
68
|
+
Create a `.env` file with the required variables. See [references/setup.md](references/setup.md) for how to obtain each value.
|
|
69
|
+
|
|
70
|
+
```env
|
|
71
|
+
SERVER_STELLAR_ADDRESS=G...
|
|
72
|
+
FACILITATOR_URL=https://channels.openzeppelin.com/x402/testnet
|
|
73
|
+
FACILITATOR_API_KEY=your-api-key
|
|
74
|
+
CLIENT_STELLAR_SECRET=S...
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Run the dependency checker to validate:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
node scripts/check-deps.js
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Step 4 — Add x402 Payment Middleware
|
|
84
|
+
|
|
85
|
+
Add the payment middleware to your Express server. See [references/patterns.md](references/patterns.md) for the complete server pattern.
|
|
86
|
+
|
|
87
|
+
Key points:
|
|
88
|
+
- Create the `HTTPFacilitatorClient` with auth headers from your API key
|
|
89
|
+
- Create the `x402ResourceServer` and register `stellar:testnet` with `ExactStellarScheme`
|
|
90
|
+
- Define route pricing in the `paymentMiddleware` configuration object
|
|
91
|
+
- Each route specifies scheme, price, network, and payTo address
|
|
92
|
+
|
|
93
|
+
### Step 5 — Create Client with Payment Signer
|
|
94
|
+
|
|
95
|
+
Build the client that handles 402 responses and signs payments. See [references/patterns.md](references/patterns.md) for the complete client pattern.
|
|
96
|
+
|
|
97
|
+
Key points:
|
|
98
|
+
- Use `createEd25519Signer` with the client's Stellar secret key
|
|
99
|
+
- Register the signer with `ExactStellarScheme` on the client side
|
|
100
|
+
- Use `x402HTTPClient` to parse 402 responses and create payment payloads
|
|
101
|
+
- Resend the original request with the payment signature header
|
|
102
|
+
|
|
103
|
+
### Step 6 — Test the 402 Flow
|
|
104
|
+
|
|
105
|
+
1. Start the server
|
|
106
|
+
2. Send a request without payment — expect HTTP 402
|
|
107
|
+
3. Parse the 402 response headers for payment requirements
|
|
108
|
+
4. Sign and send payment with the client
|
|
109
|
+
5. Verify the response returns 200 with the expected data
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# Start server
|
|
113
|
+
npx tsx src/server.ts &
|
|
114
|
+
|
|
115
|
+
# Test without payment (should get 402)
|
|
116
|
+
curl -s -o /dev/null -w "%{http_code}" -X POST http://localhost:3000/api/endpoint
|
|
117
|
+
|
|
118
|
+
# Run the full client flow
|
|
119
|
+
npx tsx src/client.ts
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Common Mistakes
|
|
123
|
+
|
|
124
|
+
| Mistake | Why It Fails | Fix |
|
|
125
|
+
|---------|-------------|-----|
|
|
126
|
+
| Missing facilitator API key | Server can't verify or settle payments; returns 500 instead of 402 | Generate a key at `channels.openzeppelin.com/testnet/gen` |
|
|
127
|
+
| Wrong import path for ExactStellarScheme | Server and client export different classes with the same name | Server: `@x402/stellar/exact/server` — Client: `@x402/stellar/exact/client` |
|
|
128
|
+
| Client using createStellarSigner for payment directly | That creates the signer, not the full payment flow | Use `x402Client` + `x402HTTPClient` + `ExactStellarScheme(signer)` together |
|
|
129
|
+
| No USDC trustline on receiving account | Payment settlement fails because the account can't hold USDC | Add USDC trustline via Stellar Laboratory or SDK before accepting payments |
|
|
130
|
+
| Hardcoding network to mainnet | Testnet payments fail silently because the network passphrase doesn't match | Use `"stellar:testnet"` for development and testing |
|
|
131
|
+
| Not parsing x-payment header | Client can't extract payment requirements from the 402 response | Use `httpClient.getPaymentRequiredResponse()` to parse headers |
|
|
132
|
+
| Forgetting `express.json()` middleware | Request body parsing fails on POST endpoints | Add `app.use(express.json())` before the payment middleware |
|
|
133
|
+
| Price format without dollar sign | Middleware rejects the price configuration | Always use `"$0.001"` format with the dollar sign prefix |
|
|
134
|
+
|
|
135
|
+
## References
|
|
136
|
+
|
|
137
|
+
- [references/setup.md](references/setup.md) — Account creation, API keys, environment setup
|
|
138
|
+
- [references/patterns.md](references/patterns.md) — Working server and client code patterns
|
|
139
|
+
- [references/api.md](references/api.md) — Package API reference for @x402 modules
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# x402-stellar API Reference
|
|
2
|
+
|
|
3
|
+
## @x402/express
|
|
4
|
+
|
|
5
|
+
Express middleware for x402 payment verification and settlement.
|
|
6
|
+
|
|
7
|
+
### `paymentMiddleware(routeConfig, resourceServer)`
|
|
8
|
+
|
|
9
|
+
Creates Express middleware that intercepts requests matching configured routes and requires payment before allowing them through.
|
|
10
|
+
|
|
11
|
+
**Parameters:**
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Description |
|
|
14
|
+
|-----------|------|-------------|
|
|
15
|
+
| `routeConfig` | `Record<string, RoutePaymentConfig>` | Map of `"METHOD /path"` to payment configuration |
|
|
16
|
+
| `resourceServer` | `x402ResourceServer` | Configured resource server instance |
|
|
17
|
+
|
|
18
|
+
**RoutePaymentConfig:**
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
interface RoutePaymentConfig {
|
|
22
|
+
accepts: PaymentAccept[];
|
|
23
|
+
description: string;
|
|
24
|
+
mimeType: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface PaymentAccept {
|
|
28
|
+
scheme: "exact"; // Payment scheme identifier
|
|
29
|
+
price: string; // Dollar-prefixed price: "$0.001"
|
|
30
|
+
network: string; // Network identifier: "stellar:testnet"
|
|
31
|
+
payTo: string; // Stellar public key (G...)
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Returns:** Express middleware function.
|
|
36
|
+
|
|
37
|
+
**Behavior:**
|
|
38
|
+
- Requests to unconfigured routes pass through without payment.
|
|
39
|
+
- Requests to configured routes without valid payment receive HTTP 402 with payment requirements in headers.
|
|
40
|
+
- Requests with valid payment are verified, then passed to the next handler.
|
|
41
|
+
- After the response is sent, payment is settled via the facilitator.
|
|
42
|
+
|
|
43
|
+
### `x402ResourceServer`
|
|
44
|
+
|
|
45
|
+
Manages payment scheme registration and facilitator communication.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
const resourceServer = new x402ResourceServer(facilitatorClient);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**Methods:**
|
|
52
|
+
|
|
53
|
+
| Method | Signature | Description |
|
|
54
|
+
|--------|-----------|-------------|
|
|
55
|
+
| `register` | `(network: string, scheme: SchemeInstance) => this` | Register a payment scheme for a network. Returns `this` for chaining. |
|
|
56
|
+
|
|
57
|
+
**Example:**
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const resourceServer = new x402ResourceServer(facilitatorClient)
|
|
61
|
+
.register("stellar:testnet", new ExactStellarScheme());
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## @x402/stellar
|
|
67
|
+
|
|
68
|
+
Stellar-specific signers and payment scheme implementations.
|
|
69
|
+
|
|
70
|
+
### `createEd25519Signer(secretKey)`
|
|
71
|
+
|
|
72
|
+
Creates a Stellar Ed25519 signer from a secret key. Used on the **client side** to sign payment transactions.
|
|
73
|
+
|
|
74
|
+
**Parameters:**
|
|
75
|
+
|
|
76
|
+
| Parameter | Type | Description |
|
|
77
|
+
|-----------|------|-------------|
|
|
78
|
+
| `secretKey` | `string` | Stellar secret key (S..., 56 characters) |
|
|
79
|
+
|
|
80
|
+
**Returns:** `Ed25519Signer` — signer object compatible with `ExactStellarScheme`.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { createEd25519Signer } from "@x402/stellar";
|
|
84
|
+
|
|
85
|
+
const signer = createEd25519Signer("SCZANGBA5YHTNYVVV3C7CAZMCLP...");
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### `ExactStellarScheme` (Server)
|
|
89
|
+
|
|
90
|
+
**Import:** `@x402/stellar/exact/server`
|
|
91
|
+
|
|
92
|
+
Server-side scheme for verifying and settling Stellar payments. Takes **no constructor arguments**.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { ExactStellarScheme } from "@x402/stellar/exact/server";
|
|
96
|
+
|
|
97
|
+
const scheme = new ExactStellarScheme();
|
|
98
|
+
// Register with resource server
|
|
99
|
+
resourceServer.register("stellar:testnet", scheme);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### `ExactStellarScheme` (Client)
|
|
103
|
+
|
|
104
|
+
**Import:** `@x402/stellar/exact/client`
|
|
105
|
+
|
|
106
|
+
Client-side scheme for creating and signing Stellar payment transactions. Takes a **signer** as constructor argument.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { ExactStellarScheme } from "@x402/stellar/exact/client";
|
|
110
|
+
import { createEd25519Signer } from "@x402/stellar";
|
|
111
|
+
|
|
112
|
+
const signer = createEd25519Signer(process.env.CLIENT_STELLAR_SECRET!);
|
|
113
|
+
const scheme = new ExactStellarScheme(signer);
|
|
114
|
+
// Register with x402 client
|
|
115
|
+
client.register("stellar:testnet", scheme);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
> **Critical:** Do not confuse the server and client `ExactStellarScheme`. They share the same class name but have different import paths and different constructor signatures.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## @x402/core
|
|
123
|
+
|
|
124
|
+
Core x402 protocol utilities shared between server and client.
|
|
125
|
+
|
|
126
|
+
### `HTTPFacilitatorClient` (Server)
|
|
127
|
+
|
|
128
|
+
**Import:** `@x402/core/server`
|
|
129
|
+
|
|
130
|
+
HTTP client for communicating with the OpenZeppelin facilitator service.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { HTTPFacilitatorClient } from "@x402/core/server";
|
|
134
|
+
|
|
135
|
+
const facilitatorClient = new HTTPFacilitatorClient({
|
|
136
|
+
url: "https://channels.openzeppelin.com/x402/testnet",
|
|
137
|
+
createAuthHeaders: async () => {
|
|
138
|
+
const headers = { Authorization: `Bearer ${apiKey}` };
|
|
139
|
+
return { verify: headers, settle: headers, supported: headers };
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**Constructor Options:**
|
|
145
|
+
|
|
146
|
+
| Option | Type | Description |
|
|
147
|
+
|--------|------|-------------|
|
|
148
|
+
| `url` | `string` | Facilitator service URL |
|
|
149
|
+
| `createAuthHeaders` | `() => Promise<AuthHeaders>` | Async function returning auth headers for each operation |
|
|
150
|
+
|
|
151
|
+
**AuthHeaders:**
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
interface AuthHeaders {
|
|
155
|
+
verify: Record<string, string>; // Headers for payment verification requests
|
|
156
|
+
settle: Record<string, string>; // Headers for payment settlement requests
|
|
157
|
+
supported: Record<string, string>; // Headers for supported schemes requests
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `x402Client` (Client)
|
|
162
|
+
|
|
163
|
+
**Import:** `@x402/core/client`
|
|
164
|
+
|
|
165
|
+
Core client for managing payment schemes and creating payments.
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { x402Client } from "@x402/core/client";
|
|
169
|
+
|
|
170
|
+
const client = new x402Client();
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
**Methods:**
|
|
174
|
+
|
|
175
|
+
| Method | Signature | Description |
|
|
176
|
+
|--------|-----------|-------------|
|
|
177
|
+
| `register` | `(network: string, scheme: SchemeInstance) => this` | Register a payment scheme for a network |
|
|
178
|
+
|
|
179
|
+
### `x402HTTPClient` (Client)
|
|
180
|
+
|
|
181
|
+
**Import:** `@x402/core/client`
|
|
182
|
+
|
|
183
|
+
HTTP-aware client that handles 402 response parsing and payment header encoding.
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { x402HTTPClient } from "@x402/core/client";
|
|
187
|
+
|
|
188
|
+
const httpClient = new x402HTTPClient(client);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**Methods:**
|
|
192
|
+
|
|
193
|
+
| Method | Signature | Description |
|
|
194
|
+
|--------|-----------|-------------|
|
|
195
|
+
| `getPaymentRequiredResponse` | `(headerGetter: (name: string) => string \| null) => PaymentRequired` | Parse 402 response headers into payment requirements |
|
|
196
|
+
| `createPaymentPayload` | `(paymentRequired: PaymentRequired) => Promise<PaymentPayload>` | Create and sign a payment transaction |
|
|
197
|
+
| `encodePaymentSignatureHeader` | `(payload: PaymentPayload) => Record<string, string>` | Encode payment into HTTP headers for the retry request |
|
|
198
|
+
|
|
199
|
+
**Usage Flow:**
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// 1. Parse 402 headers
|
|
203
|
+
const paymentRequired = httpClient.getPaymentRequiredResponse(
|
|
204
|
+
(name) => response.headers.get(name)
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
// 2. Create payment (signs Stellar transaction)
|
|
208
|
+
const payload = await httpClient.createPaymentPayload(paymentRequired);
|
|
209
|
+
|
|
210
|
+
// 3. Encode as headers
|
|
211
|
+
const headers = httpClient.encodePaymentSignatureHeader(payload);
|
|
212
|
+
|
|
213
|
+
// 4. Resend request with payment headers
|
|
214
|
+
const paidResponse = await fetch(url, { headers: { ...originalHeaders, ...headers } });
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## Network Identifiers
|
|
220
|
+
|
|
221
|
+
| Identifier | Network | Use Case |
|
|
222
|
+
|------------|---------|----------|
|
|
223
|
+
| `stellar:testnet` | Stellar Testnet | Development and testing |
|
|
224
|
+
| `stellar:pubnet` | Stellar Mainnet | Production |
|
|
225
|
+
|
|
226
|
+
## Price Format
|
|
227
|
+
|
|
228
|
+
Prices are specified as dollar-prefixed strings:
|
|
229
|
+
|
|
230
|
+
| Format | Value |
|
|
231
|
+
|--------|-------|
|
|
232
|
+
| `"$0.001"` | One-tenth of a cent |
|
|
233
|
+
| `"$0.01"` | One cent |
|
|
234
|
+
| `"$0.10"` | Ten cents |
|
|
235
|
+
| `"$1.00"` | One dollar |
|
|
236
|
+
|
|
237
|
+
The middleware converts these to the appropriate USDC amount for the Stellar transaction.
|