What You Can Build with Python Web APIs

A web API is the program on a server that apps ask for data, and this lesson shows what people build with Python web APIs, what a web framework does, why this course uses FastAPI, and the five APIs you'll build.

Novice · FastAPI · REST API · Updated

By

After this lesson you can

  • Name things people build with Python web APIs, such as a phone app's back end or a store's checkout, and say what an API does for the app
  • Explain what a web framework does for an API, and why this course uses FastAPI rather than Django or Flask

Key takeaways

  • A web API is a program on a server that other programs ask for data or actions. It answers in JSON, and it's the back end of the app that asks.
  • People build the back ends of phone apps, stores' checkouts, club sign-up forms and data models with Python web APIs.
  • A web framework does the work every API repeats, such as waiting for requests and sending JSON, so you write only the functions that answer.
  • FastAPI is a Python framework built for APIs. It reads your type hints to check each request and makes a docs page by itself. Django and Flask are the other well-known ones.
  • In this course you build five APIs, one for each module, the last one online at a link, and then an API of your own.

Maple Street Bakery's owner wants this morning's specials to show up in the bakery's phone app: sourdough at $6.50, a dozen bagels at $12.00, and something new tomorrow. The app on a customer's phone can't know what came out of the oven an hour ago. It has to ask, over the internet, and something on the bakery's side has to answer. That something is a web API, and building one in Python is what this course is about.

This lesson shows what web APIs do, what people build with them, why this course uses FastAPI, and the projects you'll build.

Who answers when an app asks?

Think of ordering at a bakery counter. You don't walk into the kitchen: you ask at the counter, and someone brings back what you asked for.

An API (short for application programming interface) is that counter for programs: a program that other programs ask for data or actions. A web API is one they ask over the internet. The bakery's app sends a request to an address such as /specials, and the API answers with data in JSON, a text format nearly every app can read:

[{"name":"Sourdough loaf","price":6.5},{"name":"A dozen bagels","price":12.0}]

The app shows it as a tidy list. (JSON writes 6.50 as 6.5; the app adds the zero back.)

The part of an app that runs on a server (a computer that's always on and connected), keeping its data and its rules, is its back end. The screens people tap are its front end. A web API is how the two talk, and this course is about building back ends.

Things people build with Python web APIs

What An example Who asks it
The back end of a phone app The bakery's specials, your saved orders The app on each customer's phone
A store's checkout Checking a jacket is in stock, totaling the cart, saving the order The store's website and app
A club's sign-up form Saving a sign-up, and saying no when the event is full The sign-up page in the browser
A data model behind an address Predicting tomorrow's bread sales from the weather forecast The bakery's ordering program

A data model is a program that learned a pattern from past data, such as last year's sales; most are written in Python. At work, back-end and full-stack developers build these APIs, and data scientists put their models behind one.

Why FastAPI?

Every API repeats the same chores: wait for requests, work out which code answers each address, and turn Python's dictionaries and lists into JSON. A web framework does those chores for you. Think of a food truck that comes with its kitchen already built: you bring the recipes. You write the functions that answer, and the framework does the rest.

Python has three well-known web frameworks:

  • Django builds whole websites, with pages, an admin area and a database built in.
  • Flask is small and flexible, for websites and APIs.
  • FastAPI is built for APIs. It reads the type hints you write, such as price: float, to check every request, and it makes an interactive docs page for your API by itself.

This course uses FastAPI because an API is all it builds, and its docs page lets you see your API working from the first route. Here's a whole FastAPI app for the bakery:

from fastapi import FastAPI

app = FastAPI()


@app.get("/specials")
def list_specials():
    return [
        {"name": "Sourdough loaf", "price": 6.50},
        {"name": "A dozen bagels", "price": 12.00},
    ]

Read it as: when a request asks for /specials, run list_specials and send back what it returns. You'll build it in Your First FastAPI App.

What you'll build in this course

Each module ends with a project of its own, called a mini-capstone, that puts the whole module to work.

  • Book Club API (Module 1). A book club's API: list its books, find one, add one, and see the book the club is reading now.

    A book club's API answering three requests: a list of the club's books, one book found by its id, and the book the club is reading now

  • Event Sign-Up API (Module 2). Event sign-ups that politely refuse bad input, with a test for every rule.

    An event sign-up API accepting a sign-up for two seats and politely refusing one whose email isn't an email, each answer checked by a passing test

  • Book Club API on SQLite (Module 3). The book club again, with its books kept in a database, so they're still there after a restart.

    The book club's API restarted, with its books still there because they're kept in a database file

  • Room Bookings API (Module 4). Members sign in to book a room, and only staff can add one.

    Room bookings: a signed-in member books a room, and only a staff member can add a new room

  • Tool Library API (Module 5). A neighborhood tool library's API, online at a link, with its tests run on every push.

    A tool library's API online at a link, with a green badge that shows its tests passed on the last push

  • Your own API (the final capstone). An API you design, such as a habit tracker or a recipe box, online with tests and a README.

    Three ideas for an API of your own, a habit tracker, a recipe box and a club's events, each online at a link with its tests passing

Check yourself

2 questions to try on the lesson page.

So what

Behind nearly every app you use is an API answering its questions, and with Python and FastAPI you'll build five of them in this course, the last one online for anyone to use.

Key terms