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
@@ -10,23 +10,34 @@ import GlossaryTerm from '@theme/GlossaryTerm';
10
10
11
11
# Pay for an x402 API with delegation
12
12
13
-
In this guide, you use a buyer <GlossaryTermterm="MetaMask smart account">smart account</GlossaryTerm>
14
-
to access API data from an x402 server.
13
+
In this guide, you use a buyer account to access API data from an x402 server by creating
14
+
an <GlossaryTermterm="Open root delegation">open delegation</GlossaryTerm> that authorizes
15
+
token transfers on your behalf.
15
16
16
-
You create an open delegation, restrict redemption to the facilitator, encode the delegation chain,
17
-
and send it as the payment payload when calling a protected API route.
17
+
You set up an `x402Erc7710Client` with a delegation provider, register it with the x402 client,
18
+
and use `wrapFetchWithPayment` to automatically handle payment when calling a protected API route.
18
19
19
20
## Prerequisites
20
21
21
22
-[Install and set up the Smart Accounts Kit.](../../../get-started/install.md)
22
23
23
24
## Steps
24
25
25
-
### 1. Create a buyer account
26
+
### 1. Install the dependencies
26
27
27
-
Create an account to represent the buyer, the <GlossaryTermterm="Delegator account">delegator</GlossaryTerm> who will create a delegation.
28
+
```bash npm2yarn
29
+
npm install @x402/core @x402/fetch @metamask/x402
30
+
```
31
+
32
+
### 2. Create a buyer account
33
+
34
+
Create an account to represent the buyer, the
35
+
<GlossaryTermterm="Delegator account">delegator</GlossaryTerm> who creates a delegation.
28
36
29
-
The delegator must be a <GlossaryTermterm="MetaMask smart account" />; use the toolkit's [`toMetaMaskSmartAccount`](../../../reference/smart-account.md#tometamasksmartaccount) method to create the buyer account.
37
+
The delegator must be a <GlossaryTermterm="MetaMask smart account" />.
38
+
Use the toolkit's
39
+
[`toMetaMaskSmartAccount`](../../../reference/smart-account.md#tometamasksmartaccount) method to
40
+
create the buyer account.
30
41
31
42
:::note Important
32
43
Fund the smart account with USDC for the requested payment.
const paymentRequired =JSON.parse(decodedPaymentRequired) as {
98
-
accepts:PaymentRequirements[]
99
-
}
100
-
101
-
const accepted =paymentRequired.accepts[0]
102
-
if (!accepted) {
103
-
console.error('Server did not provide accepted payment requirements')
104
-
// Handle error
105
-
}
106
-
107
-
if (accepted.extra.assetTransferMethod!=='erc7710') {
108
-
console.error('Server does not support ERC-7710 delegation payments')
109
-
// Handle error
110
-
}
111
-
```
112
-
113
-
</TabItem>
114
-
<TabItemvalue="types.ts">
115
-
116
-
```ts
117
-
exporttypePaymentRequirements= {
118
-
scheme:string
119
-
network:string
120
-
amount:string
121
-
asset:`0x${string}`
122
-
payTo:`0x${string}`
123
-
maxTimeoutSeconds:number
124
-
extra: {
125
-
assetTransferMethod:string
126
-
facilitators?:`0x${string}`[]
127
-
}
128
-
}
129
-
```
130
-
131
-
</TabItem>
132
-
</Tabs>
133
-
134
-
### 3. Create a delegation
81
+
### 3. Create an x402 ERC-7710 client
135
82
136
-
Create an [open root delegation](../../../concepts/delegation/overview.md#open-root-delegation)
137
-
from the buyer smart account. With an open root delegation, the buyer delegates authority without
138
-
setting a specific delegate. Use [`createOpenDelegation`](../../../reference/delegation/index.md#createopendelegation) to create the open root delegation.
83
+
Create an `x402Erc7710Client` with a `delegationProvider` callback.
84
+
The x402 client calls this function automatically when it needs to pay for a request, passing
85
+
in the payment requirements from the server.
139
86
87
+
Inside the provider, create an [open delegation](../../../concepts/delegation/overview.md#open-root-delegation)
88
+
using [`createOpenDelegation`](../../../reference/delegation/index.md#createopendelegation).
140
89
This example uses the [`erc20TransferAmount`](../../../guides/delegation/use-delegation-scopes/spending-limit.md#erc-20-transfer-scope)
141
-
scope to allow USDC transfers up to the amount requested in payment terms.
142
-
It also uses the [`redeemer`](../../../reference/delegation/caveats.md#redeemer) caveat enforcer to restrict
143
-
redemption to facilitator addresses provided by the server.
90
+
scope to allow USDC transfers up to the requested amount, and the
91
+
[`timestamp`](../../../reference/delegation/caveats.md#timestamp) caveat to set a short expiry.
144
92
145
-
:::warning
146
-
Before creating the delegation, make sure your buyer smart account is deployed. If it is not
147
-
deployed, delegation redemption will fail.
148
-
:::
93
+
For ERC-7710, x402 requires the payload fields `delegationManager`, `permissionContext`, and `delegator`.
94
+
Use [`encodeDelegations`](../../../reference/delegation/index.md#encodedelegations) to encode the delegation chain.
0 commit comments