Agent Basic
A minimal example of an agent making a single paid API call.
What it does
- Creates a
Crumbinstance with your private key - Checks the Gateway balance
- Makes a single
POST /searchrequest to a local provider - Prints the response, amount paid, and transaction hash
Source
import { Crumb } from 'crumb-alpha-sdk'
const crumb = new Crumb({
privateKey: process.env.TEST_PRIVATE_KEY!,
chain: 'arcTestnet',
onLowBalance: (balance: string) => {
console.warn(`Low balance warning: ${balance} USDC remaining`)
},
})
async function main() {
console.log('Agent address:', crumb.address)
console.log('Gateway balance:', await crumb.balance(), 'USDC')
const result = await crumb.fetch('http://localhost:3001/search', {
method: 'POST',
body: { query: 'AI research papers 2025' },
maxPayment: 0.01,
})
console.log('Results:', result.data)
console.log('Paid:', result.formattedAmount, 'USDC')
console.log('Tx:', result.txHash)
}
main().catch(console.error)
Running
Requires a provider running on port 3001 (see Provider Express).
# Terminal 1 — start the provider
npx tsx --env-file=.env examples/provider-express/index.ts
# Terminal 2 — run the agent
npx tsx --env-file=.env examples/agent-basic/index.ts
Expected output
Agent address: 0x22b204CF75B960c6cCf3A49D78a3Aa9845953752
Gateway balance: 9.995 USDC
Results: { results: [...] }
Paid: 0.001 USDC
Tx: 0xabc...