Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 50 ยท Getting started

What FastAPI is and how to set it up

Learn what FastAPI is, how it builds on Starlette and Pydantic, why type hints drive everything, and how to install and run it on this site and at home.

FastAPI is a Python framework for building web APIs: programs that other programs talk to over HTTP, usually by exchanging JSON. A mobile app asks for a list of orders, a script posts a new customer, a web page loads a profile. FastAPI's central idea is that you describe your endpoints with ordinary type hints, and the framework does the rest: it reads the request, converts and validates the data, produces helpful errors, and writes interactive documentation.

This lesson explains what FastAPI is made of, why that design is pleasant to work with, and how you will run it throughout this tutorial, both in the editors on this page and on your own computer. If you want a quick tour first, REST APIs with FastAPI in the Python tutorial covers the highlights in one lesson. This track goes much deeper.

Two libraries under one hood

FastAPI is a thin, clever layer over two other libraries:

FastAPI is built from parts

Starlette Routing, requests, responses, middleware, WebSockets.
Pydantic Turns type hints into validation and JSON conversion.
FastAPI Glues them together and adds dependencies and docs.

Learning FastAPI means learning how these three fit together.

1 / 2

Starlette is a lightweight toolkit for web services. It knows about URLs, requests, responses, cookies, middleware and WebSockets. Pydantic is a data-validation library: you describe a shape with a class, and it checks and converts incoming data to match. FastAPI adds the glue: it inspects your function signatures, decides which parameters come from the URL, the query string or the body, calls Pydantic to validate them, and describes everything in an OpenAPI document.

Because FastAPI is a Starlette application, anything you learn about Starlette's Request, Response and middleware works directly. Because it uses Pydantic for data, every model you write is also a plain Python class you can use outside the web layer.

ASGI: the interface underneath

Older Python web frameworks such as Flask and classic Django speak WSGI, a simple interface where one function handles one request and returns. ASGI is its asynchronous successor. An ASGI application is a callable that receives a connection scope and two awaitable channels, one to receive messages and one to send them. This lets a single process juggle thousands of waiting connections, and it makes long-lived connections like WebSockets possible.

flowchart LR
    C["Client"] --> S["ASGI server<br/>(uvicorn)"]
    S --> A["FastAPI app<br/>(an ASGI callable)"]
    A --> E["Your endpoint"]
    E --> A
    A --> S
    S --> C

The server speaks HTTP to the client and ASGI to your app. The app never opens a socket itself.

That last point is the key to how this site works. Since an ASGI app is just a callable, anything that can speak ASGI can call it, with no server and no network involved.

How FastAPI runs on this site

The editors on this page run Python in your browser (Pyodide). A browser tab cannot open a port, so we do not start a server. Instead, every example does three things:

  1. Installs FastAPI with micropip, Python's package installer for the browser.
  2. Builds the app exactly as you would at home.
  3. Calls it with an httpx.AsyncClient whose transport is httpx.ASGITransport(app=app). The request goes straight into your app, through routing, validation and your endpoint, and the response comes back.

Your routes, validation and error handling run exactly as they would behind a real server. Only the network is missing. Run the next example to check that everything is installed, and to see the versions in use.

Python Install FastAPI in the browser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first run downloads the packages, so it takes a few seconds. Later runs on the same page reuse them.

async only. The browser cannot start threads, and FastAPI runs a plain def endpoint in a thread pool. So every live example here uses async def endpoints. Both styles work on a real server, and async def vs def endpoints explains when to pick which.

A first look at an app

Here is a complete app with one endpoint. Do not worry about the details yet; the next lesson walks through every line. Notice how little code there is, and that the name parameter has a type hint.

Python A tiny app, called in-process

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The third request sends shout=maybe, which is not a valid boolean. You wrote no checking code, yet FastAPI answered with status 422 and a description of the problem. That behavior comes entirely from the bool type hint, and it is the main theme of this whole track.

Running it at home

On your own machine, you install FastAPI with its standard extras, which include the fastapi command and the Uvicorn server:

Terminal
python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install "fastapi[standard]"

Save your app in main.py, then start the development server:

Terminal
fastapi dev main.py

You can also call Uvicorn directly, which is what many production setups do:

Terminal
uvicorn main:app --reload

main:app means "the object named app in the file main.py". The server listens on http://127.0.0.1:8000. Three addresses are worth knowing:

URL What you get
/docs Interactive Swagger UI, generated from your code
/redoc A reading-friendly version of the same documentation
/openapi.json The raw OpenAPI description of every endpoint

The server blocks above are read-only on this site because they need a real process and a port. Everything else you will see here, from endpoints to tests, you can run live.

Always use a virtual environment. It keeps FastAPI's dependencies away from your other projects. See packages and pip for the basics.

What you should already know

You will get the most out of this track if you are comfortable with Python functions and type hints, and have met async/await in the asyncio lesson. You do not need prior web-framework experience, though knowing roughly what HTTP methods and status codes are helps. Each lesson explains the web concepts it uses.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two libraries is FastAPI built on?

    Starlette provides the web toolkit (routing, requests, middleware) and Pydantic provides validation and conversion based on type hints.
  2. 2. What does ASGI let a Python web application do that classic WSGI does not?

    ASGI is the asynchronous interface. It lets one process wait on many connections at once and supports WebSockets, which WSGI cannot.
  3. 3. How do the live examples on this site call a FastAPI app?

    The ASGI transport sends the request straight into the app in the same process, so routing and validation run for real without any network.
  4. 4. In the first example, why did ?shout=maybe produce a 422 response?

    FastAPI reads the type hint, tries to convert the query string value, and rejects the request when the conversion fails. You wrote no validation code.

Summary

  • FastAPI is a Python API framework built on Starlette (web toolkit) and Pydantic (validation).
  • Type hints on your function parameters drive parsing, validation, documentation and editor support.
  • FastAPI apps are ASGI applications, so any ASGI-capable client or server can call them.
  • On this site, apps are installed with micropip and called in-process with httpx.ASGITransport; endpoints are async def.
  • At home you install fastapi[standard] and run fastapi dev main.py or uvicorn main:app --reload, then open /docs.

Next, write and call your own app in Your first endpoint and the test client.