Build Better APIs With Python
How to build production-ready APIs with FastAPI
API development is a huge domain with a thriving ecosystem. Unsurprisingly, Python is a big player in this space.
That also means that there are many frameworks designed or supporting API development in Python. There are a few big names that seem to dominate though —like Flask, Django, and FastAPI.
Both Flask and Django are general web development frameworks, and they’re great at what they do — API development included.
FastAPI on the other hand is a smaller project, focused solely on API development. Now, FastAPI simply excels in this domain. Although both Flask and Django are good, it’s hard to argue in their favor over FastAPI.
So, this article will introduce FastAPI. We will take a look at why it’s so powerful, and how we can get started with it — covering:
> Why FastAPI?> Building an API> API DocumentationWhy FastAPI?
There are four big reasons:
- Native async support
- Superfast speeds
- Incredible ease of use
- Automated documentation
Asynchronous
FastAPI natively supports asynchronous calls.

So for example, if we send multiple requests to our API, it would be great if we could execute those requests in parallel. That’s exactly what we can do with FastAPI.
This approach is used everywhere, so having an API that supports it is incredibly important.
Superfast
FastAPI is fast in every way, but here I refer to latency speeds. It is easily the fastest option we have for API development in Python. We can compare it to other popular frameworks using TechEmpower — the results look like this:

Between Flask to FastAPI, we have a speedup of ~10x — which is insane.
Ease of Use
You will see how easy the framework is to use throughout this article, but as a quick taster, we code a GET request like this:
@app.get()
def func_name():
...
return responseDocumentation
Easily my favorite little feature is the flashy API documentation that is automatically generated when we deploy our API.
We will cover it near the end of this article — but in short — any method contained by our API will be identified and used to produce clean, but detailed documentation using Swagger UI.
Building an API
We create our API script file, typically this would be called main.py when working with FastAPI — but we can call it anything.
Inside this script, we will build a simple API with three methods — GET, POST, and DELETE.
Feel free to refer to the full script at the end of this section, or on GitHub here.
Setup
APIs built with FastAPI are incredibly easy to setup. Here we import fastapi and initialize our API like so:
from fastapi import FastAPIapp = FastAPI()GET
To add a GET request method, we simply decorate our get function with @app.get() — meaning that we place this on the line above the function that we would like to run whenever sending a GET request to our API.
If our API contains multiple endpoints, we define which endpoint to use with @app.get("/endpoint").
We won’t be using multiple endpoints in this example, but we will define a single endpoint of places to see how they work — this is not required though!
So, we want our GET request to return all of the places contained in a dataset (which we will define next). To do this, we write:
@app.get("/places")
def get_places():
return {'data': DATA}, 200It’s that simple! The only unknown here is the DATA variable. Typically we would use something like a SQL database connected to the API, but for the sake of simplicity, here we will use a dictionary defined within main.py:
DATA = {
'places':
['rome',
'london',
'new york city',
'los angeles',
'brisbane',
'new delhi',
'beijing',
'paris',
'berlin',
'barcelona']
}There’s one last thing we need to do to complete our GET request. To allow asynchronous operations, we simply add async to our function definition:
@app.get("/places")
async def get_places():
return {'data': DATA}, 200Our API so Far
Okay so we’ve initialized our API, defined our data, and added the GET method — how does all of this fit together?














