If you have read some of my previous Python articles, you know I’m a Flask fan. It is my go-to for building APIs in Python. However, recently I started to hear a lot about a new API framework for Python called FastAPI. After building some APIs with it, I can say it is amazing!
This project was created by Sebastian Ramírez, and at the time of writing, it has accumulated almost 20K stars. Big names like Microsoft, Uber, Netflix, and others have been building APIs with it.
FastAPI is a rather minimalistic framework. However, that doesn’t make it less powerful. FastAPI is built using modern Python concepts and is based on Python 3.6 type declarations. Let’s see some of the features this library is packed with.
FastAPI是一个相当简单的框架。 但是，这并不会使它的功能降低。 FastAPI是使用现代Python概念构建的，并且基于Python 3.6类型声明。 让我们看一下该库附带的一些功能。
自动文档 (Automatic docs)
A must-have for any API is documentation about the endpoints and types. A common approach to solve this problem is the use of OpenAPI and tools like Swagger UI or ReDoc to present the information. These come packed automatically with FastAPI, allowing you to focus more on your code than setting up tools.
键入Python (Typed Python)
This is a big one: FastAPI makes use of Python 3.6 type declarations (thanks to Pydantic). This means that it uses a Python feature that allows you to specify the type of a variable. And this framework makes extensive use out of it, providing you with great editor support. Autocompletion works amazingly well.
这是一个很大的问题：FastAPI使用Python 3.6类型声明(感谢Pydantic)。 这意味着它使用Python功能，可让您指定变量的类型。 并且该框架对其进行了广泛使用，为您提供了强大的编辑器支持。 自动补全效果非常好。
Here is some sample code using typed declarations:
We just went from:
That’s it! And as a result:
Validation is already integrated into this framework thanks to Pydantic. You can validate standard Python types as well as some custom field validations. Here are a few examples:
由于Pydantic，验证已集成到此框架中。 您可以验证标准Python类型以及一些自定义字段验证。 这里有一些例子：
JSON objects (
JSON array (
str) with min and max lengths
float) with min and max values
- URL 网址
- Email 电子邮件
- UUID UUID
- And many more… 还有很多…
安全与认证 (Security and authentication)
This is a crucial part of any API and it’s code that we usually just repeat, so why not integrate much of it into the framework? FastAPI does exactly that.
The library provides support for the following:
- HTTP Basic HTTP基本
- OAuth2 (JWT tokens) OAuth2(JWT令牌)
- API keys in headers, query params, or cookies. 标头，查询参数或cookie中的API密钥。
This is perhaps not exactly a feature of the framework, but it is worth mentioning. The documentation of the project is simply amazing. It’s very clear and covers topics with examples and explanations.
这也许不是框架的确切功能，但是值得一提。 该项目的文档简直太神奇了。 这非常清楚，并通过示例和解释涵盖了主题。
FastAPI is fast! It is not only fast to code, but it also processes requests super fast! You can check the benchmarks across multiple frameworks using the TechEmpower benchmark tool. Here are the results I got for Python frameworks. Flask and Django are way behind on the list, with FastAPI being first and thus the most performant:
自然异步 (Asynchronous by Nature)
Let’s take a look at the following code:
async def create_item(item: Item):
result = await some_code_running_in_background(item)
FastAPI supports asynchronous endpoints by default, which can simplify and make your code more efficient. This is a huge advantage over Flask. Django already made some async support work, but it is not as integrated as it is in FastAPI.
FastAPI默认情况下支持异步端点，这可以简化并提高代码效率。 与Flask相比，这是一个巨大的优势。 Django已经进行了一些异步支持工作，但是集成程度不如FastAPI。
FastAPI is a relatively new framework that follows the minimalist approach of Flask but adds crucial features that make it easier to work with and stunningly performant. It is a great choice for your next API project, and I will be writing more about it as I use it more and more on my APIs.
Thanks for reading!