API Bouncer

Buy me a coffee

Understanding API Rate Limits and How to Handle Them

You're building an app, making API calls, everything works great — then suddenly you get a 429 Too Many Requests error. You've hit a rate limit. Here's what that means and how to handle it.

What are rate limits?

Rate limits restrict how many API requests you can make within a given time period. They're set by the API provider and apply per API key (or per IP address for no-auth APIs).

Common rate limit formats:

Why rate limits exist

API servers have finite resources. Without rate limits:

Rate limits protect the API for all users. They're not a punishment — they're a necessity.

How to detect rate limits

When you exceed a rate limit, the API returns a 429 status code ("Too Many Requests"). Many APIs also include helpful headers in every response:

X-RateLimit-Limit: 60 // Max requests per window X-RateLimit-Remaining: 12 // Requests left in current window X-RateLimit-Reset: 1709251200 // Unix timestamp when window resets Retry-After: 30 // Seconds to wait before retrying

Always check these headers if they're available. They tell you exactly how much room you have and when you can try again.

Strategies for handling rate limits

1. Cache responses

The simplest way to reduce API calls is to not make unnecessary ones. If the data doesn't change every second, cache the response and reuse it.

const cache = new Map(); const CACHE_TTL = 5 * 60 * 1000; // 5 minutes async function fetchWithCache(url) { const cached = cache.get(url); if (cached && Date.now() - cached.time < CACHE_TTL) { return cached.data; } const response = await fetch(url); const data = await response.json(); cache.set(url, { data, time: Date.now() }); return data; }

2. Implement exponential backoff

When you get a 429, don't immediately retry. Wait, then try again. If it fails again, wait longer. This is called exponential backoff.

async function fetchWithRetry(url, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { const response = await fetch(url); if (response.status === 429) { const waitTime = Math.pow(2, i) * 1000; // 1s, 2s, 4s await new Promise(r => setTimeout(r, waitTime)); continue; } return response.json(); } throw new Error('Rate limit exceeded after retries'); }

3. Throttle your requests

If you need to make many requests (e.g., fetching data for 100 items), don't fire them all at once. Space them out.

async function throttledFetch(urls, delayMs = 200) { const results = []; for (const url of urls) { const data = await fetch(url).then(r => r.json()); results.push(data); await new Promise(r => setTimeout(r, delayMs)); } return results; }

4. Use webhooks instead of polling

If the API supports webhooks (push notifications), use them instead of repeatedly polling for updates. Instead of checking "did anything change?" every minute, the API tells you when something changes.

5. Batch requests when possible

Some APIs let you request multiple items in a single call. Instead of 10 separate requests, you make one request with 10 IDs. Always check the documentation for batch endpoints.

What to do when you're truly limited

If you consistently need more requests than the free tier allows, you have a few options:

Common rate limits by API type

As a rough guide, here's what to expect from free tiers:

Rate limits vary widely. Always check the specific API's documentation before you start building.