API Rate Limiting Explained (and How to Handle It)
APIs limit how often you can call them. Here's why, how to spot when you've hit a limit, and how to stay under it.
Rate limiting is how an API caps how many requests you may send in a given window — per second, per minute, per day, or per month. It protects the service from overload and abuse, and on paid APIs it’s also how tiers are enforced. Hit the limit and you get HTTP 429.
How limits are expressed
- Fixed window: “1,000 requests per hour” measured on the clock — the counter resets at the top of the hour. Simple, but allows bursts at the boundary.
- Sliding window: the last 60 minutes, continuously. Smoother, harder to game.
- Token bucket: you have a bucket of N tokens that refills at a steady rate; each request spends one. Allows short bursts up to the bucket size, then throttles to the refill rate. This is the most common model for ‘per second’ limits.
Read the headers
Most APIs tell you where you stand on every response. Names vary, but the pattern is:
| X-RateLimit-Limit | Your ceiling for the current window |
| X-RateLimit-Remaining | How many requests you have left |
| X-RateLimit-Reset | When the window resets — Unix seconds, or seconds-from-now |
| Retry-After | On a 429/503: how many seconds to wait before trying again |
Handle 429 properly: exponential backoff with jitter
import time, random, requests
def get(url, **kw):
for attempt in range(5):
r = requests.get(url, timeout=10, **kw)
if r.status_code != 429:
r.raise_for_status()
return r
# prefer the server's hint, else back off exponentially
wait = float(r.headers.get('Retry-After', 2 ** attempt))
wait += random.uniform(0, 0.5) # jitter avoids thundering herd
time.sleep(wait)
raise RuntimeError('rate limit: gave up after 5 attempts')The jitter matters: if every client retries after exactly 2 seconds, they all hammer the API again at the same instant. A small random offset spreads them out.
Better: don’t hit the limit in the first place
- Cache. The cheapest request is the one you don’t send. Cache responses for as long as the data is meaningfully fresh — seconds for prices, hours for weather, days for reference data.
- Batch. Many APIs accept multiple ids per call (
?ids=1,2,3orname[]=a&name[]=b). One request for 50 items beats 50 requests. - Throttle client-side. Keep a token bucket or a simple ‘minimum ms between calls’ gate so you never even reach 429.
- Spread background work. Don’t refresh everything at midnight — stagger jobs across the window.
- Use webhooks where offered. Let the API push changes to you instead of polling for them — see webhooks.
Per-key, per-IP, or per-user?
Know which one applies. If the limit is per API key and you call a third-party API directly from every user’s browser with the same key, one busy user can exhaust the quota for everyone — proxy through your backend and apply your own fair-use limits. If it’s per IP, serverless functions sharing an egress IP can collide. Design for the dimension the API actually meters.
FAQ
What does HTTP 429 mean?
429 Too Many Requests means you’ve exceeded the API’s rate limit. Check the Retry-After header for how long to wait, then retry with backoff.
What is exponential backoff?
A retry strategy where the wait between attempts roughly doubles each time (1s, 2s, 4s, 8s…), usually with a little random jitter added and a cap on the number of attempts. It relieves a struggling API instead of hammering it.
How can I avoid rate limits entirely?
Cache responses, batch multiple items into one request, throttle your client so it never exceeds the documented rate, stagger background jobs, and use webhooks instead of polling where the API supports them.
Is the rate limit per API key or per IP address?
It depends on the API — check the docs. Per-key limits mean all traffic using that key shares the quota; per-IP limits can catch out serverless functions that share an egress IP.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ