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 IDRequestsTotal Spend
demo-plan4$0.04
pro-plan2$0.15
basic-plan1$0.00

By Agent

Agent IDRequestsTotal Spend
frontend-demo4$0.04
backend-bot1$0.05
scraper-bot1$0.00
ios-app1$0.10

Recent Requests

TimeServiceSubscriptionAgentAmountStatusNetworkTx Hash
12:00:00 PMweather-apipro-planios-app$0.10 Allowedavalanche-fuji / USDC0xaaaa...3333
11:05:00 AMx402-demodemo-planfrontend-demo$0.01 Allowedavalanche-fuji / USDC0x1111...0000
11:00:00 AMweather-apibasic-planscraper-bot$0.00 Blockedavalanche-fuji / USDC-
10:20:00 AMx402-demodemo-planfrontend-demo$0.00 Blocked- / --
10:15:00 AMx402-demopro-planbackend-bot$0.05 Allowedavalanche-fuji / USDC0x9876...5432
10:05:00 AMx402-demodemo-planfrontend-demo$0.02 Allowedavalanche-fuji / USDC0xabcd...ef12
10:00:00 AMx402-demodemo-planfrontend-demo$0.01 Allowedavalanche-fuji / USDC0x1234...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!