X402 hands-on
How it works ?

Manage wallet & funds
You can generate a key pair with the command:
cast wallet new
For the sake of this document, we will use the following key pairs:
Client (buyer)
Address: 0x2980bc24bBFB34DE1BBC91479Cb712ffbCE02F73
Private key: 0x9d2675820d55300a05c8991df217a619bcfdc86e2fd91e56443dbbcf159337fd
Server (seller)
Address: 0x7553F6FA4Fb62986b64f79aEFa1fB93ea64A22b1
Private key: 0xf4f7e165433421377856179c698aa387bd8f872657977bd8fa6d62604f41773c
You can add funds to your wallet using the CDP faucet.
Getting ready
You need to install Foundry (explained in the README) and those tools:
npm install -g pnpm
Then, you can download the sources:
git clone git@github.com:coinbase/x402.git
Make it work
Coinbase provides an example server here.
cd x402/examples/typescript/servers/express/
cp .env-local .env
Edit .env and have those two lines to ADDRESS=0x2980bc24bBFB34DE1BBC91479Cb712ffbCE02F73
.
Then you can run the server:
cd ../../
pnpm install
pnpm build
Now, to run the server:
cd servers/express
pnpm install
pnpm dev
Now, we will run a client.
cd ../../clients/fetch
cp .env-local .env
Edit .env and have those two lines to
ADDRESS=0x2980bc24bBFB34DE1BBC91479Cb712ffbCE02F73
PRIVATE_KEY=0x9d2675820d55300a05c8991df217a619bcfdc86e2fd91e56443dbbcf159337fd
pnpm install
pnpm dev
You will get a message like this:
{ report: { weather: 'sunny', temperature: 70 } }
{
success: true,
transaction: '0x83645ca1dd0a5a59653748c8a63dd65126a8dd6097ca3f98fe4b27325f2fa92f',
network: 'base-sepolia',
payer: '0x2980bc24bBFB34DE1BBC91479Cb712ffbCE02F73'
}
The transaction can be seen here.
Last updated