Skip to content

x402 quickstart

In this guide you’ll charge 0.01 USDC on Base for GET /v1/weather/:city, then make the first payment yourself. The payment lands in your own wallet.

You need:

  • A Cryptonly account with business verification approved. Until it’s approved, payments fail with settlement_disabled.
  • Node.js 18 or later.
  • A wallet on Base to receive payments, such as MetaMask or a Safe.
  • A second wallet with a little USDC on Base, to act as the buyer. It needs no ETH.

Prices from the dashboard need one of the Node.js packages below. For a seller in Python or Go, see Prices in your code.

Open dashboard Create account

Under x402 payments, the Overview tab tracks the steps below:

Start accepting x402 payments checklist: business verified, payout chosen and API key created are done; price your first endpoint and receive your first payment are next.Start accepting x402 payments checklist: business verified, payout chosen and API key created are done; price your first endpoint and receive your first payment are next.
  1. Add a payout wallet

    Go to Settings → Payouts → Add wallet, paste your address and sign the message. Payments will go to this wallet.

    Verified wallets card on Base: Treasury receives payments, Ops Safe can be used as payTo, with an Add wallet button.Verified wallets card on Base: Treasury receives payments, Ops Safe can be used as payTo, with an Add wallet button.
  2. Create an API key

    Go to Settings → Integration → Create x402 key. Save it as CRYPTONLY_API_KEY. You’ll only see it once.

    Create an API key card with one x402 key and a Create x402 key button.Create an API key card with one x402 key and a Create x402 key button.
  3. Set a price

    On the Endpoints tab, click Add endpoint:

    • URL: your API’s URL with the path /v1/weather/:city. :city matches any value.
    • Price: 0.01 USDC on Base.

    To accept another currency too, such as USDC on Solana, click Add currency. The 402 offers every price and the buyer pays with any of them.

    Add paid endpoint form: GET https://api.acme.com/v1/weather/:city, price 0.01 USDC on Base, a description, and List publicly checked.Add paid endpoint form: GET https://api.acme.com/v1/weather/:city, price 0.01 USDC on Base, a description, and List publicly checked.

    Leave List publicly off for now. It adds the endpoint to the discovery feed.

  4. Add the middleware

    Terminal window
    npm install @cryptonly/x402-express @x402/core @x402/express @x402/evm express
    import express from 'express';
    import { cryptonlyPaymentMiddleware } from '@cryptonly/x402-express';
    const app = express();
    app.get('/health', (_req, res) => res.send('ok')); // free routes go first
    const payments = cryptonlyPaymentMiddleware({
    apiKey: process.env.CRYPTONLY_API_KEY!,
    });
    app.use(payments);
    app.get('/v1/weather/:city', (req, res) => {
    res.json({ city: req.params.city, temperatureC: 21 });
    });
    await payments.ready(); // fails at startup if the prices can't load
    app.listen(4021);

    The middleware loads your prices from the dashboard and refreshes them every minute, so price changes need no deploy. It matches endpoints by path, so it works on localhost too.

  5. Check that the endpoint asks for payment

    Terminal window
    curl -i http://localhost:4021/v1/weather/paris
    # HTTP/1.1 402 Payment Required
  6. Pay for it

    Run this as the buyer, with the buyer wallet’s key in BUYER_PRIVATE_KEY:

    Terminal window
    npm install @x402/fetch @x402/evm viem
    import { wrapFetchWithPaymentFromConfig } from '@x402/fetch';
    import { ExactEvmScheme } from '@x402/evm/exact/client';
    import { toClientEvmSigner } from '@x402/evm';
    import { createPublicClient, http } from 'viem';
    import { privateKeyToAccount } from 'viem/accounts';
    import { base } from 'viem/chains';
    const account = privateKeyToAccount(process.env.BUYER_PRIVATE_KEY as `0x${string}`);
    const publicClient = createPublicClient({ chain: base, transport: http() });
    const pay = wrapFetchWithPaymentFromConfig(fetch, {
    schemes: [
    { network: 'eip155:8453', client: new ExactEvmScheme(toClientEvmSigner(account, publicClient)) },
    ],
    });
    const response = await pay('http://localhost:4021/v1/weather/paris');
    console.log(response.status, await response.json());
    // 200 { city: 'paris', temperatureC: 21 }

    The payment shows up on the Payments tab within seconds. For a buyer in Python or Go, see buyer clients.

To try x402 without real money, or before your business verification is approved, use the free sandbox. Follow the same steps with these changes:

  • Use a sandbox account. Its keys and wallets are separate from production.
  • Pass sandbox: true to cryptonlyPaymentMiddleware, cryptonlyPayments or X402Module.forRoot().
  • In the buyer code, use baseSepolia and eip155:84532. Get test USDC from the Circle faucet.
Create sandbox account