Skip to content

Meeting FastAPI: Speed and Documentation

Welcome to the fast lane! FastAPI is a modern, high-performance web framework for building APIs with Python based on standard Python type hints. It is designed to be easy to use, fast to code, and ready for production. Two of its massive benefits are:

  • Speed: It is one of the fastest Python frameworks available, on par with NodeJS and Go (thanks to Starlette and Pydantic).
  • Developer Experience: It drastically reduces bugs and increases development speed by providing great editor support (completion everywhere).

Before we start, ensure you have Python 3.8 or a later version installed. FastAPI relies on modern Python features, specifically type hints, to work its magic.

To start building, we need to install FastAPI and an ASGI server to run our code. The standard server used is Uvicorn. You can install them via pip:

Terminal window
pip install fastapi "uvicorn[standard]"

Let’s write the simplest possible file to see FastAPI in action. Create a file named main.py and add the following code:

from fastapi import FastAPI
# Create a FastAPI instance
app = FastAPI()
# Define a path operation decorator
@app.get("/")
async def root():
# Return a JSON response
return {"message": "Hello World"}

Here is what is happening:

  • app = FastAPI(): Creates the main application instance.
  • @app.get("/"): Tells FastAPI that the function below handles GET requests sent to the path /.
  • async def root(): This is the function that executes. FastAPI fully supports asynchronous code (async/await).

To run the application, we use the Uvicorn server we installed earlier. Open your terminal in the folder containing main.py and run:

Terminal window
uvicorn main:app --reload

The command breakdown:

  • main: Refers to the file main.py.
  • app: Refers to the object created inside main.py with the line app = FastAPI().
  • --reload: Makes the server restart automatically after code changes (perfect for development).

This is where FastAPI truly shines. Without writing a single extra line of code, FastAPI has generated interactive API documentation for you. It inspects your code and uses standards like OpenAPI and JSON Schema.

While your server is running, open your browser and go to http://127.0.0.1:8000/docs. You will see the Swagger UI, an interactive dashboard where you can see your endpoints and even execute requests directly from the browser.

Alternatively, you can visit http://127.0.0.1:8000/redoc to see the ReDoc interface, another beautiful way to visualize your API documentation.