logo

Rate Limiting Best Practices

Learn how to handle rate limits effectively and optimize your API usage.

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:

PlanRequests/HourBurst Limit
Free1,00050/min
Pro10,000500/min
EnterpriseCustomCustom

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

  1. Monitor usage regularly through dashboard and API
  2. Implement backoff strategies for rate limit errors
  3. Cache responses when appropriate
  4. Use batch endpoints for multiple operations
  5. Configure webhooks instead of polling