Skip to main content
The OpenAI Agents SDK lets you give an agent a set of callable tools with typed parameters. By wrapping createAgentClient from @meterlane/x402/client inside a tool() definition, your agent can reach any Meterlane x402-gated route without you writing any payment-retry logic — the client handles the 402 → sign → retry cycle transparently inside the tool’s execute function.
All examples on this page target Base Sepolia (eip155:84532) and the public sandbox demo route. Do not use mainnet keys or mainnet facilitators in development or CI.
1

Install dependencies

You need @openai/agents for the agent runtime, zod for parameter schemas, and @meterlane/x402 for the x402 payment client.
Import the MIT-licensed client — not the server middleware:
The client is MIT-licensed; audit what it signs before running it with a funded wallet.
2

Set your environment variable

Never embed your private key in source code or commit it to version control. Store it in AGENT_PRIVATE_KEY and load it at runtime. Use a dedicated hot wallet with a limited USDC balance — do not reuse cold-storage or exchange keys.
Get test USDC for Base Sepolia from the Circle faucet — select Base Sepolia before requesting.
3

Create the payment tool

The tool calls paidFetch — a fetch wrapper that handles x402 automatically — instead of calling fetch directly.
4

Attach the tool to an agent and run it

Pass the tool to an Agent instance, then call run() with a prompt:
Run the file:
Without OPENAI_API_KEY the script invokes the tool directly, which is useful for verifying that your wallet is funded and the payment flow works before you spend model tokens.
The tool, invoke, and run APIs vary slightly across @openai/agents versions. Adjust imports to match your installed version — the payment logic inside execute stays unchanged regardless.

How it works

When the agent calls meterlanePaidGet, the execute function fires paidFetch(url). The x402 cycle runs inside that call:
  1. The client sends a plain GET to the gateway.
  2. The gateway returns 402 with an X-Payment-Requirements header describing the USDC price and receiver address.
  3. ExactEvmScheme uses your private key to sign an ERC-3009 authorization for that amount on Base Sepolia.
  4. The client retries the request with the authorization attached as a payment header.
  5. The gateway settles on-chain and returns 200 with the upstream response body.
  6. The tool returns { ok: true, status: 200, bodyPreview } to the agent.
Use "eip155:84532" (Base Sepolia) with the default public x402.org facilitator for all development work. Production deployments that need Base mainnet require a private mainnet-capable facilitator configured in your Meterlane dashboard under Settings → Developer.