Guardrails for x402 payments and subscriptions on Avalanche
402Guard is a TypeScript SDK that wraps x402 clients with spend limits, rate limits, usage analytics, and on chain subscription checks. You keep your existing HTTP client and facilitators. We handle the policies.
quickstart.tstypescript
import { createGuardedAxios } from class="text-[#ce9178]">"@402guard/client";
const client = createGuardedAxios({
agentId: class="text-[#ce9178]">"marketing-page-demo",
policies: {
globalDailyUsdCap: 5,
perServiceCaps: {
class="text-[#ce9178]">"api.thirdweb.com": { dailyUsdCap: 1 },
},
},
});
const res = await client.guardedRequest({
url: class="text-[#ce9178]">"https://api.thirdweb.com/x402/paid-endpoint",
method: class="text-[#ce9178]">"GET",
});Why 402Guard?
Guard your x402 spend
Set exact daily or global caps in USD. Stop running up bills on paid APIs accidentally.
On chain subscriptions
Enforce access control based on active on-chain subscriptions on Avalanche Fuji.
Invoices and analytics
Track every paid request. Download generic PDF/JSON invoices for your accounting.
Under the hoodtypescript
// Pseudo-flow of policy enforcement
if (policy.isRateLimited(user)) throw new Error(class="text-[#ce9178]">"Rate limit");
if (subscription.required) {
const active = await chain.isActive(user.address);
if (!active) throw new Error(class="text-[#ce9178]">"Subscription inactive");
}
const quote = await x402.getQuote(url);
if (quote.cost > policy.remainingBudget) {
throw new Error(class="text-[#ce9178]">"Budget exceeded");
}
await x402.pay(quote);
await analytics.record(quote);How it works
1
Deploy Guard402Subscriptions on Avalanche Fuji
2
Wrap your x402 client with createGuardedAxios and configure budgets
3
Expose your paid API endpoint via thirdweb x402 and let 402Guard enforce policies
Integration Examples
client.tstypescript
// Client-side usage with React hooks
const { client } = useGuardedClient();
const fetchData = async () => {
// Automatically handles 402 Payment Required
// Checks local policies before sending
const data = await client.get(class="text-[#ce9178]">"/api/data");
return data;
}