FastAPI is built on top of Starlette for the web parts and Pydantic for the data parts. It uses Python type hints to validate, serialize, and deserialize data, which makes the code more readable and less error - prone.
Security in a FastAPI application involves protecting against various threats such as SQL injection, cross - site scripting (XSS), and unauthorized access. This can be achieved through proper authentication, input validation, and secure coding practices.
Authentication is the process of verifying the identity of a user. FastAPI supports various authentication mechanisms such as OAuth2, API keys, and HTTP Basic Auth.
Input validation is crucial to prevent malicious data from being processed by the application. Pydantic, which is integrated with FastAPI, can be used to validate input data against predefined schemas.
First, create a virtual environment and activate it. Then, install FastAPI and Uvicorn (a server for running FastAPI applications):
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
pip install fastapi uvicorn
Here is a simple example of a FastAPI application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
To run the application, use the following command:
uvicorn main:app --reload
Now you can access the application at http://127.0.0.1:8000
.
Pydantic can be used to define data schemas and validate input data. For example, let’s create a simple API that accepts a user’s name and age:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class User(BaseModel):
name: str
age: int
@app.post("/users/")
def create_user(user: User):
return user
In this example, Pydantic will automatically validate that the input data has the correct types and structure.
OAuth2 is a widely used authentication protocol. Here is an example of implementing OAuth2 with FastAPI:
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/items/")
def read_items(token: str = Depends(oauth2_scheme)):
return {"token": token}
This code creates a simple API that requires a valid OAuth2 token to access the /items/
endpoint.
Rate limiting can prevent abuse of your API by limiting the number of requests a user can make in a given time period. You can use the slowapi
library to implement rate limiting in FastAPI:
from fastapi import FastAPI
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
RATE_LIMIT = "5/minute"
@app.get("/")
@limiter.limit(RATE_LIMIT)
def read_root(request):
return {"Hello": "World"}
Setting secure headers can protect your application from various security threats. You can use the fastapi_headers
library to add secure headers to your responses:
from fastapi import FastAPI
from fastapi.responses import Response
from fastapi_headers import add_security_headers
app = FastAPI()
@app.get("/")
def read_root():
response = Response(content="Hello, World!")
add_security_headers(response)
return response
In this blog, we have covered the process of creating a secure FastAPI application from scratch. We started with the fundamental concepts of FastAPI and security, then moved on to setting up the project and implementing common security practices such as input validation and authentication. Finally, we discussed some best practices for performance and security, including rate limiting and setting secure headers. By following these guidelines, you can build a robust and secure FastAPI application.