Accessing Payer Info
How to use `req.payerAddress` for personalization and user-specific logic.
One of the most powerful features of Atomic Rail is the ability to know exactly who paid for an API request. This allows you to personalize responses or implement token-based systems.
1. Using the Atomic Gate Result
The gate.process(request) method returns a result object containing the verified payerAddress.
// middleware.ts
import { NextResponse } from "next/server";
export async function middleware(req) {
const result = await gate.process(req);
if (result.payerAddress) {
// Pass to downstream Route Handlers via headers
const response = NextResponse.next();
response.headers.set("x-payer-address", result.payerAddress);
return response;
}
}2. Using the usage-gate Wrapper
If you are using the withUsageGate higher-order function, the payerAddress is automatically attached to the request object. This is ideal for Next.js Route Handlers and Hono.
// api/premium/route.ts
import { withUsageGate } from "@atomic-rail/usage-gate";
export const GET = withUsageGate(config, async (req) => {
const address = req.payerAddress;
return Response.json({ message: `Hello, ${address}!` });
});3. Personalization Examples
Premium Quotas
You can use the payerAddress to track user-specific state in your own database.
const user = await db.users.findUnique({ where: { address: req.payerAddress } });
const currentTokens = user.tokens + 100;
await db.users.update({ ... });Simulation Detection
If you need to know if a request is real or a Playground test:
const result = await gate.process(request);
if (result.isSimulated) {
// Return mock data for testing.
// result.payerAddress will be a mock address (e.g. 0x000...)
console.log("Playground simulation detected");
} else {
// Call real OpenAI / Expensive Service
console.log(`Real payment from: ${result.payerAddress}`);
}4. Wallet Address Format
The payerAddress is always returned as a standard EVM-compliant hex string (e.g., 0x...). For Solana, it will be a base58 string.