Skip to content

Commit d46e5fe

Browse files
address review comments
1 parent 34323c7 commit d46e5fe

1 file changed

Lines changed: 32 additions & 17 deletions

File tree

smart-accounts-kit/guides/x402/seller.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@ import GlossaryTerm from '@theme/GlossaryTerm';
1111

1212
# Create an x402 server with ERC-7710
1313

14-
In this guide, you build a Node.js server that charges for HTTP API access using [x402](https://www.x402.org/) and verifies payments through the MetaMask facilitator.
14+
In this guide, you build a Node.js server that charges for HTTP API access using
15+
[x402](https://www.x402.org/) and accepts [ERC-7710](https://eips.ethereum.org/EIPS/eip-7710)
16+
delegations payments verified through the MetaMask facilitator.
1517

16-
The official `@x402/express` middleware doesn't support ERC-7710 delegation payloads, so you implement the x402 HTTP contract manually.
18+
The official `@x402/express` middleware doesn't support ERC-7710 delegation payloads. This guide
19+
shows you how to implement the x402 HTTP contract manually with Express.
1720

1821
## Prerequisites
1922

2023
- [Node.js 18](https://nodejs.org/en) or later.
21-
- [Minimal Node.js Express server](https://expressjs.com/en/starter/installing.html).
22-
- A seller payout address to recieve funds (for example, [MetaMask Wallet](https://metamask.io/download)).
24+
- A [Node.js Express server](https://expressjs.com/en/starter/installing.html).
25+
- A seller payout address to receive funds (for example, a [MetaMask wallet](https://metamask.io/download) address).
2326

2427
## Steps
2528

2629
### 1. Install the dependencies
2730

28-
### 1. Create payment requirements
31+
```bash npm2yarn
32+
npm install viem
33+
```
34+
35+
### 2. Create payment requirements
2936

3037
Define the payment requirements that every protected route returns to buyers. Follow the [x402 payment requirements schema](https://docs.x402.org/core-concepts/http-402#payment-headers-in-v2), and set `scheme` to `erc7710`.
3138

@@ -61,7 +68,7 @@ export const paymentRequirements: PaymentRequirements = {
6168
resource: '/api/premium',
6269
description: 'Premium API access',
6370
mimeType: 'application/json',
64-
payTo: 'YOUR_PAYTO_ADDRESS',
71+
payTo: '<YOUR_PAYTO_ADDRESS>',
6572
maxTimeoutSeconds: 60,
6673
// USDC contract address on Base.
6774
asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
@@ -90,9 +97,10 @@ export type PaymentRequirements = {
9097
</TabItem>
9198
</Tabs>
9299
93-
### 2. Add a discovery method
100+
### 3. Add a discovery method
94101
95-
Add a public `GET /info` route that publishes your protected paths and their payment terms. Buyers call this route before they prepare a ERC-7710 delegation payment.
102+
Add a public `GET /info` route that publishes your protected paths and their payment terms.
103+
Buyers call this route before they prepare an ERC-7710 delegation payment.
96104
97105
```ts
98106
// src/index.ts
@@ -110,11 +118,13 @@ app.get('/info', (_req, res) => {
110118
})
111119
```
112120

113-
### 3. Add payment middleware
121+
### 4. Add payment middleware
114122

115-
Add payment middleware that runs before each protected handler. Parse the base64 `X-PAYMENT` header to extract the encoded delegation chain (`permissionContext`) the buyer prepared.
123+
Add payment middleware that runs before each protected handler. Parse the base64 `X-PAYMENT` header to
124+
extract the encoded delegation chain (`permissionContext`) the buyer prepared.
116125

117-
When the `X-PAYMENT` header is missing or malformed, respond with `402 Payment Required` and a JSON body that tells the buyer how to pay.
126+
When the `X-PAYMENT` header is missing or malformed, respond with `402 Payment Required` and a JSON body
127+
that tells the buyer how to pay.
118128

119129
```ts
120130
// src/middleware.ts
@@ -146,7 +156,7 @@ export function requirePayment(req: Request, res: Response, next: NextFunction)
146156
}
147157
```
148158

149-
### 4. Verify the payment
159+
### 5. Verify the payment
150160

151161
Call the MetaMask facilitator's `verify` endpoint to confirm the encoded delegation chain authorizes the requested resource. If verification fails, return `402` with the failure reason from the facilitator so the buyer can correct the payment and retry.
152162

@@ -192,7 +202,7 @@ const FACILITATOR_URL = 'https://tx-sentinel-base-mainnet.dev-api.cx.metamask.io
192202
</TabItem>
193203
</Tabs>
194204

195-
### 5. Settle the payment
205+
### 6. Settle the payment
196206

197207
After verification succeeds, run your business logic and call the MetaMask facilitator's `settle` endpoint. Attach the settlement result to the response as the `X-PAYMENT-RESPONSE` header so the buyer can correlate it with the original request.
198208

@@ -220,8 +230,13 @@ app.get('/api/premium', requirePayment, verifyPayment, async (_req, res) => {
220230
// Run your business logic and send as a response.
221231
res.json({ message: 'Premium content unlocked' })
222232
})
223-
;(app.listen(4402),
224-
() => {
225-
console.log('Seller listening on port 4402')
226-
})
233+
234+
app.listen(4402, () => {
235+
console.log('Seller listening on port 4402')
236+
})
227237
```
238+
239+
## Next steps
240+
241+
- Learn more about [ERC-7710 delegation](../../concepts/delegation/overview.md).
242+
- See the [x402 ERC-7710 specification](https://github.com/coinbase/x402/blob/main/specs/schemes/exact/scheme_exact_evm.md#3-assettransfermethod-erc-7710).

0 commit comments

Comments
 (0)