What Is FastAPI?

FastAPI is one of the best Python frameworks for building high-performance APIs and web applications. Its also a great option for data scientists and analytics professionals deploying natural language processing (NLP) apps and machine learning models since much of that source code is Python-based, making integration easy.

FastAPI was initially created in 2018 and has quickly gained in popularity due its asynchronous performance and ease of use. The FastAPI framework is used by big tech companies such as Netflix, Uber, and Microsoft to build robust, enterprise-grade APIs that scale.

Its key features include:

  • It’s fast! FastAPI has comparable performance to NodeJS and Go.
  • Developer speed. The well-designed framework is easy to use and intuitive to work with, making development a breeze.
  • Fewer bugs. Web frameworks like FastAPI reduce the need to reinvent the wheel, which produces fewer errors in the process.
  • Robust. FastAPI takes advantage of Starlette and Pydantic for security and data validation.
  • Standards-based. FastAPI is based on OpenAPI and JSON Schema standards so developers get automated API documentation and automatic type validation.

The framework is easy to learn, has excellent documentation, and is supported by a great community of Python developers.

Performance

The world tends to think of Python as being just a slow scripting language when compared to its compiled counterparts. The instructions in scripts are executed one-thing-at-a-time in a very neat and orderly fashion. This synchronous-style of code execution impacts performance for web applications that communicate with other web applications across the internet. Delays are introduced by a number of things outside of our control:

  • waiting for data from the client
  • data passing through a slow network
  • reading file contents
  • writing to files
  • remote database and API operations
  • and so on

However, that’s not the case with FastAPI. The framework is built to be asynchronous. When Python 3.5 was released in 2015 it came with a new set of keywords—await and async—for creating coroutines. Coroutines allow developers to create asynchronous programs that handle multiple requests at the same time. They don’t have to wait for one task to finish before starting the next.

FastAPI fulfills a very nice want in a web framework; fast and easy.

Development

FastAPI is great to work with. As mentioned, the framework itself was created to be asynchronous, and it outruns the native performance of other Python frameworks like Flask and Django. Development in FastAPI feels similar to Flask because of its simplicity. However, FastAPI has much better documentation and so many built-in features for common tasks that it can also feel like working with a more professional framework, like Django.

With FastAPI, you don’t have to write as much code. When building an API from scratch, the developer must check all the incoming data for correct types. But this is not needed in FastAPI. The framework does the testing automatically for you and generates a validation error when it sees incorrect data.

The FastAPI framework automatically generates documentation by using the OpenAPI specification for all the endpoints you declare. It also has built-in support for web sockets, authentication, and cookies. And because you are defining your own types in FastAPI you get better auto completion and code suggestions when using an IDE like Visual Studio Code and PyCharm.

Learning the framework is enjoyable. The FastAPI documentation contains clear explanations, code examples, and user guides that cover setting up projects, working with databases, and application deployment. In addition, there are many sources for learning FastAPI. Udemy, YouTube, and testdriven.io all have great tutorials to help you get started.

Example Code

Everyone loves a “Hello World” example.

The following code snippet creates a FastAPI web application. A root path is created with the path operation decorator @app.get("/"). And the path function returns the message “Hello, World!” in JSON format.

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"message": "Hello, World!"}

Summary

FastAPI is one of the best Python frameworks for building high-performance APIs and web applications. Its key features make it fast, easy to learn, and easy to work with. Developers will love how quickly they can create a solid API with a well-documented, Python framework.

In the next lesson, Up and Running With FastAPI, we walk you through how to install FastAPI and Uvicorn, a web server that you can run locally.