PYTHON

Implement Outgoing API Request Rate Limiting

Prevent exceeding external API rate limits by implementing a robust rate-limiting mechanism for outgoing requests from your server-side application using Python.

import requests
import time
from ratelimit import limits, sleep_and_retry # Requires: pip install ratelimit

# --- Configuration ---
# Define the rate limit: 5 calls per 1 second
CALLS = 5
PERIOD = 1 # seconds

# External API URL
EXTERNAL_API_URL = 'https://jsonplaceholder.typicode.com/todos/1' # Example API

@sleep_and_retry # Ensures the function waits if the rate limit is hit
@limits(calls=CALLS, period=PERIOD)
def make_api_request(url, headers=None):
    """Makes a rate-limited API request."""
    print(f"Attempting to call {url} at {time.time()}")
    try:
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status() # Raise an exception for bad status codes
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"API request failed: {e}")
        return None

def fetch_multiple_items(num_items):
    """Fetches multiple items from the API with rate limiting."""
    results = []
    for i in range(num_items):
        # Simulate fetching different items, or just repeat the same one
        item_url = f'https://jsonplaceholder.typicode.com/todos/{i + 1}'
        data = make_api_request(item_url)
        if data:
            results.append(data)
        time.sleep(0.1) # Simulate some work between requests
    return results

if __name__ == '__main__':
    print(f"Starting to fetch {CALLS * 2} items with a rate limit of {CALLS} calls per {PERIOD} second(s)...")
    # This will demonstrate the rate limiting in action, pausing every 'PERIOD' seconds
    fetched_data = fetch_multiple_items(CALLS * 2) # Try to fetch more than allowed in one period
    print(f"Finished fetching. Total items: {len(fetched_data)}")
    print("Example data point:", fetched_data[0] if fetched_data else "No data")
How it works: This Python snippet demonstrates how to implement outgoing API request rate limiting using the `ratelimit` library. By decorating the `make_api_request` function with `@limits` and `@sleep_and_retry`, the code ensures that calls to external APIs do not exceed a specified rate (e.g., 5 calls per 1 second). If the rate limit is about to be exceeded, `sleep_and_retry` automatically pauses execution until the next allowed period, preventing your application from getting blocked or receiving errors from external services due to excessive requests.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs