A Brief Introduction to FastAPI

Pinkal Sanoria
crossML Blog
Published in
4 min readOct 7, 2021
Photo by https://www.pexels.com/@jeshoots from Pexels

Good programming language frameworks make it easy to produce quality products faster. Great frameworks even make the whole development experience enjoyable. FastAPI is a new Python web framework that’s powerful and enjoyable to use.

Today, we’re going to explore FastAPI, an open-source web framework used to build APIs with Python.

We’ll cover:

  • What is FastAPI?
  • FastAPI features
  • Installation
  • FastAPI Hello Word!
  • What to learn next

Let’s get started!

What is FastAPI?

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python. It is easy to learn, fast to code, and production-ready.

The most exciting feature of FastAPI is that it supports asynchronous code out of the box using the async/await Python keywords. Testing FastAPI endpoints are really straightforward and can be done using TestClient provided by FastAPI. This makes Test Driven Development(TDD) very easy. We can easily deploy our apps via Dockers provided by FastAPI or via AWS Lambda, even when using Lambda layers.

FastAPI is very fast due to its out-of-the-box support of the async feature of Python 3.6+.

FastAPI was released in 2018, and it was created by Sebastián Ramírez. Ramírez was unhappy with existing frameworks like Flask and DRF, so he created his own framework using tools like Starlette and Pydantic. Now, many big tech companies like Uber, Netflix, and Microsoft are using FastAPI to build their apps.

FastAPI features:

  • High-performance: As the name suggests, FastAPI is fast. It’s considered to be one of the fastest Python frameworks currently available.
  • Robust: You can create production-ready code using automatic interactive documentation.
  • Intuitive: FastAPI was designed to be easy to use and learn. It offers great editor support and documentation.
  • Quick to code: FastAPI increases your developing speed by 200%-300%.
  • Fewer bugs: It reduces around 40% of induced bugs.
  • Compatible: It works well with the open standards for APIS, OpenAPI (previously known as Swagger), and JSON schema.
  • Plugins: You can easily create plugins using dependency injection.
  • Type hints: You can use type hinting for data validation and conversion.

Pros

  • Data validation: It validates your data type even in nested JSON requests.
  • Exception handling: With FastAPI, it’s easy to do exception handling.
  • Asynchronous code support: It supports async code using the async/await Python keywords.

Cons

  • Request validation: FastAPI uses Pydantic for request validation. This process isn’t always very intuitive, and it sometimes requires you to write your own custom validator.
  • Smaller community: Since the framework is still pretty new, the community is smaller in comparison to other frameworks.

Use cases

FastAPI is commonly used for projects such as:

  • Internal crisis management
  • Deploying machine learning models
  • Create accounts, logins, and authentication for web applications

Installation

Before you can use it, you’ll need to install the dependencies.

Also, install uvicorn to work as the server:

FastAPI Hello World

Let’s get some practice with FastAPI! We’ll take a look at a simple Hello World! and break down the pieces.

Documentation: https://fastapi.tiangolo.com

Create a file main.py with:

Run it

Run the server with:

Check it

Open your browser at http://127.0.0.1:8000

You will see the JSON response as:

Interactive API docs

Now go to http://127.0.0.1:8000/docs.

You will see the automatic interactive API documentation:

What to learn next

Congrats on taking your first steps with FastAPI! FastAPI is a lighter web framework for Python. It allows you to build APIs easily, quickly, and efficiently. If you’re interested in web application development, learning FastAPI will put you ahead of the curve. To get comfortable with the framework, we suggest you dive deeper into the framework and work on some projects.

Some recommended topics to cover next are:

  • Parallel processing
  • Deploying your API to the cloud using GitHub
  • Default and optional parameters

You’re now ready to start creating your own highly performant APIs for your projects. If you want to dive deeper into the world of FastAPI, then you can follow the official User Guide in the FastAPI documentation.

Happy learning!

hello@crossml.com

--

--