UsageGate in 5 minutes

UsageGate decides, on every request, what each of your users may do under their plan. You write your plans once. Your server asks UsageGate before each action, and gets a yes or a no.

Three words to know

Plan
What you sell, like Free or Pro. Each plan has an id such as plan_free.
Feature key
The thing your code asks about, like ai_credits (a monthly amount) or export_pdf (on or off).
User id
Your own user's id. Your users never see or log into UsageGate.

Links marked ↗ open your dashboard.

  1. Get an API key

    Create a free account, then generate a key in API keys. Keep it on your server.

    .env
    USAGEGATE_KEY=gk_live_...
  2. Set your plans

    Open Access rules. Your workspace starts with Free, Starter and Pro, already filled in (Free gets 10 ai_credits a month). Change the numbers to match your pricing, then Save. More on plans.

  3. Install the SDK

    lib/gate.ts
    // npm install @usagegate/sdk
    import { GateClient } from "@usagegate/sdk";
    
    export const gate = new GateClient(process.env.USAGEGATE_KEY!);
  4. Put new users on the free plan

    New users have no plan until you give them one. Call this after sign-up. It is safe to call again on every login.

    after sign-up
    await gate.grantPlan(user.id, "plan_free");
  5. Check before each action

    For something with a monthly amount, spend one unit. If the user has none left, stop and offer an upgrade.

    app/api/generate/route.ts
    const spent = await gate.consume(user.id, "ai_credits", 1);
    if (!spent.success) {
      return Response.json({ error: "Upgrade to keep going" }, { status: 402 });
    }
    // ...do the work

    For an on/off feature, ask instead of spending:

    on/off feature
    if (!(await gate.canAccess(user.id, "export_pdf"))) {
      return Response.json({ error: "Pro feature" }, { status: 402 });
    }

Other ways to start