You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: smart-accounts-kit/guides/x402/seller.md
+32-17Lines changed: 32 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,21 +11,28 @@ import GlossaryTerm from '@theme/GlossaryTerm';
11
11
12
12
# Create an x402 server with ERC-7710
13
13
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.
15
17
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.
- 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).
23
26
24
27
## Steps
25
28
26
29
### 1. Install the dependencies
27
30
28
-
### 1. Create payment requirements
31
+
```bash npm2yarn
32
+
npm install viem
33
+
```
34
+
35
+
### 2. Create payment requirements
29
36
30
37
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`.
@@ -90,9 +97,10 @@ export type PaymentRequirements = {
90
97
</TabItem>
91
98
</Tabs>
92
99
93
-
### 2. Add a discovery method
100
+
### 3. Add a discovery method
94
101
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.
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.
116
125
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
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.
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.
// Run your business logic and send as a response.
221
231
res.json({ message: 'Premium content unlocked' })
222
232
})
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
+
})
227
237
```
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