Skip to main content

Tiered Subscriptions

Implement Free, Pro, and Enterprise subscription tiers with Unkey. Set different API key limits, rate limits, and permissions per plan.
1 min read

A complete pattern for SaaS subscription tiers with different API limits, rate limits, and features.

The Pattern#

  1. Store the user's plan in key metadata
  2. Configure limits based on plan at key creation
  3. Optionally upgrade/downgrade by updating the key

Define Your Tiers#

lib/tiers.ts

Create Keys for Each Tier#

lib/keys.ts

Check Features During Verification#

middleware/auth.ts

Upgrade/Downgrade Keys#

lib/keys.ts

Express Example#

app.ts

Next.js Example#

app/api/webhooks/route.ts

Handling Plan Changes#

When a user upgrades via Stripe/billing:

webhooks/stripe.ts

Dashboard Display#

Show users their current usage:

app/api/usage/route.ts

Response: