12. APIs#
APIs let your code communicate with web services, data sources, and cloud tools.
REST APIs and HTTP methods
requests.get()and response objectsJSON data as Python dictionaries and lists
Query parameters and headers
Reliability patterns for errors, timeouts, pagination, and retries
Chapter Video
This overview introduces HTTP requests in Python, including reading JSON data from web responses.
Python Requests Tutorial: Request Web Pages, Download Images, POST Data, Read JSON, and More (25:00) by Corey Schafer
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Explain what an API is and how REST APIs communicate over HTTP
Make GET requests with
requests, pass query parameters, and convert between JSON text and Python objects withresponse.json(),json.loads(), andjson.dumps()Handle HTTP errors, timeouts, and invalid JSON without crashing your program
Send POST requests with JSON bodies and authenticate with headers and API keys read from environment variables
Parse nested JSON defensively and validate a response against the keys your code expects
Collect paginated results and retry temporary failures with exponential backoff
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
API |
An application programming interface, a defined way for software systems to communicate. |
2 |
Web API |
An API accessed over the web, usually with HTTP requests. |
3 |
HTTP |
The protocol commonly used for web requests and responses. |
4 |
REST |
An API design style that uses resources, URLs, and standard HTTP methods. |
5 |
Endpoint |
A URL where a specific API resource or action is available. |
6 |
HTTP method |
A request action such as GET, POST, PUT, PATCH, or DELETE. |
7 |
Status code |
A numeric response code that summarizes whether an HTTP request succeeded or failed. |
8 |
GET request |
An HTTP request used to retrieve data. |
9 |
POST request |
An HTTP request commonly used to send new data to a server. |
10 |
Query parameter |
A key-value option added to a URL to customize a request. |
11 |
URL encoding |
Replacing special characters in a URL, such as spaces, |
12 |
Header |
Metadata sent with an HTTP request or response. |
13 |
API key |
A credential used to identify or authorize an API client. |
14 |
JSON |
A text data format commonly used for API request and response bodies. |
15 |
JSON serialization |
Converting Python objects to JSON text with |
16 |
Response body |
The data returned by a server after a request. |
17 |
|
A popular Python library for making HTTP requests. |
18 |
Timeout |
A limit on how long a client waits for a response. |
19 |
Pagination |
Splitting a large API response into multiple pages or batches. |
20 |
Exponential backoff |
A retry strategy that increases wait time after repeated transient failures. |
21 |
Retry |
Trying a failed request again, often after a short delay. |
22 |
Response contract |
The expected structure and meaning of data returned by an API. |
23 |
Environment variable |
A named value set outside the program, read in Python with |
24 |
Authorization header |
The HTTP header that carries a client’s credentials, such as an API token, with each request. |
25 |
Bearer token |
An access token sent as |
26 |
Prepared request |
A request that |
27 |
Cursor-based pagination |
A pagination scheme in which each response returns a token, such as |
28 |
Jitter |
A small random amount added to a retry delay so that many clients do not retry at exactly the same time. |
29 |
|
The base class of the |