Skip to content
← Inforjota
Cursos Inforjota Português

Lesson 7 of 7 · 15 min · Checked on 6 October 2026

Project: an API with database, tests and deploy

By the end of this lesson you will be able to:

  • Build with an agent an API with three endpoints and a SQLite database
  • Ask for the tests before the code and review every step before accepting it
  • Publish the API on an internet service and explain why the data can be lost

Mrs Graça has a grocery shop in Velas, on the island of São Jorge. Her nephew is building the shop's web page and wants to fetch the list of products from one place, always up to date. He needs a counter where you ask “what products are there?”, “add this one” and “show me number 3”. That counter is an API.

In this project, you build that counter with the agent, step by step, using everything you learned in this level.

What you will build

Three endpoints (produtos means “products”):

  • GET /produtos: the list of products;
  • POST /produtos: create a product, with a name and a price;
  • GET /produtos/{id}: see one product.

We use FastAPI, a Python library that creates, by itself, a page where you try the API in the browser, and SQLite, because it keeps everything in a single file and already comes with Python: there is no database server to install.

Each step is a request to the agent and a check by you. After each step that changes files, make a commit (lesson 1/05).

Step 0: prepare the folder

Inside experiencias (“experiments”):

mkdir loja-api
cd loja-api
git init
python3 -m venv .venv
source .venv/bin/activate
pip install "fastapi[standard]" pytest

(loja means “shop”.) On Windows, the activation command is .venv\Scripts\Activate.ps1 (lesson 2/01). Create the .gitignore with .venv/ and *.db (lesson 1/06).

Step 1: the plan

I want an API in FastAPI, in the file main.py, with SQLite (Python's sqlite3 module), in a file called loja.db. Endpoints: GET /produtos, POST /produtos (fields nome and preco, that is, name and price; responds 201) and GET /produtos/{id} (404 if it does not exist). Before writing code, propose a plan of at most 5 steps, each with its done criterion. Do not change any files.

Before accepting: does the plan contain only what you asked for? If login or photos show up, cut them.

Step 2: the tests first

Write only the file test_main.py, with FastAPI's TestClient. The tests use a temporary database, never loja.db. Cases: the list starts empty; creating a product returns 201 and an id; that id shows the product that was created; an id that does not exist gives 404. Do not write main.py.

One of the tests will look like this. The cliente (“client”) is set up at the top of the file, already with the temporary database:

def test_criar_e_ver(cliente):
    r = cliente.post("/produtos", json={"nome": "Queijo curado", "preco": 12.5})
    assert r.status_code == 201
    pid = r.json()["id"]
    assert cliente.get(f"/produtos/{pid}").json()["nome"] == "Queijo curado"

(Queijo curado means “aged cheese”.)

Before accepting: run python3 -m pytest and watch it fail, because there is no main.py yet. Read the tests: do they say what you want?

Step 3: the code

Now write main.py so that the tests pass. Create the table if it does not exist. Do not change test_main.py. Run the tests and show me the result.

Before accepting: in the diff, test_main.py has not changed. Run python3 -m pytest yourself. Then run fastapi dev, which finds main.py on its own, open http://127.0.0.1:8000/docs, create a product in POST /produtos with Try it out and check that it appears in GET /produtos.

Tip

Do this step on a branch (lesson 2/06): git switch -c codigo (codigo means “code”). When everything passes, go back to the main one and run git merge codigo.

Step 4: README and dependencies

Create requirements.txt with fastapi[standard] and pytest, and a short README.md: what the API does, how to install it, how to run the tests and how to start it.

Before accepting: follow the README yourself, in a new terminal. If a command fails, the README is wrong.

Step 5: publish

This is how it looks on 6 October 2026; your screen may differ. Look for the button with the same name.

We use Render as an example; other services do the same. First, put the project in a GitHub repository, as in lesson 1/08. Then, in Render, create a new Web Service, give it access to that repository and fill in:

  • Language: Python 3
  • Build Command: pip install -r requirements.txt
  • Start Command: uvicorn main:app --host 0.0.0.0 --port $PORT

At the end, Render gives you an address ending in onrender.com. Add /docs to it and try it. Prices and options: see the Current state page and the service's own site.

Watch out

On Render, the disk is ephemeral: the loja.db file is lost every time the service is deployed again or restarts, and on some types of service also when it falls asleep for lack of visits. For real data you need a permanent disk or a managed database. And this API has no login: anyone can create products. Do not put real data in it.

Tip

Ask the reviewer subagent (lesson 2/06) to read the project before you publish: “Look for secrets, files that should not go to GitHub and endpoints without a test.”

Exercise

How I know it worked python3 -m pytest shows all the tests passing (for example, 4 passed); the /docs page shows the three endpoints, on your computer and at the Render address; git log --oneline shows at least four commits.

If it didn't work

  • “No module named fastapi”: the virtual environment is not active.
  • The tests create loja.db: they are not using the temporary database. Reject the result and repeat the step 2 request.
  • Render fails during the build: requirements.txt is missing from the repository, or it is not in the root folder.
  • Render says the application does not start: check the Start Command, letter by letter.

What you take from this level

  • Asking with a done criterion and tests first (2/01).
  • Commands and skills for what you repeat (2/02).
  • Hooks for what cannot fail (2/03).
  • MCP, with care about prompt injection (2/04).
  • One task per session and the state in a file (2/05).
  • Subagents, the right model for each task and branches (2/06).
  • A project from plan to publication (2/07).

In Level 3

The Agent SDK, so you can build your own agents; agents in CI/CD, the automatic checks that run by themselves on every change; security in depth; several agents working together; and a final project with an autonomous system and its brakes. In an appendix, cross-auditing: an agent from one provider reviewing the work of another.

Sources:

Videos

Como Criar API em Python Usando o FastAPI
Hashtag Programação · 25 min · PT-BR · subtitles · Watch on YouTube
How to create an API in Python with FastAPI. It is from 2022, but the general idea of an API with routes still holds.
Please Learn How To Write Tests in Python… • Pytest Tutorial
Tech With Tim · 33 min · EN · subtitles · Watch on YouTube
In the specified section: installing pytest, writing a first test and assertions, fixtures and parameterised tests.

Test what you learned

1. In the proposed plan, the agent wants to add login, photos and categories to the three endpoints. What do you do?
2. The tests pass on your computer. Before publishing, what do you do?
3. You published the API. The next day, the products you created have disappeared. What is the most likely explanation?