APIs

12. APIs#

APIs let your code communicate with web services, data sources, and cloud tools.

  • REST APIs and HTTP methods

  • requests.get() and response objects

  • JSON 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:

  1. Explain what an API is and how REST APIs communicate over HTTP

  2. Make GET requests with requests, pass query parameters, and convert between JSON text and Python objects with response.json(), json.loads(), and json.dumps()

  3. Handle HTTP errors, timeouts, and invalid JSON without crashing your program

  4. Send POST requests with JSON bodies and authenticate with headers and API keys read from environment variables

  5. Parse nested JSON defensively and validate a response against the keys your code expects

  6. 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, &, and =, with % codes so values cannot break the query string.

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 json.dumps(); json.loads() parses JSON text back into Python objects.

16

Response body

The data returned by a server after a request.

17

requests

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 os.environ.get(), often used to keep secrets out of source code.

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 Authorization: Bearer <token>, granting access to whoever presents it.

26

Prepared request

A request that requests has fully built, including its final URL and headers, but has not yet sent.

27

Cursor-based pagination

A pagination scheme in which each response returns a token, such as next_cursor, that names the next page to request.

28

Jitter

A small random amount added to a retry delay so that many clients do not retry at exactly the same time.

29

RequestException

The base class of the requests exceptions, so except requests.RequestException catches any request failure.

Chapter Overview Slides