Command Palette

Search for a command to run...

Hectal
Phase 15Intermediate16 of 18 in Python

Python for APIs, Services and Tools

Call web APIs safely, validate data with Pydantic, build a FastAPI service, use databases, write command-line tools, automate work and ship it in a container.

Most real Python work is not a single script. It talks to other programs over the network, reads secrets from its environment, checks the data it receives, stores results in a database, and runs as a service or a command-line tool. This phase teaches those everyday jobs, one at a time.

A thread runs through it. Every large language model API is an ordinary web API that takes JSON and returns JSON. The timeouts, retries, rate-limit handling, API-key hygiene and Pydantic models you learn here are exactly what Phase 16 uses to call models.

Your browser has no real network, so the live examples here use a fake server that is a plain Python function (httpx.MockTransport). You still see real request and response objects, status codes, headers and errors. Code that needs a real network, a web server or a subprocess is shown as a normal code block, with output from a real run.

0/10 · 0%
10 topics ~5 h 37 code blocks & diagrams
Start with the first topic
1
15.1

Calling Web APIs: HTTP with httpx and requests

A web API is a program on another computer that answers HTTP requests, usually with JSON. With httpx or requests you send a request (a method, a URL, headers, maybe a body) and read back a response (a status code, headers, a body); you must always set a timeout and check the status.

30 min 1 diagram 3 code practice

2
15.2

Robust API Clients: Retries, Backoff and Rate Limits

Networks and servers fail in short bursts, so a good client retries only the failures that can pass (timeouts, 429, 5xx), waits longer after each try with random jitter, honours the server's Retry-After header, and never repeats a request that is not safe to repeat.

29 min 1 diagram 2 code practice

3
15.3

Configuration, Environment Variables and Secrets

Settings that change between your laptop, a test server and production (and secrets such as API keys) should live outside the code, in environment variables, and be read once into a typed settings object. A secret that is committed to Git must be treated as stolen.

28 min 3 code practice

4
15.4

Data Validation with Pydantic

Pydantic turns type hints into real checks: you describe the shape of your data as a class, and it converts and validates incoming dictionaries or JSON, collects clear errors, and can produce a JSON Schema. FastAPI, the AI SDKs and agent frameworks are built on it.

27 min 3 code practice

5
15.5

Building an API with FastAPI

FastAPI is a Python framework for web APIs: you write ordinary functions with type hints, and it turns them into HTTP endpoints, validates path, query and body data with Pydantic, shapes the response, and generates interactive documentation. You run it with the uvicorn server.

29 min 1 diagram 3 code practice

6
15.6

Databases from Python: sqlite3 and Beyond

Python can store data in a database through a driver. The built-in sqlite3 module needs no server; whatever the database, always pass values to SQL as parameters (placeholders), never by pasting text, and group changes in transactions so they succeed or fail together.

28 min 3 code practice

7
15.7

Command-Line Tools: argparse and Typer

A command-line tool reads its settings from the words typed after its name. Python's argparse module turns sys.argv into a clean object, builds --help for free, rejects bad input with a usage message, and lets the program report success or failure through its exit code.

26 min 4 code practice

8
15.8

Automation Scripts: Files, Dates and Subprocesses

Automation scripts do boring work reliably: they rename and move files with pathlib, handle dates and time zones correctly with datetime and zoneinfo, and run other programs with subprocess.run using a list of arguments, a timeout and a return-code check, never a shell built from user input.

28 min 3 code practice

9
15.9

Shipping Python: Docker and Deployment

To ship a Python service, you package the code and its exact dependencies into a container image, run it as a non-root user with its settings from environment variables, give it a health check, and let a platform start, restart and scale it.

27 min 4 code practice

10
15.10

Collecting Web Data Responsibly

Web scraping means fetching web pages with a program and pulling data out of them. Use an official API when one exists, read the site's robots.txt and terms, identify your program, go slowly, cache what you fetch, handle personal data with care, and parse with a real HTML parser.

37 min 6 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.