Credits system
Unkey’s credits system tracks how many requests a key has remaining:- Create a key with credits — Set
credits.remainingto the initial quota - Each verification consumes credits — By default, 1 credit per request
- Custom costs — Charge different amounts for different operations
- Key becomes invalid at zero — Verification fails with
code: USAGE_EXCEEDED
Keys without a
credits configuration are unlimited and never hit usage limits.Use cases
Subscription quotas
Pro plan: 50,000 requests/month. Credits reset automatically on the 1st.
Pay-per-use APIs
Sell 10,000 requests for $10. Key stops working when credits run out.
Trial tiers
Give new users 100 free requests to try your API. They upgrade or stop.
One-time tokens
Generate a key that works exactly once, then expires.
Creating keys with usage limits
Basic credit limit
Verification response
When verifying a key with credits, the response includes the remaining balance:The
credits field shows remaining credits after this verification. If it returns 999, the key can be used 999 more times.Custom costs per operation
Not all API operations are equal. Complex queries might cost 10 credits while simple lookups cost 1:Cost of zero
Setcost: 0 to verify a key without consuming credits. Useful for:
- Health checks
- Fetching key metadata
- Checking validity before expensive operations
Dynamic pricing example
Adjust costs based on operation type:Auto-refill for subscriptions
Auto-refill automatically restores credits on a schedule. Perfect for subscription models:Monthly refill
Credits reset on the 1st of each month:Custom refill day
If billing cycles start mid-month, userefillDay:
For months with fewer days (e.g., February with 28 days), refills happen on the last day of the month if
refillDay exceeds the month’s length.Daily refill
For free tiers with daily quotas:Managing credits
Add credits manually
When users purchase additional credits:Set exact credit amount
Deduct credits
Remove usage limits
Make a key unlimited:Subscription tier patterns
Tiered plans
Upgrade flow
When a user upgrades mid-cycle:Pay-as-you-go with top-ups
Combined with rate limits
Usage limits and rate limits solve different problems. Use both together:Analytics and billing
Monitor credit consumption for billing insights:Best practices
Set realistic quotas
Set realistic quotas
Analyze your API’s actual usage patterns before setting limits. Too restrictive and you’ll frustrate users; too generous and you’ll hurt profitability.
Notify before limits are hit
Notify before limits are hit
Send alerts when users reach 80% and 90% of their quota. Give them time to upgrade or purchase more credits.
Use cost-based pricing for fairness
Use cost-based pricing for fairness
Charge more for expensive operations. A 5-minute AI inference shouldn’t cost the same as a 10ms database lookup.
Combine with rate limits
Combine with rate limits
Usage limits alone won’t prevent abuse. A user could burn through 10,000 credits in 10 seconds without rate limiting.
Next steps
Rate limit overrides
Adjust rate limits dynamically per customer
Key expiration
Time-based limits for temporary access