β οΈ This repository has been archived.The x402 Agent Template has been moved to our official ADK-TS Samples repo:
https://github.com/IQAIcom/adk-ts-samples/tree/main/apps/atp-micropayment-agent
Future updates, maintenance, and support will happen there.
Starter template for creating monetized AI Agents with ADK-TS, IQ AI, and x402 payment protocol
LLM-powered β’ x402 Micropayments β’ IQ AI ATP Integration β’ TypeScript
A template showing how to build AI agents that access IQ AI's Agent Tokenization Platform (ATP) through paid API endpoints using the x402 micropayment protocol. The agent pays for API calls automatically using cryptocurrency, enabling new business models for AI-powered services.
Built with ADK-TS - Agent Development Kit (ADK) for TypeScript
This template demonstrates how to build monetized AI agents that:
-
π€ Access IQ AI's ATP (Agent Tokenization Platform) through paid API endpoints:
- Token Prices: Get current token prices for IQ AI agents
- Agent Holdings: Get wallet holdings for IQ AI agents
- Agent Info: Retrieve agent metadata by token contract address
- Agent Stats: Get performance statistics for agents
- Top Agents: List top-performing agents by market cap, holders, or inferences
-
π° Implement micropayments using the x402 protocol for API access monetization
-
π Automatic payment handling with Web3 wallet integration
-
π Provides monetized API server that proxies premium endpoints
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββββββ
β AI Agent β β x402 Server β β IQ AI ATP API β
β (ADK-TS) β β β β β
β β’ Wallet Client βββββΆβ β’ Payment Gates βββββΆβ β’ Premium Endpoints β
β β’ Premium Tools β β β’ Proxy Routes β β β’ ATP Data β
β β’ Auto Payment β β β’ Price Config β β β’ Agent Analytics β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββββββ
- Node.js 18+ and pnpm
- A Google account (for free AI API access)
- A crypto wallet with Base Sepolia ETH and USDC for micropayments
- Basic understanding of cryptocurrency/Web3
# Clone this repository
git clone https://github.com/IQAIcom/iqai-x402-agent.git
cd iqai-x402-agent
# Install dependencies
pnpm install- Visit Google AI Studio
- Sign in with your Google account
- Click "Create API Key"
- Copy the generated key
You need a wallet private key with Base Sepolia ETH for micropayments:
-
Create a new wallet (recommended for testing):
-
Fund with Base Sepolia ETH:
- Get Base Sepolia ETH from Base Sepolia Faucet or Google Sepolia Faucet
-
Fund with testnet USDC:
- Get testnet USDC from Circle Testnet Faucet
Create environment files for both server and agent:
Server Configuration (.env in /server folder):
# Navigate to server directory and copy environment file
cd server
cp .env.example .envEdit server/.env:
FACILITATOR_URL="https://x402.org/facilitator"
ADDRESS=your_wallet_address_here # Your wallet address (to receive payments)
NETWORK=base-sepoliaAgent Configuration (.env in /agent folder):
# Navigate to agent directory and copy environment file
cd ../agent
cp .env.example .envEdit agent/.env:
ADK_DEBUG=false # Enable debug mode
WALLET_PRIVATE_KEY=your_wallet_private_key_here
GOOGLE_API_KEY=your_google_api_key_hereStart both the payment server and AI agent:
# Start both server and agent in development mode
pnpm devThis will start:
- Server on
http://localhost:3001- handles x402 payments and proxies IQ AI ATP API - Agent on
https://adk-web.iqai.com- provides web interface to interact with the agent
# Check if your server is running and view available endpoints
curl http://localhost:3001/api/price-listExpected response showing endpoint prices:
{
"prices": {
"/api/prices": { "price": "$0.01", "network": "base-sepolia" },
"/api/holdings": { "price": "$0.05", "network": "base-sepolia" },
"/api/agents/info": { "price": "$0.05", "network": "base-sepolia" },
"/api/agents/stats": { "price": "$0.05", "network": "base-sepolia" },
"/api/agents/top": { "price": "$0.10", "network": "base-sepolia" }
}
}- Open the web interface at
https://adk-web.iqai.com - Start a conversation - the agent will greet you and show current endpoint prices
- Ask for agent insights:
- "Show me the top agents by market cap"
- "Get holdings for address 0x..."
- "Tell me about agent at address 0x..."
The agent will:
- β¨ Show prices when you start a conversation
- π Ask permission before making any paid calls
- πΈ Automatically pay using your wallet when you approve
- π Return data from IQ AI's ATP API
To test just the server or agent individually:
# Test just the server
cd server && pnpm dev
# Test just the agent (in another terminal)
cd agent && pnpm dev
# Test agent without web interface
cd agent && npx @iqai/adk-cli run- Base URL:
http://localhost:3001 - Network: Base Sepolia
- Payment Protocol: x402
- Facilitator:
https://x402.org/facilitator
| Endpoint | Price | Description |
|---|---|---|
/api/price-list |
Free | Get endpoint pricing information |
/api/prices |
$0.01 | Get current token prices |
/api/holdings |
$0.05 | Get wallet holdings for IQ AI agents |
/api/agents/info |
$0.05 | Get agent metadata by contract address |
/api/agents/stats |
$0.05 | Get agent performance statistics |
/api/agents/top |
$0.10 | Get top agents by various metrics |
x402-agent/
βββ agent/ # AI Agent (ADK-TS)
β βββ src/
β β βββ agents/
β β β βββ x402/
β β β βββ agent.ts # Main agent configuration
β β β βββ tools.ts # Payment-enabled API tools
β β βββ env.ts # Environment configuration
β βββ package.json
β βββ README.md
βββ server/ # Payment Server (Hono + x402)
β βββ src/
β β βββ index.ts # Payment middleware & ATP proxy
β βββ package.json
β βββ README.md
βββ package.json # Root workspace configuration
βββ README.md
- Create new tools in
agent/src/agents/x402/tools.ts:
const getNewTool = createTool({
name: "GET_NEW_TOOL",
description: "Description of your new tool",
schema: z.object({
param: z.string().describe("Parameter description"),
}),
fn: async ({ param }) => {
const response = await apiClient.get(`/api/new-endpoint`, {
params: { param },
});
return response.data;
},
});- Add to clientTools array and update agent instructions
- Add endpoint to server in
server/src/index.ts:
// Add to PAID_ROUTES configuration
const PAID_ROUTES = {
// ... existing routes
"/api/new-endpoint": { price: "$0.05", network },
};
// Add route handler
app.get("/api/new-endpoint", async (c) => {
// Your endpoint logic here
});- Update agent tools to use the new endpoint
Modify the PAID_ROUTES object in server/src/index.ts:
const PAID_ROUTES: Record<string, { price: string; network: Network }> = {
"/api/prices": { price: "$0.02", network }, // Changed from $0.01
// ... other routes
};Update the network configuration in your server .env:
NETWORK=mainnet # or polygon, optimism, etc.- Ensure the server is running on
http://localhost:3001 - Check that your
.envfiles are properly configured - Verify your wallet has sufficient Base Sepolia ETH or other tokens for payments
- Verify the private key is valid and has Base Sepolia ETH
- Check that the address matches between agent and server config
- Ensure the API key is from Google AI Studio
- Make sure there are no extra spaces in your
.envfile - Verify the key has proper permissions for Gemini API
- Verify the payment server is running and accessible
- Check server logs for any API proxy errors
- Ensure IQ AI ATP API is accessible from your location
This template is open source and contributions are welcome! Feel free to:
- Report bugs or suggest improvements
- Add new tool examples
- Improve documentation
- Share your customizations
π° Ready to monetize? This template gives you everything you need to start building profitable AI-powered applications with micropayments!