Skip to content

    Chapter 2, Basic

    Installing and running the first app

    In a few minutes you have an API up and running. The important thing is to do this within an isolated environment, and to know what each command does.

    Requirements

    The current FastAPI requires Python 3.10 or higher. To check which Python you have:

    Terminal
    python3 --version
    

    On Windows, use python --version (or py --version). If the version is older than 3.10, update before continuing.

    Create the project and install

    A virtual environment isolates the dependencies of each project, so that one does not break the other. Choose the tool you use:

    Terminal
    uv init minha-api
    cd minha-api
    uv add "fastapi[standard]"
    

    The uv add creates the environment, installs the package, and registers it in pyproject.toml. To run any command inside the environment, use the prefix uv run.

    Installing without a virtual environment works, but it's a bad idea: two projects that need different versions of FastAPI will start fighting for the same Python. The quotes in "fastapi[standard]" prevent the terminal from interpreting the brackets.

    The extra [standard] brings the uvicorn (the server that runs the application), the command fastapi and the dependencies for forms and uploads. In tests, you will also want the HTTP client that the TestClient uses:

    Terminal
    uv add --dev pytest httpx2
    

    The first app

    Create the file main.py:

    basico/cap02_instalacao.pylinhas 10 a 17
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/")
    def home():
        return {"mensagem": "Olá, FastAPI"}
    

    To see what the route returns without starting a server, the TestClient makes the request in memory. The entire course uses this feature to show real results:

    basico/cap02_instalacao.pylinhas 19 a 23
    from fastapi.testclient import TestClient
    
    cliente = TestClient(app)
    resposta = cliente.get("/")
    print(resposta.status_code, resposta.json())
    
    Output
    200 {'mensagem': 'Olá, FastAPI'}
    

    Run the server

    The FastAPI development command starts the server and restarts itself every time you save the file:

    Terminal
    uv run fastapi dev main.py
    

    Open http://127.0.0.1:8000 in the browser to see the JSON, and http://127.0.0.1:8000/docs for the interactive documentation. The classic way, which you will find in much material, calls the uvicorn directly and gives the same result:

    Terminal
    uv run uvicorn main:app --reload
    

    The text main:app means "in the module main, the object called app". In production, the command is fastapi run main.py (without automatic reload), and chapter 30 details this.

    The pages that come for free

    FastAPI automatically creates three addresses in addition to your routes:

    basico/cap02_instalacao.pylinha 28
    print(cliente.get("/docs").status_code, cliente.get("/redoc").status_code, cliente.get("/openapi.json").status_code)
    
    Output
    200 200 200
    
    AddressWhat is
    /docsSwagger UI: lists the routes and lets you test them
    /redocThe same documentation, in a readable format
    /openapi.jsonThe API description, which the other two pages use

    What is `--reload`

    Automatic reloading is only for development. It watches the files and restarts the process on each change, which consumes resources and is not safe in production.

    Exercise 1

    A health route

    Create a app with the route GET /saude that returns {"status": "ok"}, and check with the TestClient. Routes like this are used by deployment platforms to know if the application is alive.