Skip to main content

Build an Agent

Build agents that can autonomously interact with APIs, make x402 payments, and access paywalled resources. This guide covers two approaches: a basic agent for simple use cases and an AI-powered agent using LangChain for more complex decision-making.

Prerequisites

  • Node.js and npm installed
  • A SKALE Chain endpoint
  • Understanding of x402 protocol (see Make Payments)
  • Familiarity with TypeScript/JavaScript
  • A wallet with funds (USDC or supported token)
  • Anthropic API key (for LangChain agent with Claude)

Overview


Implementation

A basic agent handles x402 payments programmatically without AI. Perfect for automated scripts, bots, or services that need to access paywalled resources.

Step 1: Install Dependencies

Step 2: Set Up Environment Variables

Create a .env file:

Step 3: Define Your Chain

Create a file chain.ts:

Step 4: Create the Agent Class

Create a file agent.ts:

Step 5: Use the Agent

Create a file index.ts:

Step 6: Run the Agent


Best Practices

  1. Set Spending Limits: Implement maximum payment amounts to prevent unexpected costs
  2. Use Environment Variables: Never hardcode private keys or API keys
  3. Log Transactions: Keep records of all payments for debugging and accounting
  4. Handle Errors Gracefully: Always wrap payment logic in try-catch blocks
  5. Test on Testnet First: Use SKALE testnet before deploying to production

Security Considerations

  • Never expose private keys in client-side code or logs
  • Use secure key management (environment variables, secrets manager)
  • Implement rate limiting to prevent abuse
  • Consider using a dedicated wallet with limited funds for agents
  • Audit your agent’s behavior regularly

Next Steps