Payments

x402 Agents

Easily create AI agents that can pay for any x402-compatible API calls.

Using the MCP Server

The easiest way to equip your agents with the ability to pay for API calls is to use the remote MCP server and provide the tools to your agent.

The MCP server comes with all the tools by default, but you can filter the tools available by passing a comma-separated list of tools as a query parameter.

In this example, we create a ReAct agent using LangChain and filter the MCP tools to fetchWithPayment and getWalletBalance as the only 2 tools we give our agent. You can view the full list of available tools here.

import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { MultiServerMCPClient } from "@langchain/mcp-adapters";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-4.1",
});
const mcpServers = {
thirdweb: {
url: `https://api.thirdweb.com/mcp?secretKey=<your-project-secret-key>&tools=fetchWithPayment,getWalletBalance`,
},
};
// Connect to MCP servers and get tools
const client = new MultiServerMCPClient(mcpServers);
const tools = await client.getTools();
const agent = createReactAgent({
llm: model,
tools: tools,
prompt:
"Use the fetchWithPayment tool to fetch any endpoint. Always pay in {{tokenAddress}}. Your wallet address is {{walletAddress}}.",
});

Using the API directly

You can also create your own MCP tool or wrap all external calls with the /v1/x402/fetchWithPayment endpoint to automatically handle payment flows when APIs return a 402 Payment Required response.

Pass the target url, method and optional from wallet address to the endpoint that will complete the payment. The address should be one of your user wallets or server wallets.

curl -X POST https://api.thirdweb.com/v1/payments/x402/fetch?url=https://api.example.com/premium&method=GET&from=0x1234... \
-H "Content-Type: application/json" \
-H "x-secret-key: <your-project-secret-key>" \
-d '{ ... }' # optional request body passed through to the url called.

Request

fetch(
"https://api.thirdweb.com/v1/payments/x402/fetch?from=0x1234567890123456789012345678901234567890&url=https%3A%2F%2Fapi.example.com%2Fpaid-api&method=GET&maxValue=1000000&asset=0x1234567890123456789012345678901234567890",
{
method: "POST",
headers: {
"x-secret-key": "<your-project-secret-key>",
},
},
);

Response

{
"message": "Returns the final result from the API call"
}