tool() helper lets you give a language model structured actions it can invoke during a conversation. By combining tool() with createAgentClient from @meterlane/x402/client, you can expose any Meterlane gateway route as a model-callable tool that handles x402 payment negotiation without any extra logic in your application code.
All examples on this page target Base Sepolia (
eip155:84532) and the public sandbox demo route. Do not use mainnet keys in development or CI environments.1
Install dependencies
You need the Import the MIT-licensed client entry point — not the server middleware:
ai package for the tool primitive and generateText, zod for parameter schemas, and @meterlane/x402 for the payment client.2
Set your environment variable
3
Define the tool
Save the following as
vercel-ai-meterlane-tool.ts. The execute function uses paidFetch as a drop-in replacement for fetch; x402 payment is handled automatically on 402 responses.4
Call the tool and optionally attach it to a model
You can invoke the tool directly for testing, or pass it to Run the file:A successful paid call returns
generateText so the model decides when to call it:"status": 200 and a bodyPreview with the upstream API response.How it works
Theexecute handler calls paidFetch instead of native fetch. When the Meterlane gateway returns 402, @meterlane/x402/client reads the X-Payment-Requirements header, uses ExactEvmScheme to produce an ERC-3009 USDC authorization signed by your private key on Base Sepolia, and retries the request with the authorization header attached. The second request receives 200 and the tool returns the response body to the model.
The tool also forwards the settlement receipt header (PAYMENT-RESPONSE) back to the model so it has an on-chain record of the payment if needed.