Return to blog

Introduction to FastAPI — a great modern web framework

hero image for Introduction to FastAPI — a great modern web framework

Hi folks! It has been some time since I last wrote an article. I had lots of work, but now I’m back! Today I would like to present you my favourite web framework in Python - FastAPI.

This framework is based on the author’s experience with other Python web frameworks like Django, Flask, Requests. Fast API is inspired not only by web frameworks. One of the inspirations for it was also NestJS.

Fast API uses python types to have a great editor support and a powerful dependency injection system. You can read more about the inspirations for Fast API on their main page—I highly recommend it!

But what is it that makes me like this framework so much?

Clear and detailed documentation

Let’s start from the very beginning. What is the first thing you check in a new framework? That’s right—the main page of the framework! On the web page you can find lots of information starting from what Fast API and its key features are.

The style of the documentation is very comfortable because on the left, there are the main elements of the documentation (that is deployment, tutorials, etc.) and on the right you can see the table of contents for a selected element.

Support for async

Python frameworks had problems with handling asynchronous requests. Many popular python web frameworks uses WSGI. The Fast API uses ASGI which allows to handle the async requests. But how does it look like? Below, I present a sample of the async endpoint.

@app.get('/example')
async def example_endpoint():
    return { 'result': 'Example' }

Interactive API documentation

Fast API framework has a built-in interactive documentation. We can access the documentation by using  <address_of_your_service>/docs.

Let’s take our example code and run it via uvicorn main:app --reload command.

As we can see, the service is running. Now we can enter the documentation via http://127.0.0.1:8000/docs.

We can see our endpoints defined in the code. Some of you has for sure recognised this technology—Swagger.

Fast API has it implemented in the framework. It’s very comfortable because each of our endpoints get documented automatically. Morover, we can test the endpoints from the documentation level.

Modern API

FastAPI is an interesting web framework. When we take a look at other web frameworks, we will see specific patterns. For example Django provides a lot of useful out of the box functions at the expense of coding flexibility. Django forces a particualr pattern of resolving problems, creating software.

On the other hand, there is Flask web framework which provides the user with lots of freedom. The cost of it is that the developer must do lots of work to create for example the MVP.

FastAPI is similar to Flask but it’s more balanced. In the documentation, we can read that FastAPI recommends the typing to control the program flow better.

@app.get('/users/{user_id}')
async def get_user(user_id: int):
    return { 'user_id': user_id }

We pass the type int to the get_user endpoint. Now when we try to pass value other than int, we will get the information that value is not a valid integer.

Thanks to Pydantic we have an out of the box validation. We can use primitive types and build more complex data types. If we want to create our own validation, we can do it, as well. This is one of the many built-in functions in Fast API.

In the documentation we can find a lot of things like:

  • Integration with different databases
  • Integration with GraphQL
  • Async tests
  • Security

For more information, visit: https://fastapi.tiangolo.com/

Summary

Fast API is a modern and fresh web framework in the Python world. The framework contains documentation with lots of useful information.

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.

Another case that makes Fast API worth to learn is modern API—implementing backend is extremely comfortable and easy. Thanks to the built-in features, we don’t need to make everything on our own.

The FastAPI performance is also worth mentioning. FastAPI is based on uvicorn, a lightning-fast ASGI server implementation, using uvloop and httptools. In benchmarks FastAPI is the fastest web framework available in Python.

In my opinion, if you are looking for a python web framework for your project—FastAPI is definitely worth to consider!

Link to repository: https://github.com/DominikMarciniszyn/fast-api-article

As a reliable software company we’re focused on delivering the best quality IT services. However, we’ve discovered that programming skills give us a very particular opportunity...

.eco profile for codetain.eco

Reach Us

65-392 Zielona Góra, Poland

Botaniczna 70

© 2015-2024 Codetain. All rights reserved.

cnlogo