Running our example server
If you’d like a working example, you can obtain the example server source code by clicking or by running git clone https://github.com/mogami-tech/x402-examples.git
.
Once retrieved, go inside the project directory and type:
The server will start on the 4021 port, you can check with this command:
Copy curl localhost:4021/weather
You will get this kind of result:
Copy {
"x402Version": 1,
"accepts": [
{
"scheme": "exact",
"network": "base-sepolia",
"maxAmountRequired": "1000",
"resource": "http://localhost:4021/weather",
"description": "",
"mimeType": "",
"payTo": "0x7553F6FA4Fb62986b64f79aEFa1fB93ea64A22b1",
"maxTimeoutSeconds": 60,
"asset": "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
"extra": {
"name": "USDC",
"version": "2"
}
}
],
"error": "Payment required"
}
Running a client
For the moment, we will use the official Coinbase client to call our example service with a payment. Get it by typing:
Copy git clone git@github.com:coinbase/x402.git
cd x402/examples/typescript/clients/fetch
cp .env-local .env
Edit the .env
and set this:
Copy RESOURCE_SERVER_URL=http://localhost:4021
ENDPOINT_PATH=/weather
ADDRESS=0xAbb25843e9cba098361735eBC79a7fc47019b117
PRIVATE_KEY=0x9b9569df59719e5a3e6a82eb180a5435bec9a4aafb657e351be9ce47ed6cfcb7
Run the client with:
Copy pnpm install
pnpm dev
You will get a message like this:
Copy { report: { weather: 'sunny', temperature: 70 } }
Copy {
success: true,
transaction: '0x83645ca1dd0a5a59653748c8a63dd65126a8dd6097ca3f98fe4b27325f2fa92f',
network: 'base-sepolia',
payer: '0x2980bc24bBFB34DE1BBC91479Cb712ffbCE02F73'
}