A Vercel AI SDK agent that pays per call.
A working Vercel AI SDK tool that calls a live Apiosk endpoint. The model picks the tool and reads the result; the x402 client underneath answers the 402 and signs a USDC payment on Base per call. No API key, no account.
npm install ai @ai-sdk/anthropic zod @x402/axios @x402/evm axios viemWhat you are about to do
The whole integration is one tool definition and one wallet key. Everything specific to Vercel AI SDK is below; everything specific to payment happens inside the client.
Install the SDK and a paying HTTP client
npm install ai @ai-sdk/anthropic zod @x402/axios @x402/evm axios viem. Put a funded Base wallet key in WALLET_PRIVATE_KEY. Nothing to sign up for, so there is no API key to add.
Define the tool with inputSchema
Wrap the endpoint in tool() from the ai package. Describe the arguments with zod under inputSchema, and let execute call the gateway through the wrapped axios instance. The x402 interceptor handles the 402 and the retry, so execute reads like an ordinary request.
Hand the tool to generateText
Pass it in the tools map on generateText or streamText and bound the loop with stopWhen: isStepCount(n). The model decides when the job needs the tool, calls it with its own arguments, and reads the returned JSON on the next step.
Vercel AI SDK, end to end
Copy these in order. The endpoints are placeholders. Swap in any endpoint from the catalog.
Three things to know
The key lives in the client, not the tool
WALLET_PRIVATE_KEY is read once, where the axios instance is built. Tools import that instance. The model sees the description and the inputSchema and nothing else: not the wallet, not the price, not the payment header. Keep the client in its own module so a second tool cannot reconstruct it with different settings.
execute is async because the call is two round trips
A paid request is a 402 with a price, then a signed retry. Both happen inside execute, so the promise resolves later than an unpaid fetch would. Return the parsed JSON, not the axios response: whatever you return is what the model reads on the next step, and a response object serialises into noise.
stopWhen is the ceiling on a run
maxSteps is gone. stopWhen: isStepCount(n) bounds how many tool round trips one generateText or streamText can make, which is the upper bound on paid calls per run. Leave it out and a loop that keeps calling the tool keeps paying. stepCountIs still resolves as an alias if you have it in older code.
What is Apiosk?
Apiosk helps people and AI agents get data from paid APIs. Describe what you need, review the proposed service or research plan and its price, then approve the work. Apiosk handles the calls and returns the available results with their sources.
What is the difference between Answer and Research?
Answer helps with a direct data request, such as a company profile or filed accounts. Research creates a plan for requests that need several steps. It can resolve a company first and use that identity to retrieve its records. The plan shows what it can cover before you approve.
What if my question is unclear or data is missing?
Apiosk asks for context when it needs a company, location, period or other detail. If a provider does not have the requested data, the result should say what is missing. Data availability depends on the source; an API listing does not guarantee that every record exists.
How much does it cost?
The price depends on the services your request needs. Apiosk shows the total, including its service fee, before you approve. You pay per use from your balance, with no subscription required.
See pricingDo I need to know how APIs work?
No. In the Apiosk app, you ask in ordinary language and approve the price. You do not need a separate API key or account for each provider. Developers can also connect through MCP or API.
Can I use Apiosk inside ChatGPT or Claude?
Yes. Apiosk is available as a connector for ChatGPT and Claude, and through MCP for other agents and tools. Connect once, sign in to Apiosk, and ask your question from there. The price is shown before anything runs and is paid from your Apiosk balance.
Connect an agentCan my AI agent use Apiosk?
Yes. Connect a compatible agent through MCP or API and authorize its access to your Apiosk account. Spending limits and approval rules control what it can buy.
Connect an agent