0% complete

Files, data formats, and web APIs · Lesson 3 of 3

HTTP requests and API clients

Call external APIs with timeouts, status checks, and responsible credential handling.

Lesson

1. Concept and mental model

A web API lets programs exchange data over HTTP. A client sends a request to a documented endpoint and interprets its response. When using an HTTP library, set a finite timeout: a request without one can wait indefinitely and tie up a worker. Check the status code before trusting a response body, because an error page may not match the expected JSON structure. Treat timeouts, connection failures, rate limits, and server errors as distinct cases where retry policies differ.

2. How it works

Store API credentials in a secret manager or protected environment configuration, not in source code, tutorial examples, logs, or URLs. Do not send credentials to a host outside the provider’s documented API domain. Validate responses because a service may return missing fields or a changed schema. For retries, use bounded backoff and respect Retry-After; avoid retrying non-idempotent operations unless the API supports idempotency.

3. Use it safely and effectively

Keep network code behind a small function or client class so it can be tested with mocked responses. Do not call live services in every unit test. Record only safe metadata such as request duration and status, never authorization headers or private payloads. Practice by fetching a public JSON endpoint with a timeout, checking the status, and handling a non-JSON or unavailable response.

4. Syntax reference

import requests

response = requests.get('https://api.example.com/status', timeout=10)
response.raise_for_status()
status = response.json()
print(status['service'])

5. Guided walkthrough

Set a timeout for every network call, check the HTTP status before trusting the body, and validate the decoded response. Keep authentication and URL construction in one client so callers do not duplicate security and error-handling rules.

6. Common mistakes to avoid

  • Making a request without a timeout, which can leave a worker waiting indefinitely.
  • Assuming a successful JSON parse means the server returned the expected resource or schema.

Visual guide

Reliable API call

  1. Step 01 Request

    Send a documented request with a timeout.

  2. Step 02 Check

    Validate status and response shape.

  3. Step 03 Handle

    Report or retry transient errors safely.

Code examples

Worked example
PYTHON
import requests

response = requests.get('https://api.example.com/status', timeout=10)
response.raise_for_status()
status = response.json()
print(status['service'])
Expected output
The service name returned by the documented API.
Call a public endpoint with a timeout
PYTHON
import requests

response = requests.get('https://api.example.com/status', timeout=10)
response.raise_for_status()
data = response.json()
print(data['status'])
Expected output
The status field from the validated endpoint response.
Build query parameters safely
PYTHON
import requests

response = requests.get(
    'https://api.example.com/search',
    params={'q': 'python automation', 'limit': 5},
    timeout=10,
)
print(response.url)
Expected output
A URL with correctly encoded query parameters.

Key points

  • Call external APIs with timeouts, status checks, and responsible credential handling.
  • Prefer explicit, testable behavior over hidden side effects.
  • Validate assumptions at system boundaries and handle failure deliberately.

Check your understanding

Practice

Why should an HTTP request have a finite timeout?

Your learning progress

Finish this lesson