How to Handle API Authentication and Errors in Python

APIs are the backbone of modern applications, from powering live market feeds to enabling complex automation tasks. But tapping into their power isn't as simple as making a request and expecting a result. There are challenges to navigate – like authentication, error handling, and rate limits. Here's what you need to know to not just use APIs, but master them.

SwiftProxy
By - Linh Tran
2026-02-03 16:52:34

How to Handle API Authentication and Errors in Python

What Are APIs and How They Work

APIs (Application Programming Interfaces) allow software to communicate. They're the bridges that connect your Python script with remote services, pulling in data, submitting actions, and triggering automated workflows. Behind the scenes, APIs rely on HTTP – the standard protocol used by most APIs. Your interactions with an API typically involve sending HTTP requests and processing the response.

The four core HTTP methods you'll use are

  • GET: Request data.
  • POST: Submit data to the server.
  • PUT/PATCH: Update resources.
  • DELETE: Remove resources.

Every API exposes specific endpoints, like /users or /products. To send a request, you'll use Python's requests library, a flexible and easy-to-use tool. Here's how you can make a basic GET request:

import requests

response = requests.get("https://example.com/data")
print(response.status_code)  # Status code
print(response.json())  # Parsed JSON

But there's more than just pulling in data. You'll need to consider authentication, error handling, rate limits, and sometimes – proxies. Let's dive in.

Building Your API Call in Python

Here's how you can use Python to make an API call. First, ensure you have the requests library installed:

pip install requests

Now, let's pull data from a public API. We'll use JSONPlaceholder – a free fake API for testing and prototyping:

import requests

url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url)

print("Status code:", response.status_code)
print("Response JSON:", response.json())

This returns data in JSON format, which we can access and manipulate. For example:

data = response.json()
print("Title:", data['title'])

Handling With Authentication

In production environments, most APIs require authentication to verify that the requester has permission to access the data. There are several common methods:

API Keys

The most straightforward form of authentication, an API key is often included in request headers or as a query parameter:

import requests

url = "https://example.com/data"
headers = {
    "Authorization": "Bearer YOUR_API_KEY"
}

response = requests.get(url, headers=headers)
print(response.json())

Never hardcode your API keys in scripts. Instead, store them in environment variables:

import os

api_key = os.getenv("MY_API_KEY")

Basic Authentication

Used in older or internal systems, Basic Authentication requires a username and password:

from requests.auth import HTTPBasicAuth

response = requests.get(
    "https://example.com/secure-data",
    auth=HTTPBasicAuth("username", "password")
)

OAuth 2.0

OAuth is a more complex method, involving multiple steps to acquire an access token for user-based access, especially when dealing with platforms like Google or Twitter.

headers = {
    "Authorization": "Bearer ACCESS_TOKEN"
}
response = requests.get("https://platform.com/userinfo", headers=headers)

Handling Errors and Rate Limits

APIs are not foolproof. Servers might go down, requests could be malformed, or you might hit a rate limit. Here's how to handle these issues:

HTTP Status Codes

These are the server's way of telling you how things went. Some of the most common:

  • 200 OK: Everything's good.
  • 400 Bad Request: Something's wrong with your request.
  • 401 Unauthorized: Missing or invalid credentials.
  • 403 Forbidden: You're authenticated but lack permission.
  • 404 Not Found: The resource doesn't exist.
  • 429 Too Many Requests: You've exceeded the rate limit.
  • 500+ Server Errors: Usually not your fault.

Use Python to check the status code and react accordingly:

if response.status_code == 200:
    data = response.json()
else:
    print(f"Error {response.status_code}: {response.text}")

Retry Logic

Sometimes things fail temporarily. Rather than giving up, you can try again with a short delay:

import time

url = "https://example.com/data"
retries = 3

for i in range(retries):
    response = requests.get(url)
    if response.status_code == 200:
        print("Success!")
        break
    else:
        print(f"Attempt {i + 1} failed. Retrying...")
        time.sleep(2)

For more sophisticated retry strategies, consider using libraries like tenacity or urllib3's built-in retry adapter.

Using Proxies to Overcome Restrictions

Some APIs limit access based on your IP address. They may restrict requests per minute or only allow access from certain regions. To bypass these limits, proxies can be incredibly helpful.

A proxy routes your requests through a different server, hiding your original IP. This is useful for overcoming IP-based rate limits, geo-restricted APIs, or rotating identities in large-scale data scraping. Here's how you can use a proxy with the requests library:

proxies = {
    "http": "http://username:[email protected]:8000",
    "https": "http://username:[email protected]:8000"
}

response = requests.get("https://example.com/data", proxies=proxies)

For larger-scale operations, you may need to rotate proxies. You can manually rotate proxies like this:

import random

proxy_list = [
    "http://user:[email protected]:8000",
    "http://user:[email protected]:8000",
    "http://user:[email protected]:8000"
]

proxy = random.choice(proxy_list)

response = requests.get(
    "https://example.com/data",
    proxies={"http": proxy, "https": proxy}
)

Conclusion

By following these best practices, you can ensure efficient, secure, and reliable API interactions. Whether managing authentication, handling errors, or respecting rate limits, these strategies will help you optimize your API calls, safeguard your data, and improve the overall performance of your applications.

Note sur l'auteur

SwiftProxy
Linh Tran
Linh Tran est une rédactrice technique basée à Hong Kong, avec une formation en informatique et plus de huit ans d'expérience dans le domaine des infrastructures numériques. Chez Swiftproxy, elle se spécialise dans la simplification des technologies proxy complexes, offrant des analyses claires et exploitables aux entreprises naviguant dans le paysage des données en rapide évolution en Asie et au-delà.
Analyste technologique senior chez Swiftproxy
Le contenu fourni sur le blog Swiftproxy est destiné uniquement à des fins d'information et est présenté sans aucune garantie. Swiftproxy ne garantit pas l'exactitude, l'exhaustivité ou la conformité légale des informations contenues, ni n'assume de responsabilité pour le contenu des sites tiers référencés dans le blog. Avant d'engager toute activité de scraping web ou de collecte automatisée de données, il est fortement conseillé aux lecteurs de consulter un conseiller juridique qualifié et de revoir les conditions d'utilisation applicables du site cible. Dans certains cas, une autorisation explicite ou un permis de scraping peut être requis.
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email