← Back to all snippets
PYTHON

Python API Client with Exponential Backoff Retries

Create a robust Python API client that automatically retries failed HTTP requests with exponential backoff, ideal for handling transient network issues and API rate limits.

import requests
import time
from requests.exceptions import RequestException

def call_api_with_retries(url, method='GET', data=None, headers=None, max_retries=3, backoff_factor=0.5):
    """
    Calls an API endpoint with retry logic for transient errors.

    Args:
        url (str): The API endpoint URL.
        method (str): HTTP method (e.g., 'GET', 'POST', 'PUT', 'DELETE').
        data (dict, optional): JSON data for POST/PUT requests.
        headers (dict, optional): Custom headers for the request.
        max_retries (int): Maximum number of retries.
        backoff_factor (float): Factor to calculate sleep duration (sleep_time = backoff_factor * (2 ** (retry_attempt - 1))).

    Returns:
        requests.Response: The successful response object.

    Raises:
        RequestException: If the request fails after all retries.
    """
    for retry_attempt in range(1, max_retries + 1):
        try:
            print(f"Attempt {retry_attempt} for {url}")
            if method.upper() == 'GET':
                response = requests.get(url, headers=headers)
            elif method.upper() == 'POST':
                response = requests.post(url, json=data, headers=headers)
            # Add other methods as needed (PUT, DELETE, etc.)
            else:
                raise ValueError(f"Unsupported HTTP method: {method}")

            response.raise_for_status() # Raises HTTPError for bad responses (4xx or 5xx)
            return response
        except RequestException as e:
            print(f"Request failed (attempt {retry_attempt}/{max_retries}): {e}")
            if retry_attempt < max_retries:
                sleep_time = backoff_factor * (2 ** (retry_attempt - 1))
                print(f"Retrying in {sleep_time:.2f} seconds...")
                time.sleep(sleep_time)
            else:
                print("Max retries reached. Failing request.")
                raise

# Example Usage:
# try:
#     # Simulating a flaky API (e.g., a service that might sometimes return 500)
#     # For actual testing, you might use a mock server or a service like httpstat.us
#     response = call_api_with_retries('https://jsonplaceholder.typicode.com/posts/1', max_retries=5)
#     # response = call_api_with_retries('http://httpstat.us/503', max_retries=5) # Uncomment to test failure
#     print("API call successful!")
#     print(response.json())
# except RequestException as e:
#     print(f"Final API call failed: {e}")
How it works: This Python snippet provides a function to make API calls with built-in retry logic using exponential backoff. It leverages the `requests` library to handle HTTP requests. When a `RequestException` occurs (e.g., network error, DNS failure, or non-2xx HTTP status codes), the function waits for an increasing duration before retrying the request, up to a specified maximum number of attempts. This significantly enhances the resilience of API integrations against transient network issues or temporary service unavailability.

Need help integrating this into your project?

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

Hire DigitalCodeLabs