An API request asks another program or service for data over HTTP.
Many APIs return JSON, so API code often combines a request step with json parsing.
Keep beginner API programs small: build the URL, send the request, decode JSON, then validate the fields you need.
Python can call APIs with standard library modules, and many real projects later use third-party libraries for convenience.
import json
response_text = '{"name": "Maya", "skills": ["Python", "SQL"]}'
data = json.loads(response_text)
print(data["name"])
print(data["skills"][0])
Maya
Python
This example focuses on the JSON parsing step used after a successful API response.
External data can miss fields or return empty values, so API code should validate before assuming a shape.
user = {"id": 7, "name": "Maya"}
if "email" not in user:
print("email field missing")
else:
print(user["email"])
email field missing
The check prevents a KeyError and gives a clearer message.
Every network call needs a timeout; otherwise a stalled connection can block a worker indefinitely. Separate transport failures, HTTP error responses, and invalid response data because they require different logs and retry decisions. A successful TCP connection does not mean the server returned a successful status or valid JSON.
Retry only operations that are safe to repeat, and use bounded exponential backoff for transient failures such as rate limits or temporary server errors. Do not retry authentication failures, malformed requests, or validation errors without changing the request.
import requests
try:
response = requests.get(
"https://api.example.com/items",
params={"limit": 20},
timeout=(3.05, 10),
)
response.raise_for_status()
payload = response.json()
except requests.Timeout:
print("The API timed out")
except requests.HTTPError as error:
print(f"HTTP failure: {error.response.status_code}")
except requests.RequestException as error:
print(f"Network failure: {error}")
except ValueError:
print("The response was not valid JSON")
else:
print(payload)
The handlers distinguish timeout, HTTP, general transport, and JSON-decoding failures without hiding unexpected programming errors.
Read credentials from a secret store or environment variable and send them only to the intended HTTPS origin. Reuse a Session for shared headers and connection pooling, but do not mutate one session concurrently without a clear synchronization design.
Pagination may use page numbers, offsets, cursors, or Link headers. Follow the API contract, stop when the server says there is no next page, and enforce a local item or page limit. Observe Retry-After and documented rate-limit headers instead of guessing a delay.
0 of 2 checked
Try this next
0 of 2 completed
Python has standard library tools for HTTP, but many real projects use a third-party package for a simpler interface. Learn the request flow first.
JSON maps naturally to dictionaries, lists, strings, numbers, booleans, and null-like values, so it is easy for many languages to exchange.
Using a field before checking whether the response actually contains it.
Explore 500+ free tutorials across 20+ languages and frameworks.