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.

關於作者

SwiftProxy
Linh Tran
Swiftproxy高級技術分析師
Linh Tran是一位駐香港的技術作家,擁有計算機科學背景和超過八年的數字基礎設施領域經驗。在Swiftproxy,她專注於讓複雜的代理技術變得易於理解,為企業提供清晰、可操作的見解,助力他們在快速發展的亞洲及其他地區數據領域中導航。
Swiftproxy部落格提供的內容僅供參考,不提供任何形式的保證。Swiftproxy不保證所含資訊的準確性、完整性或合法合規性,也不對部落格中引用的第三方網站內容承擔任何責任。讀者在進行任何網頁抓取或自動化資料蒐集活動之前,強烈建議諮詢合格的法律顧問,並仔細閱讀目標網站的服務條款。在某些情況下,可能需要明確授權或抓取許可。
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email