Rate Limiting Best Practices
Understanding and properly handling rate limits is crucial for building robust applications with API Cheap.
Understanding Rate Limits
Rate limits prevent abuse and ensure fair usage across all users:
| Plan | Requests/Hour | Burst Limit |
|---|---|---|
| Free | 1,000 | 50/min |
| Pro | 10,000 | 500/min |
| Enterprise | Custom | Custom |
Rate Limit Headers
Every response includes rate limit information:
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 847
X-RateLimit-Reset: 1640995200
X-RateLimit-Retry-After: 3600
Handling Rate Limits
1. Exponential Backoff
Implement exponential backoff when you hit rate limits:
async function makeAPICall(url, options, maxRetries = 3) {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
const response = await fetch(url, options);
if (response.status === 429) {
if (attempt === maxRetries) {
throw new Error('Rate limit exceeded, max retries reached');
}
const retryAfter = response.headers.get('X-RateLimit-Retry-After');
const delay = retryAfter ? parseInt(retryAfter) * 1000 : Math.pow(2, attempt) * 1000;
console.log(`Rate limited, retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
return response;
} catch (error) {
if (attempt === maxRetries) throw error;
}
}
}
Best Practices Summary
- Monitor usage regularly through dashboard and API
- Implement backoff strategies for rate limit errors
- Cache responses when appropriate
- Use batch endpoints for multiple operations
- Configure webhooks instead of polling