DEV Community

Allyn E Farach
Allyn E Farach

Posted on

Migrating a Super Simple CRUD App from Flask to FastAPI

What is FastAPI and Why Do I Need It?

Flask has been one of the more popular Python frameworks for many years, but it’s time for an update. Today, we’ll be discussing Flask’s faster, more efficient cousin: FastAPI. Compared to Flask, FastAPI is more performant and has Swagger documentation and other goodies built-in. If you want to run a Python web application in production in 2021, you’ll want to use FastAPI.

While there are some syntactical differences between how you write Flask and how you write FastAPI, you’ll find that they’re quite similar. Today we’re going to migrate a simple CRUD app from using Flask to Fast. So follow along!

Here is the original Flask code:

from flask import Flask, request

app = Flask(__name__)

@app.route('/basic_api/entities/<int:entity_id>', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
    if request.method == "GET":
        return {
            'id': entity_id,
            'message': 'This endpoint should return the entity {} details'.format(entity_id),
            'method': request.method
        }
    if request.method == "PUT":
        return {
            'id': entity_id,
            'message': 'This endpoint should update the entity {}'.format(entity_id),
            'method': request.method,
            'body': request.json
        }
    if request.method == "DELETE":
        return {
            'id': entity_id,
            'message': 'This endpoint should delete the entity {}'.format(entity_id),
            'method': request.method
        }
Enter fullscreen mode Exit fullscreen mode

Now we’re going to break down each piece of this Flask code, and show you the equivalent code in FastAPI. First up, importing the libraries and instantiating the application object:

from flask import Flask, request

app = Flask(__name__)


from typing import Optional
from fastapi import FastAPI

app = FastAPI()

Enter fullscreen mode Exit fullscreen mode

Next up, let’s rewrite the /basic_api/entities/<int:entity_id> endpoint in FastAPI.

@app.route('/basic_api/entities/<int:entity_id>', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
   if request.method == "GET":
       return {
           'id': entity_id,
           'message': 'This endpoint should return the entity {} details'.format(entity_id),
           'method': request.method
       }
   if request.method == "PUT":
       return {
           'id': entity_id,
           'message': 'This endpoint should update the entity {}'.format(entity_id),
           'method': request.method,
           'body': request.json
       }
   if request.method == "DELETE":
       return {
           'id': entity_id,
           'message': 'This endpoint should delete the entity {}'.format(entity_id),
           'method': request.method
       }

Enter fullscreen mode Exit fullscreen mode

Note that in FastAPI, the request methods are defined as methods on the FastAPI object, for instance @app.get, @app.put, @app.post, etc rather than as a parameter. Also note that instead of stating the type of the url parameter entity_id, within the route, it’s instead typed as a parameter in entity()

@app.get('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should return the entity {} details'.format(entity_id),
   }


@app.put('/basic_api/entities/{entity_id}')
def entity(entity_id: int, body: Entity):
   return {
       'id': entity_id,
       'message': 'This endpoint should update the entity {}'.format(entity_id),
       'body name': body.name
   }


@app.delete('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should delete the entity {}'.format(entity_id),
   }

Enter fullscreen mode Exit fullscreen mode

Also note that in the put request route, we are passing along, as body, an Entity object. To define this object, we create a new class that inherits from BaseModel.

All together, the FastAPI application looks like:

from pydantic import BaseModel


class Entity(BaseModel):
   name: str
   description: Optional[str] = None


from pydantic import BaseModel
from typing import Optional
from fastapi import FastAPI

app = FastAPI()


class Entity(BaseModel):
   name: str
   description: Optional[str] = None

@app.get('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should return the entity {} details'.format(entity_id),
   }


@app.put('/basic_api/entities/{entity_id}')
def entity(entity_id: int, body: Entity):
   return {
       'id': entity_id,
       'message': 'This endpoint should update the entity {}'.format(entity_id),
       'body name': body.name
   }


@app.delete('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should delete the entity {}'.format(entity_id),
   }

Enter fullscreen mode Exit fullscreen mode

And that’s it! You can save the main.py file and run the server with

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

This will bring the server up on http://localhost:8000

One of the nice features of FastAPI is that it comes with Swagger already integrated. You can access it at http://localhost:8000/docs

From there, you can test each of your endpoints to see that they work!

Alt Text

Top comments (5)

Collapse
 
narenandu profile image
Narendra Kumar Vadapalli

Flask==2.0.0 also provides the app.get, app.post etc... Probably swagger is the only built in advantage here? Plugins like Flask-RestX provide the swagger functionality but with additional boilerplate. In that sense FastAPI is cleaner w.r.t documentation

Collapse
 
zayyadi profile image
zayyadi

Asynchronous support in FastAPI made it clear of Flask for API development

Collapse
 
sylflo profile image
Sylvain Chateau

I think Flask supports async since the version 2

Thread Thread
 
zayyadi profile image
zayyadi

Yeah, not as extensive as FastAPI

Collapse
 
anasse99 profile image
AnasSE • Edited

What Is FastAPI?
FastAPI is a modern Python web framework designed specifically for building APIs and backend services.
It is known for high performance, making it suitable for applications that need to handle many API requests.
FastAPI uses Python type hints and Pydantic to automatically validate incoming data.
It automatically generates interactive API documentation, making APIs easier to test and understand.
It supports asynchronous programming with async and await, which is useful for I/O-heavy applications.
FastAPI provides dependency injection, which helps developers organize authentication, database sessions, permissions, and reusable logic.
It includes tools for implementing common API security and authentication mechanisms, such as OAuth2 and bearer tokens.
FastAPI can work with databases such as PostgreSQL, MySQL, and MongoDB through appropriate libraries.
A common modern backend architecture is Flutter/Web App → FastAPI → PostgreSQL.
FastAPI is useful for building REST APIs, mobile backends, e-commerce systems, social media applications, microservices, and AI/ML APIs.
Its combination of speed, simplicity, automatic validation, documentation, and modern Python features makes it a strong choice for API development.
FastAPI is especially useful when building Python-based AI applications, because AI and machine-learning models can be exposed through API endpoints.
Key Takeaway

FastAPI simplifies modern API development by allowing developers to build fast, reliable, and scalable Python backends with less boilerplate code. Its automatic validation, documentation, type hints, async support, and database integration make it suitable for both beginner projects and production applications.

For a complete explanation of FastAPI, including how it works, key features, advantages, and real-world use cases, you can read the full guide on your blog: blogmeta.pk/what-is-fastapi/