casm-agent
v1.0.0
Published
Agent wrapper for automatic x402 payment handling in CASM (Cronos Agentic Service Mesh)
Maintainers
Readme
casm-agent
Agent wrapper for automatic x402 payment handling in CASM (Cronos Agentic Service Mesh).
Installation
npm install casm-agentQuick Start
const { createCASMAgent } = require('casm-agent');
// Create agent with wallet credentials
const agent = createCASMAgent({
privateKey: '0x...your_private_key',
walletAddress: '0x...your_wallet',
onPayment: (payment) => {
console.log(`Paid ${payment.amount} to ${payment.recipient}`);
},
});
// Make requests - payments happen automatically!
const response = await agent.fetch('https://api.provider.com/whale-alerts');
const data = await response.json();
console.log(data);How It Works
- Agent makes request → Provider returns
402 Payment Required - Agent automatically signs EIP-3009 payment → Retries with signature
- Provider verifies → Returns data
- Agent never even knows payment happened! 🎉
┌─────────┐ ┌──────────┐ ┌───────────┐
│ Agent │─────▶│ Provider │─────▶│ x402 │
│ │◀─402─│ │◀─────│ Facilitator│
│ │─sig─▶│ │ │ │
│ │◀─200─│ │ │ │
└─────────┘ └──────────┘ └───────────┘API Reference
createCASMAgent(options)
Create a payment-enabled agent.
| Option | Type | Required | Description |
|--------|------|----------|-------------|
| privateKey | string | ✅ | Agent's private key |
| walletAddress | string | ✅ | Agent's wallet address |
| maxRetries | number | | Max payment attempts (default: 1) |
| onPayment | function | | Called after each payment |
| onError | function | | Called on payment errors |
agent.fetch(url, options)
Fetch wrapper with automatic 402 handling.
// Works like regular fetch
const response = await agent.fetch('https://api.example.com/data', {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});agent.getPaymentHistory()
Get array of all payments made.
const history = agent.getPaymentHistory();
// [{ timestamp, url, amount, recipient, nonce }, ...]agent.getTotalSpent()
Get total amount spent (as BigInt).
const total = agent.getTotalSpent();
console.log(`Total spent: ${total / 1000000n} USDC`);signPayment(params)
Sign an EIP-3009 authorization directly.
const { signPayment } = require('casm-agent');
const signature = await signPayment({
privateKey: '0x...',
from: '0x...sender',
to: '0x...recipient',
amount: '50000',
nonce: 'abc123...',
deadline: Math.floor(Date.now() / 1000) + 300,
tokenAddress: '0x...USDC',
chainId: 338,
});Integration with AI Agents
// Example with a hypothetical AI agent framework
const agent = createCASMAgent({
privateKey: process.env.AGENT_PRIVATE_KEY,
walletAddress: process.env.AGENT_ADDRESS,
});
// AI agent can now use paid APIs transparently
async function getWhaleData() {
const res = await agent.fetch('https://whale-tracker.casm/api/alerts');
return res.json();
}License
MIT
