FastAPI Beginners Tutorial 2025
FastAPI Beginners Tutorial 2025
Welcome to the FastAPI beginners tutorial for 2025. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It’s designed to be fast, robust, and easy to use, with a strong focus on code readability and simplicity. In this tutorial, we’ll take you through the basics of building a FastAPI application, from installation to deployment.
Installing FastAPI
To start building your FastAPI application, you need to have Python 3.7 or higher installed on your system. You can install FastAPI using pip, the Python package manager. Here’s how you can do it:
pip install fastapi uvicorn
This command installs FastAPI and Uvicorn, a ASGI web server that can run your FastAPI application.
Creating Your First FastAPI Application
Now that you have FastAPI installed, you can create your first application. Create a new file called `main.py` and add the following code:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
This code creates a new FastAPI application and defines a single route, `/`, that returns a JSON response with the message “Hello World”.
Running Your FastAPI Application
To run your FastAPI application, you can use the Uvicorn web server. Here’s how you can do it:
uvicorn main:app --reload
This command starts the Uvicorn web server and runs your FastAPI application. The `–reload` flag tells Uvicorn to reload the application if it detects any changes to the code.
Defining Routes and Path Parameters
In FastAPI, you can define routes using the `@app.get()`, `@app.post()`, `@app.put()`, and `@app.delete()` decorators. You can also define path parameters using the `{}` syntax. Here’s an example:
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}")
def read_user(user_id: int):
return {"user_id": user_id}
This code defines a new route, `/users/{user_id}`, that returns a JSON response with the `user_id` value.
Handling Request and Response Bodies
In FastAPI, you can handle request and response bodies using the `Request` and `Response` objects. Here’s an example:
from fastapi import FastAPI, Request
from pydantic import BaseModel
app = FastAPI()
class User(BaseModel):
name: str
age: int
@app.post("/users/")
def create_user(user: User):
return user
This code defines a new route, `/users/`, that accepts a JSON request body with the `name` and `age` fields. The `create_user` function returns the `User` object as a JSON response.
Using Middleware and Dependencies
In FastAPI, you can use middleware and dependencies to reuse code and handle common tasks. Here’s an example:
from fastapi import FastAPI, Depends
from fastapi.security import OAuth2PasswordBearer
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/users/")
def read_users(token: str = Depends(oauth2_scheme)):
return {"token": token}
This code defines a new route, `/users/`, that uses the `OAuth2PasswordBearer` middleware to authenticate the request. The `read_users` function depends on the `token` value, which is provided by the middleware.
Conclusion
In this tutorial, you’ve learned the basics of building a FastAPI application, from installation to deployment. You’ve also learned how to define routes, handle request and response bodies, and use middleware and dependencies. With this knowledge, you can start building your own FastAPI applications and take advantage of the framework’s speed, robustness, and ease of use.