Design API quota systems with usage tracking, limits enforcement, and billing integration.
You are a platform engineer building usage-based billing and quota management systems.
## CONTEXT
I need to implement quota management for my API.
**Requirements:**
- Quota Types: {{QUOTA_TYPES}}
- Billing Model: {{BILLING}}
- Tiers: {{TIERS}}
- Enforcement: {{ENFORCEMENT}}
## TASK
Design quota management system:
### 1. QUOTA DEFINITION
```json
{
"plan": "pro",
"quotas": {
"api_requests": { "limit": 100000, "period": "month" },
"storage_gb": { "limit": 50, "period": "lifetime" },
"team_members": { "limit": 10, "period": "lifetime" }
}
}
```
### 2. USAGE TRACKING
```json
{
"userId": "user_123",
"period": "2024-01",
"usage": {
"api_requests": { "used": 45000, "limit": 100000, "remaining": 55000 },
"storage_gb": { "used": 12.5, "limit": 50, "remaining": 37.5 }
}
}
```
### 3. RESPONSE HEADERS
```
X-Quota-Limit: 100000
X-Quota-Used: 45000
X-Quota-Remaining: 55000
X-Quota-Reset: 2024-02-01T00:00:00Z
```
### 4. ENFORCEMENT
```typescript
async function checkQuota(user, quotaType) {
const usage = await getUsage(user, quotaType);
if (usage.used >= usage.limit) {
throw new QuotaExceededError({
quotaType,
limit: usage.limit,
upgradeUrl: '/pricing'
});
}
}
```
### 5. OVERAGE HANDLING
- Hard limit (block)
- Soft limit (warn)
- Overage billing
### 6. BILLING INTEGRATION
Usage reporting for invoicing.Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{QUOTA_TYPES][{BILLING][{TIERS][{ENFORCEMENT]{
"plan": "pro",
"quotas": {
"api_requests": { "limit": 100000, "period": "month" }{ "limit": 50, "period": "lifetime" }{ "limit": 10, "period": "lifetime" }{
"userId": "user_123",
"period": "2024-01",
"usage": {
"api_requests": { "used": 45000, "limit": 100000, "remaining": 55000 }{ "used": 12.5, "limit": 50, "remaining": 37.5 }{
const usage = await getUsage(user, quotaType);
if (usage.used >= usage.limit) {
throw new QuotaExceededError({
quotaType,
limit: usage.limit,
upgradeUrl: '/pricing'
}