FastAPI | A study of Python all-new API focussed web framework

The Problem

A major problem with Python is that it is slow. And people will tell you that it’s fast enough to do 99% of the work. But let’s be honest it becomes a bottleneck when a project becomes big after a certain point if you are using any mainstream Python framework(Django or Flask). Now to deal with this problem, Python has developed and strengthened its position in the world of async. There are many features that have been integrated into the language with the advent of the Python3 series. FastAPI uses these features to give you the ability to make high-performance APIs on par with NodeJS or Go without leaving the python ecosystem.

FastAPI

FastAPI is a light-weighted, open-source, fast, and high-performance python web framework. Like Flask, FastAPI also provides the freedom to its developers in all aspects such as choosing development design, middleware support or authorization/ authentication, etc with added extra features of async/await.

FastAPI is not highly opinionated, unlike Django. Therefore it does not feel as heavy as Django. And it works better with NoSQL than the latter.

Open Standards & inbuilt API documentation

Fast-API follows OpenAPI standards. For each API, it automatically creates documentation with a UI interface. It uses swagger internally to do it. You can find the docs at http://localhost:8000/docs for development purposes.

Not present in Flask and Django. I have written about how you can integrate Swagger with Django. Check this out if you want to.

Type Hints & Data Validation

Using Pydantic, Fast-API performs custom data type checks on API input schemas and also on methods we define. It reduces human-prone errors drastically. We can perform more exotic type checks such as Email, URL & UUID, etc.

Not present in both Flask and Django.

Security and authentication

FastAPI provides JWT authentication to secure our web application. See the demo implementation of the same also. These security tools are easy to integrate with your systems, data stores, relational and NoSQL databases, etc.

Testing

FastAPI provides built-in TestClient to test APIs and other functionalities.

Not present in Flask.

Background Tasks

It means, we can perform some tasks even after sending the API response to the client. We do not have to wait for a particular task to be completed before we send our response. It is a feature that for the most part people relate to Node(JS). Now you get the same function build-in a Python framework which is very neat. No need/installation to use Celery or Reddis like we have to do when using Django or Flask.

Async & Await

To optimize our product from IO/ Networking bound functionalities, we can perform asynchronous operations. In FastAPI, we have in-built Async & await Concurrency features to perform asynchronous operations.

An example of this is we build one API that was doing three (small milliseconds) operations and each operation was taking one second in sleep. In actuality, these operations take milliseconds to finish. So when one operation goes to sleep, it actually is finished but because there is 1-second sleep the operation takes 1 second. Instead what we can do is finish 2 other operations in that 1 second itself! So, the total sum of time taken will be 1 second instead of taking 3 seconds if we have done it sequentially. This is supported by FastAPI which is not present in either Django or Flask.

It what makes FaskAPI much much faster than the other two frameworks.

Production Server

An ASGI server further improves the performance. FastAPI uses Uvicorn & Gunicorn server in development and production environments. Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools. Uvicorn is used for both development and production server. Gunicorn has a pre-fork worker model.

Development

The development process is quite similar to flask development. FastAPI definitely reduces some development overhead such as data validation, asynchronous functionality, etc. That's what makes it more developer-friendly. Learning is quite easy even if you are not familiar with Flask development.

Framework Documentation

FastAPI framework documentation is quite good. You just need to open the home page and search your query keyword. It will search your query across the whole website.

Production Ready

FastAPI can handle 9000 requests at a time. FastAPI can manage database sessions, web sockets, easy GraphQL injection and many more are still being built. FastAPI is being used by tech giants such as Netflix, Facebook, Microsoft, Uber. Making production-ready RestAPIs with few lines of code is apprehensive. Check this out

Database Independent

It’s independent of a database or ORM and compatible with all of them. Including relational databases and NoSQL.

Templating

If you want a server-side rendering of templates like in Django you can do it quite easily. You need to work/install jinja and it works seamlessly.

Follow the link to know more about it.

Modular Design / Domain-driven API modules

FastAPI provides APIRouter to decompose large products into small components. This functionality is similar to the Blueprint functionality of the Flask framework.

Downside of FastAPI

Since FastAPI is relatively new, the community is small compared to other frameworks, and regardless of its detailed documentation, there are very few external educational materials.

But here are some awesome resources related to FastAPI. I do no think you have to look anywhere else for quite a while.

Conclusion

If you want to delve into the world of microservices with Python. Then FastAPI is the way to go. It is as easy as Flask and as fast as Node. Which makes it a perfect candidate for your next project.

Happy Coding!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store