Analytics
See how 402Guard tracks spend per subscription, per agent, and per API, so you can keep budgets under control.
Total Session Spend
$0.19
Total Requests
7
Blocked / Allowed
2/5
Block Rate: 28.6%
By Subscription
| Subscription ID | Requests | Total Spend |
|---|---|---|
| demo-plan | 4 | $0.04 |
| pro-plan | 2 | $0.15 |
| basic-plan | 1 | $0.00 |
By Agent
| Agent ID | Requests | Total Spend |
|---|---|---|
| frontend-demo | 4 | $0.04 |
| backend-bot | 1 | $0.05 |
| scraper-bot | 1 | $0.00 |
| ios-app | 1 | $0.10 |
Recent Requests
| Time | Service | Subscription | Agent | Amount | Status | Network | Tx Hash |
|---|---|---|---|---|---|---|---|
| 12:00:00 PM | weather-api | pro-plan | ios-app | $0.10 | Allowed | avalanche-fuji / USDC | 0xaaaa...3333 |
| 11:05:00 AM | x402-demo | demo-plan | frontend-demo | $0.01 | Allowed | avalanche-fuji / USDC | 0x1111...0000 |
| 11:00:00 AM | weather-api | basic-plan | scraper-bot | $0.00 | Blocked | avalanche-fuji / USDC | - |
| 10:20:00 AM | x402-demo | demo-plan | frontend-demo | $0.00 | Blocked | - / - | - |
| 10:15:00 AM | x402-demo | pro-plan | backend-bot | $0.05 | Allowed | avalanche-fuji / USDC | 0x9876...5432 |
| 10:05:00 AM | x402-demo | demo-plan | frontend-demo | $0.02 | Allowed | avalanche-fuji / USDC | 0xabcd...ef12 |
| 10:00:00 AM | x402-demo | demo-plan | frontend-demo | $0.01 | Allowed | avalanche-fuji / USDC | 0x1234...5678 |
Plug in your own analytics backend
1. Implement the UsageStore Interface
To persist analytics, create a class that implements UsageStore from @402guard/client. Your recordUsage method will need to insert data into your database.
import { type UsageStore, type UsageContext } from "@402guard/client";
export class PostgresUsageStore implements UsageStore {
async recordUsage(ctx: UsageContext): Promise<void> {
// Insert into your database
await db.query(
`INSERT INTO usage_logs (service_id, agent_id, usd_amount, tx_hash, created_at)
VALUES ($1, $2, $3, $4, $5)`,
[ctx.serviceId, ctx.agentId, ctx.usdAmount, ctx.x402.transaction, ctx.timestamp]
);
}
// Implement other methods for budget tracking...
}2. Aggregate Spend Data
Write simple SQL queries or aggregation pipelines to power your dashboard.
-- Total spend by subscription SELECT subscription_id, SUM(usd_amount) as total_spend, COUNT(*) as requests FROM usage_logs GROUP BY subscription_id; -- Total spend by agent SELECT agent_id, SUM(usd_amount) as total_spend, COUNT(*) as requests FROM usage_logs GROUP BY agent_id;
3. Connect to the Guarded Client
Pass your custom store instance when creating the guarded client.
import { createGuardedAxios } from "@402guard/client";
import { PostgresUsageStore } from "./your-store";
const guarded = createGuardedAxios({
store: new PostgresUsageStore(), // Your DB-backed store
policies: {
budgets: [
{ id: "monthly-limit", maxUsdCents: 5000, windowMs: 2592000000, scope: { subscriptionId: "pro-plan" } }
]
},
});
// Now every request is logged to your DB!