Appearance
x402
Fresh 🌱AgentMail's x402 integration for HTTP-native payments
Getting started
x402 is an open payment protocol that enables HTTP-native payments. By integrating x402 with AgentMail, your agents can pay for API usage directly over HTTP without managing API keys or subscriptions.
X Layer
Agents on X Layer can pay for API usage directly over HTTP via x402. X Layer is an Ethereum Layer 2 network built with Polygon CDK, offering high throughput and near-zero gas fees.
Base URLs
To authenticate with x402 instead of an API key, you must use the x402-specific base URLs below. These replace the default AgentMail base URLs and route requests through the x402 payment layer.
| Protocol | URL |
|---|---|
| HTTP | x402.api.agentmail.to |
| WebSocket | x402.ws.agentmail.to |
Prerequisites
- A crypto wallet with USDC funds (EVM-compatible wallet on Base, or a Solana wallet)
- Node.js installed
Install dependencies
bash
npm install agentmail @x402/fetch @x402/evm viembash
npm install agentmail @x402/fetch @x402/svm @solana/kit @scure/baseQuickstart
typescript
import { privateKeyToAccount } from "viem/accounts";
import { x402Client } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { AgentMailClient } from "agentmail";
// setup x402 client
const PRIVATE_KEY = "0x...";
const signer = privateKeyToAccount(PRIVATE_KEY);
const x402 = new x402Client();
x402.register("eip155:*", new ExactEvmScheme(signer));
// setup AgentMail client
export const client = new AgentMailClient({ x402 });
// create inbox
const inboxRes = await client.inboxes.create({
username: `x402-${Date.now()}`,
});
console.log("Created inbox: ", inboxRes.inboxId);
// subscribe to inbox
const socket = await client.websockets.connect();
console.log("Connected to websocket");
socket.on("message", async (event) => {
if (event.type === "subscribed") {
console.log("Subscribed to", event.inboxIds);
} else if (event.type === "event" && event.eventType === "message.received") {
console.log("Received message from: ", event.message.from);
}
});
socket.sendSubscribe({
type: "subscribe",
inboxIds: [inboxRes.inboxId],
});typescript
import { createKeyPairSignerFromBytes } from "@solana/kit";
import { base58 } from "@scure/base";
import { x402Client } from "@x402/fetch";
import { ExactSvmClient, toClientSvmSigner } from "@x402/svm";
import { AgentMailClient } from "agentmail";
// setup x402 client
const PRIVATE_KEY = "base58-encoded-private-key...";
const keypair = await createKeyPairSignerFromBytes(
base58.decode(PRIVATE_KEY)
);
const x402 = new x402Client();
x402.register("solana:*", new ExactSvmClient(toClientSvmSigner(keypair)));
// setup AgentMail client
export const client = new AgentMailClient({ x402 });
// create inbox
const inboxRes = await client.inboxes.create({
username: `x402-${Date.now()}`,
});
console.log("Created inbox: ", inboxRes.inboxId);
// subscribe to inbox
const socket = await client.websockets.connect();
console.log("Connected to websocket");
socket.on("message", async (event) => {
if (event.type === "subscribed") {
console.log("Subscribed to", event.inboxIds);
} else if (event.type === "event" && event.eventType === "message.received") {
console.log("Received message from: ", event.message.from);
}
});
socket.sendSubscribe({
type: "subscribe",
inboxIds: [inboxRes.inboxId],
});How it works
When you pass an x402 client to AgentMailClient, the SDK automatically handles payment negotiation for each API request. If the server responds with a 402 Payment Required status, the x402 client signs a payment using your wallet and retries the request with the payment attached.
This means your agent can use the full AgentMail API (inboxes, messages, threads, attachments) without needing a traditional API key. Payment happens per-request over HTTP.
Resources
- x402 documentation
- AgentMail API reference
- WebSockets overview