Introduction
FastAPI is a high-performance framework for building APIs with Python 3.6+ versions, there are quite a few benefits of developing APIs with FastAPI, some of the benefits are,
- Auto Interactive API Documentation (Swagger in other Languages and Frameworks).
- Auto Data Validation
- Simplicity
The article explains how to develop a REST API with the FastAPI framework with examples and explores the benefits of the FastAPI mentioned above. Let’s explore.
Installation
Just like any other Python framework, FastAPI will be installed using the "pip" command
pip install fastapi
For running the API in a server, the article uses ‘uvicorn’.
pip install uvicorn
API
We will explore a simple Employee REST service with 2 GET Endpoints, the first one is ‘/employees’ which will return all the Employees in the System and the second one is ‘/employees/{empId}’ which returns the Employee details of a given id. Let’s create a filename ‘employee.py’
from fastapi import FastAPI
app = FastAPI()
@app.get('/employees')
def employees():
return {"empId":1, "name":"Test", "dept": "IT"}
First, the FastAPI will be imported, second line app = FastAPI() indicates that FastAPI is initialized, line number 3 is our GET endpoint and #4 is the endpoint implementation and it returns JSON.
Let’s run this simple service in ‘uvicorn’,
uvicorn employee:app -- reload
--reload indicates that service will run forever, and any changes will be hot deployed and changes immediately reflected without restarting the server.
By default, the server will start at port 8000, let’s execute the ‘/employees’ endpoint.
Nothing new so far, the magic of FastAPI will be visible on the execution of the endpoint ‘http://localhost:8000/docs’
Without any configuration in the service the docs are available to us, through the docs or Swagger the consumer of the API can see the structure of the API and validate/execute them, it is comprehensible for both developers & non-developers.
Developing the 2nd Endpoint ‘/employees/{empId}’, based on empId the Employee details will be returned.
from fastapi
import FastAPI
app = FastAPI()
employee_details = {
1: {
"name": "Sam",
"dept": "IT"
},
2: {
"name": "John",
"dept": "HR"
}
}
@app.get('/employees')
def employees(): return {
"name": "Test",
"dept": "IT"
}
@app.get('/employees/{empId}')
def employeeById(empId: int): return employee_details[empId]
The code is slightly changed, in the method employeeById details will be returned from the employee_details object and the parameter empId is of type int.
The empId belongs to ‘John’ which is returned properly by the API, but what if the consumer pass string instead of int, we have not handled that scenario, how API will behave. This false scenario will be handled by Auto Validation.
Excellent, developers are saved from all sorts of API documentations and Validation, even if pass an ID which doesn’t exist the FastAPI returns Internal Server Error
Conclusion
The article explains what FastAPI is how to install it, deploy it, and covers Auto Interactive Documentation and Auto Validation. The next article on FastAPI will cover POST / PUT endpoints and better documentation aspects.