What is FastAPI?

When we talk about frameworks for web development in the Python ecosystem , the first name that comes to mind is almost always Django or Flask , but the Python ecosystem is very rich and has many other frameworks aimed at API development, one of them is FastAPI, which is relatively new and is gaining more and more enthusiasts. In this article we are going to talk a little about how this framework works and what are the characteristics of this framework.

What is FastAPI?

FastAPI is a Python framework focused on API development , its main characteristics are being modern, fast and simple. It is a relatively new framework, it had its first version released on November 15, 2018, but make no mistake, despite being new it is not inferior to other frameworks that have been on the market for a longer time, such as Django or Flask.

A little history of FastAPI

FastAPI was developed and is currently maintained by Sebastián Ramírez , the same says on the framework’s website that he had the idea to create FastAPI from some difficulties encountered when using the frameworks that existed at the time to carry out the task of developing API’s, in addition to that most of the time these frameworks did not have support for the most modern features of Python, such as the type hints feature for example.

Main features

Taking the motivation for creating the framework into account, you can already imagine that it takes advantage of the most modern features made available by Python, another interesting point is that precisely because it is a recent framework it could be developed already taking into account the mistakes and successes from other frameworks that have been on the market for a longer time.

The main features of FastAPI are:

Fast : API’s developed with FastAPI have a high performance, to the point of being compared with API’s developed with Node.js and Go. It is even considered one of the fastest Python frameworks, second only to Starlette and Uvicorn, which are frameworks used internally by FastAPI;

Intuitive : The framework’s source code was entirely developed using Python’s type hints feature, this allows IDE’s and Source Code Editors to have a better intellisense, thus making it less time to debug the code.

Easy : It was designed entirely to be easy to use and learn, thus making you spend a lot less time reading the documentation.

Robust : The code developed is ready for production, so we don’t need to make any changes to then put the developed applications on the air, in addition to that FastAPI generates the documentation using Swagger and OpenAPI automatically.

Preparing our environment to use FastAPI

Now let’s see a simple example of what the code of an application developed with FastAPI looks like, remembering that FastAPI needs Python at least in version 3.6.

I will take into account that you already have Python correctly installed on your machine, if not, I recommend reading the Python Installation article and our first Hello World , where the entire language installation process is shown.

Initially, we are going to create a new folder and a virtual environment so that we can isolate our development environment from the global Python installation, to do so, run the following commands in the terminal:

The first command is used to create a folder called exemplo-fastapi our project folder, then we use the command cdto enter that folder and finally we use the command python -m venv .venvto create a new virtual environment.

Now that we have our virtual environment created we need to activate it, the activation of the virtual environment tends to change depending on your operating system, if you are using Linux or MacOS run the following command: