An Introduction to FastAPI — a Great Modern Web Framework

Introducing FastAPI


FastAPI was created by
Sebastián Ramírez who was not happy with the existing frameworks like Flask and DRF. You can learn more about it here.

A Python web framework called FastAPI was created from the ground up to make use of contemporary Python features.

Many companies, including tech behemoths like Microsoft, Netflix, and Uber, have already started developing their APIs using the FastAPI package. It is one of the fastest Python frameworks available and its performance can be compared to NodeJS and Go.

Source: https://www.techempower.com/benchmarks/

The framework is made to enhance the user experience so that we can write straightforward code to create production-ready APIs that automatically follow best practices. 

Key Elements

  • Fast: incredibly strong performance, comparable to NodeJS and Go (thanks to Starlette and Pydantic). It is among the swiftest Python frameworks on offer.
  • Fast to code: Approximately double or triple the rate at which features are developed.
  • Fewer bugs: Cut back on developer-induced errors by roughly 40%.
  • Excellent editor support, intuitive. Finished work everywhere. Debugging takes less time.
  • Easy: Created to be simple to use and comprehend. less time reading documents
  • Reduce the amount of duplicate code. From each parameter declaration, multiple features. fewer bugs.
  • Secure code that is prepared for production with interactive documentation that is automatic.
  • Standards-based: based on (and fully compatible with) OpenAPI (formerly known as Swagger) and JSON Schema, two open standards for APIs.

Opinions

“[…] I’m using FastAPI a ton these days. […] I’m actually planning to use it for all of my team’s ML services at Microsoft. Some of them are getting integrated into the core Windows product and some Office products.” – Kabir Khan, Microsoft

“We adopted the FastAPI library to spawn a REST server that can be queried to obtain predictions. [for Ludwig]” – Piero Molino, Yaroslav Dudin, and Sai Sumanth Miryala, Uber

Using a great framework can lead to a much better final product. However, you need the right expertise to fully leverage any piece of technology. Hashtrust Technologies can provide access to the collective skill of a team of experienced Python developers. 

Installation

It’s always good to start by creating a virtual environment. Create the virtual environment and install FastAPI using pip.


You will also need an ASGI server, for production such as
uvicorn.

Without further ado, let’s learn how we can build a robust API solution using FastAPI.

Hello World to FastAPI

To get things going, we’ll use FastAPI to build a “hello world” app that will be run on the uvicorn server. You will quickly see how everything operates after reading this.

Make a file called main.py with:

The Python frameworks had problems with handling asynchronous requests. Many popular Python web frameworks use WSGI. The Fast API uses ASGI, which allows it to handle async requests. 

Run the server with:

Open your browser at http://127.0.0.1:8000/

You will see the JSON response as: {“message”: “Hello World”}

The JSON response is the same dictionary that you returned from the function in your FastAPI application.

Interactive API Docs

You can also view the interactive API documentation at http://localhost:8000/docs:

There is also the option of using ReDoc. We can access the same on http://localhost:8000/redoc. See below screenshot:

Learn More

Documentation is divided into two sections: tutorial and advanced user guide. Both contain a lot of resources on how to start with the framework and use its features.

In my opinion, if you are looking for a Python web framework for your project, FastAPI is definitely worth considering! I’m hoping that this article will inspire you to use FastAPI to develop your APIs. 

Get In Touch

Contact us today if you are looking for a FastAPI development company to help build your product. We are a trusted FastAPI development company that has been chosen by multiple companies across industries. 

There is no project too small for us to consider, so tell us about your ideas and we can help make them a reality!

Our Team

We have a team of experienced Python developers who are eager to work with you to make your ideas come to life. Many of them have prior experience working with FastAPI to build web applications. 

Meet the Team to see some of the developers you could be working with if you choose to build your FastAPI app with Hashtrust Technologies.

 

Leave a Comment

Your email address will not be published.