Post

FastAPI: A Quick Start Guide for Modern Python Web APIs

FastAPI: A Quick Start Guide for Modern Python Web APIs

Introduction

FastAPI is a modern, high-performance web framework for building APIs with Python 3.7+ based on standard Python type hints. It’s designed to be easy, fast, and intuitive, making it a popular choice for developers who need to create robust and efficient web services quickly.

This guide is the first in a series that will cover everything you need to know to become proficient with FastAPI. We’ll start with the basics and move toward more advanced topics, keeping explanations concise and code-focused.

Why Choose FastAPI?

FastAPI stands out for several key reasons:

  • Fast: It offers performance on par with NodeJS and Go, thanks to Starlette and Pydantic.
  • Fast to code: Features like type hints and dependency injection help you write code faster with fewer bugs.
  • Automatic Docs: It automatically generates interactive API documentation (using Swagger UI and ReDoc).
  • Intuitive: Great editor support with autocompletion everywhere.
  • Standards-based: Based on and fully compatible with OpenAPI and JSON Schema.

Installation

Getting started is simple. You’ll need FastAPI and an ASGI server like Uvicorn to run your application.

1
pip install fastapi "uvicorn[standard]"

This command installs FastAPI and Uvicorn with its standard dependencies for better performance.

Your First FastAPI Application

Let’s create a simple “Hello World” application. Create a file named main.py:

1
2
3
4
5
6
7
8
9
from fastapi import FastAPI

# Create an instance of the FastAPI class
app = FastAPI()

# Define a path operation decorator
@app.get("/")
def read_root():
    return {"Hello": "World"}

This code defines a single endpoint that responds to HTTP GET requests at the root path (/).

Running the Server

To run your application, use Uvicorn from your terminal. The default address is localhost:8000 (or 127.0.0.1:8000).

1
uvicorn main:app --reload
  • main: The file main.py.
  • app: The FastAPI instance created inside main.py.
  • --reload: Makes the server restart after code changes.

If you need to be explicit, you can specify the host and port. The following command does the same thing:

1
uvicorn main:app --host 127.0.0.1 --port 8000 --reload

You’ll see output indicating the server is running on http://127.0.0.1:8000.

Interactive API Docs

One of FastAPI’s best features is its automatically generated documentation. Once your server is running, navigate to these URLs in your browser:

  • Swagger UI: http://127.0.0.1:8000/docs
  • ReDoc: http://127.0.0.1:8000/redoc

You’ll find an interactive interface where you can see your endpoints, their parameters, and even test them live.

FastAPI Swagger Docs

Conclusion

You’ve just created your first FastAPI application! In this post, we covered the core concepts, installation, and how to run a basic server. The automatic documentation provides immediate visibility into your API’s structure.

In the next posts in this series, we will explore path and query parameters, request bodies, validations, and how to structure a larger application.

Suggested Reading

This post is licensed under CC BY 4.0 by the author.