# Up Next: Users and Security

Look back at what Module 3 taught you, see a project that puts all of it to work, a book club's API moved onto SQLite, and see the gap Module 4 closes, by giving an API accounts, sign-in and rules about who may do what.

Journeyman · FastAPI · REST API · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Describe what Module 4's accounts, sign-in and rules about who may do what will add to an API built in Module 3

## Key takeaways

- After Module 3 you can keep an API's data in SQLite with hand-written SQL in one data module, give each request a connection, change tables with migrations, and test on a throwaway database.
- A book club's API moved onto SQLite, with related tables, a join, paged lists, migrations and tests, uses everything this module taught.
- The API doesn't know who is calling it. Anyone who can reach it can read, change or delete anyone's data.
- Module 4 adds accounts with hashed passwords, sign-in with tokens, rules about who may do what, and protection against the attacks every public API meets.

At the start of this module, everything your API saved disappeared when it stopped. Now it keeps its data in a database, asks for it with SQL you wrote yourself, keeps that SQL in one tidy file, and proves it all with tests on a database made fresh for every test. That's the shape of most back ends you'll work on.

This lesson looks back at what you can do, shows you a project that puts it all together, and gives you a first look at Module 4, where the API learns who is calling it.

## What you can do now

After Module 3, you can:

- **explain what a database is** and why SQLite, a whole database in one file, is a good place to start;
- **write SQL from zero:** `CREATE TABLE`, `INSERT`, `SELECT` with `WHERE` and `ORDER BY`, `UPDATE` and `DELETE`, always with `?` placeholders that stop SQL injection;
- **relate tables** with foreign keys, bring them back together with joins, and count, group and page through the rows;
- **keep every query in one data module,** with functions that take a connection and give back plain values;
- **give every request its own connection** with a `get_db` dependency that commits, rolls back and closes;
- **create and change tables with migrations,** numbered scripts that each database runs once;
- **test against a real, throwaway database** in `tmp_path`, wired in with `app.dependency_overrides`;
- **read code that uses an ORM,** SQLAlchemy or SQLModel, when a project you join uses one.

## Put it all together: a book club's API on SQLite

Here's a project that uses all of it, one you can build on your own now. Page Turners Book Club has an API that keeps its books, members and meetings in memory. Move it onto SQLite:

- **tables** for books, members and meetings, where each meeting points at the book it discusses with a foreign key;
- **migrations** that create them, numbered and run when the app starts;
- **a data module** with every query, and a **`get_db` dependency** that gives each request its connection;
- **a member's reading list,** built with a join;
- **book lists one page at a time,** with the total count beside each page;
- **404s** for books, members and meetings that aren't there;
- **tests** on a throwaway database in `tmp_path`, one for every rule.

When it's done, you have an API whose data is still there after a restart, with all its SQL in one file you can show a friend.

## Where it falls short

Rep Counter's API now keeps every workout safely. Here it saves Luis's workout, then Priya's with one set. Then a request deletes Priya's workout, and nothing asks who sent it. Each answer below is a request, its status line and the body that came back:

```text
DELETE /workouts/2
HTTP/1.1 204 No Content

GET /workouts/2
HTTP/1.1 404 Not Found
{"status":404,"title":"Not Found","detail":"There's no workout 2."}
```

That request could have come from Priya, from Luis, or from a stranger who found the API's address. The API can't tell them apart: it has no idea who is calling. The same goes for every route: anyone can read every member's workouts, and add sets to someone else's.

## What Module 4 unlocks

Module 4 teaches an API to know who's calling, and what they may do:

- **Accounts with passwords stored safely:** never the password itself, only a *hash* of it, made with pwdlib.
- **Signing in:** a sign-in route that checks the password and hands back a *token*, a signed pass the app sends with every later request, made with PyJWT; and sessions with cookies, for when they fit better.
- **Who may do what:** routes that need a signed-in member, members who change only their own data, and staff who can do more, with 401 and 403 answers when someone may not.
- **Hardening:** which web pages may call the API, limits on how fast someone can try passwords, secrets kept out of the code, and the attacks every public API should expect.

## Check yourself

2 questions to try on the lesson page.

## So what

Module 3 taught your API to remember everything it's told; Module 4 teaches it to ask who's telling it.

## Key terms

- [Authentication](https://www.ask-codey.com/glossary/authentication)
- [Authorization](https://www.ask-codey.com/glossary/authorization)
- [Hashing](https://www.ask-codey.com/glossary/hashing)
- [JWT](https://www.ask-codey.com/glossary/jwt)

## Related

- [Exercise: Workout Log API](https://www.ask-codey.com/lessons/exercise-fastapi-workout-log-api)
- [Testing with a Real, Throwaway Database](https://www.ask-codey.com/lessons/fastapi-data-testing-real-database)
- [Keeping the Data Code in One Place](https://www.ask-codey.com/lessons/fastapi-data-module)

Source: https://www.ask-codey.com/lessons/fastapi-up-next-users-and-security
