0
0
Rest-apiDebug / FixBeginner · 4 min read

How to Handle 429 Too Many Requests Error in REST APIs

The 429 Too Many Requests error happens when your client sends too many requests to a server in a short time. To handle it, implement retry logic with delays and respect the server's Retry-After header to avoid overwhelming the server.
🔍

Why This Happens

The 429 Too Many Requests error occurs when a client sends more requests than the server allows in a given time frame. Servers use this to protect themselves from overload or abuse. For example, if you send many requests in a loop without waiting, the server will respond with 429 to tell you to slow down.

python
import requests

url = 'https://api.example.com/data'

for i in range(10):
    response = requests.get(url)
    print(f'Request {i+1}:', response.status_code)
Output
Request 1: 200 Request 2: 200 Request 3: 200 Request 4: 429 Request 5: 429 Request 6: 429 Request 7: 429 Request 8: 429 Request 9: 429 Request 10: 429
🔧

The Fix

To fix this, add logic to detect the 429 status code and wait before retrying. Use the Retry-After header if the server provides it, or wait a fixed time. This prevents flooding the server and respects its limits.

python
import requests
import time

url = 'https://api.example.com/data'

for i in range(10):
    response = requests.get(url)
    if response.status_code == 429:
        retry_after = int(response.headers.get('Retry-After', 5))
        print(f'Got 429, retrying after {retry_after} seconds')
        time.sleep(retry_after)
        response = requests.get(url)
    print(f'Request {i+1}:', response.status_code)
Output
Request 1: 200 Request 2: 200 Request 3: 200 Request 4: 429 Got 429, retrying after 5 seconds Request 4: 200 Request 5: 200 Request 6: 200 Request 7: 200 Request 8: 200 Request 9: 200 Request 10: 200
🛡️

Prevention

To avoid 429 errors in the future, follow these best practices:

  • Implement client-side rate limiting to control how often you send requests.
  • Respect the server's Retry-After header when present.
  • Use exponential backoff for retries to gradually increase wait times.
  • Cache responses when possible to reduce unnecessary requests.
  • Check API documentation for rate limits and design your app accordingly.
⚠️

Related Errors

Other errors related to request limits include:

  • 403 Forbidden: Sometimes used if you exceed limits permanently or violate policies.
  • 503 Service Unavailable: Server is overloaded or down, retry later.
  • 401 Unauthorized: If your API key is invalid or missing, causing request rejection.

Handling these properly also improves your app's reliability.

Key Takeaways

The 429 error means you are sending requests too fast and must slow down.
Use the Retry-After header to know how long to wait before retrying.
Implement retry logic with delays to handle 429 responses gracefully.
Prevent 429 by limiting request rate and caching responses when possible.
Check API docs for rate limits and design your client to respect them.