FastAPI vs. Flask: Python web frameworks comparison and tutorial

Published on June 12, 2025

fastapi-vs-flask-header1

FastAPI and Flask are popular Python frameworks for building web applications.

Like all frameworks, their goal is to provide a solid foundation for your unique code and remove a lot of the work required to build applications.

However, FastAPI and Flask provide different features and do not target the same use cases. So, which one is best to use for your project?

What is FastAPI?

FastAPI is a web framework for building REST APIs. These APIs provide the backend logic for mobile, web, and desktop applications that make API calls to your FastAPI app endpoints.

FastAPI is used to build headless apps. It does not provide the tools for generating pages, routing, or building user interfaces, and it only handles the backend logic and data requirements. You develop the client code separately, making it possible to develop multiple independent front ends.

For example, you can connect to your API from a browser-based single page web app, native mobile apps for iOS and Android, and even apps for driving digital billboards or autonomous AI agents. By using the same backend API, these independent front ends can work from the same code, using the same single source of data.

Adopting this API-first approach to designing and building your applications allows for great flexibility and long-term expandability with high scalability.

The key features of FastAPI that benefit API-driven applications include:

  • A clear separation of concerns: As FastAPI doesn't deal with frontend or presentation code at all, it promotes complete separation of client and server code. This can help with maintainability by splitting monolithic codebases into more manageable parts.

  • High performance and scalability: Applications that use APIs rather than dynamically generating pages lend themselves to microservices-based architectures that are highly scalable and can support multiple different types of clients.

  • Built in security and validation: FastAPI comes with built-in data validation and sanitization, as well as support for HTTP basic authentication, OAuth 2.0, and API keys.

  • Concurrency and async: FastAPI allows apps to serve multiple concurrent requests and run asynchronous tasks in parallel for increased performance and efficiency.

  • Automatic documentation and testing: FastAPI automatically documents API endpoints with example interactive requests and responses.

What is Flask?

Flask is a microframework for building full web applications. Rather than just focusing on APIs, it can be used to generate and serve complete web pages.

Flask has a broader scope than FastAPI. It also allows you to build full applications that load data and perform logic, as well as generate web pages for users to view and interact with. This, however, does not make it superior: it just targets a more general use case. 

Flask provides a quick way to spin up web applications, handling page routing and templating. It leaves you to choose your own data storage, UI components, and other elements, and then build your own templates and pages (hence being referred to as a "micro" framework, providing core functionality and as little else as is practical).

This is beneficial for developers who want to quickly prototype or who are entirely web-focused (such as those with outcomes reliant on search engine optimization). Flask can also be used to build API endpoints, and you can mix API and web pages in the same app; however, it is not solely optimized for this and is less scalable and performant than FastAPI for API-first development. Some development teams will find having their web app and API in the same codebase beneficial; however, for projects that have multiple front ends, keeping them separate is cleaner.

The Flask Python web framework offers the following features to developers who want to build browser-based web applications:

  • A minimalist microframework: There is no included validation, database abstraction, or other "extras" in Flask. Extensions provide all of these so you can choose what is appropriate for your project.

  • Extra functionality using extensions: Extensions add functionality such as database abstraction, email/notifications, adding REST APIs, connecting to AI tools, and almost anything else a developer might need to help bootstrap an app.

  • Templates for building pages: Flask includes the Jinja2 template engine (though you can use a different one if you wish), allowing you to generate web pages based on pre-defined templates, rather than having to duplicate the presentation (stylesheets, layout, etc.) code on each page.

  • Community support: Flask has been around since 2010, and it has a large community and plenty of support material, forum posts, and code examples to help you out.

  • Flexible: Due to its general use case, Flask is suitable for bootstrapping a wide variety of projects and is less prescriptive on how you should build apps.

Choosing FastAPI or Flask

Whether FastAPI or Flask is the "best" Python framework for you depends entirely on your project's requirements (both present and future). If you need to get a browser-based application up and running quickly, Flask provides a flexible platform for doing so; however, if you require a scalable, API-driven application with multiple front ends and longer-term viability, you may find FastAPI a more suitable choice. The level of help is also important: if your team is inexperienced with Python development, it's a lot harder to find help in FastAPI’s smaller (but growing) community.

Time to market and API-driven development aren't the only project considerations. FastAPI is much more suitable for real-time applications due to its support for concurrency with ASGI (Asynchronous Server Gateway Interface), whereas Flask only supports the synchronous WSGI (Web Server Gateway Interface).

Your overall architecture and application complexity matter too. Complex applications are often best split into microservices to improve reliability and maintainability. FastAPI is better suited to this, as microservices can use HTTP API endpoints to communicate, and they can expose endpoints to your other applications and user-facing clients to interact with them. Flask will largely let you do whatever you want, whereas FastAPI enforces some level of organization, which can be beneficial for complex projects (or disorganized development teams).

You should also consider the future of both your project and the framework you choose. Your choice should make it possible to implement future planned features and not paint you into a corner.. You can keep an eye on the development plans of the Flask and FastAPI teams to see if there are any relevant changes coming to the framework that will impact you.

Setting up a Python web development environment for FastAPI or Flask

Get started with FastAPI or Flask by installing Python 3 and the Python pip package manager for your operating system. You should also install support for Python virtual environments so that the dependencies and settings for your different projects don't conflict. 

On Ubuntu-based Linux systems, you can do this by running: 

apt install python3 python3-pip python3-venv

Note: the python command is used in the following instructions. Depending on your operating system and installation method, you may need to use the python3 command in its place instead. 

Next, create a folder for your project. This example will use my_web_project. A virtual environment is recommended for each of your Python projects to keep their dependencies and configuration separate. 

Create and enable a virtual environment for the current session by running:

python -m venv .

To activate the virtual environment (which you'll need to do for each new terminal session), run:

source ./bin/activate

You can check that the virtual environment is activated by looking at the start of your terminal prompt. Once activated, it will be prepended with your project name, and will look something like:

(my_web_project) username@localhost:~

You've now got everything you need to start working on either a Flask or FastAPI project.

Getting started with FastAPI

Navigate to the project folder created above and install FastAPI using pip by running the following command:

python -m pip install fastapi[standard]

Then, create a file called main.py, which will contain all of the code created in this tutorial. Add the following code to the file (as always with Python code, watch the indentation when copying and pasting, as the code won't run if it's not correctly formatted):

You've just created your first FastAPI app! Run it using this command:

python3 -m fastapi dev main.py

Here's the console output of FastAPI running a development server:

fastapi-vs-flask-image-1

You should see something like the following when you access the development server at http://localhost:8000:

fastapi-vs-flask-image-2

This example doesn't do much; it just returns a JSON message. Here's a main.py file that shows off a few of the unique features FastAPI offers with some comments pointing them out:

You can test these endpoints using Postman. Here's what happens when you make a POST request to /players that omits the high_score value:

fastapi-vs-flask-image-3

Notice the 422 response because the data does not pass validation. Here's how it looks when the request contains all of the required data and passes validation:

fastapi-vs-flask-image-4

This FastAPI tutorial and example demonstrates how, with relatively little code, you can use Python and the FastAPI framework to create a fully documented API that includes validation, custom errors, and concurrency.

As you write your code, FastAPI automatically generates documentation. To view this, you can access the following URLs:

fastapi-vs-flask-image-5


Depending on the purpose and scope of your project, once your API is functional, you can start building out your front end using React, Angular, Svelte, or any other client-side JavaScript framework.

How to deploy FastAPI

You can deploy FastAPI manually to your own servers or use managed cloud hosting. It is common to deploy FastAPI using Docker, and in larger deployments, use Kubernetes to orchestrate automatic scaling. 

You can also go “serverless” and run FastAPI apps on AWS Lambda to further reduce hosting costs and management overheads.

Getting started with Flask

Note: if you've already followed the tutorial steps for FastAPI, you'll need to create a new empty project directory and virtual environment.

Navigate to the project folder created above and install Flask using pip by executing the following command:

python -m pip install flask

Create a file called main.py, which will contain all of the code created in this tutorial. Add the following code to the file:

You've just created a working Flask web app! Run it with the following command:

python -m flask --app main.py run 

Below is the console output from a successfully running Flask development server:

fastapi-vs-flask-image-6

You should then see something like the following when you access the development server at http://localhost:5000:

fastapi-vs-flask-image-7

Notice how instead of a JSON response, Flask lets you return HTML directly that will be displayed by the browser. Flask is, by default, geared toward delivering pre-processed web pages like this rather than providing API endpoints for programmatic use.

You can also run Flask in debug mode (just mind you don't do so in production, as it allows for arbitrary code execution):

python -m flask --app main.py run --debug

Here's a more complex example of a Flask web app that demonstrates some of its features, with comments to explain:

Note the custom HTTP 403 error handler: in many cases, you want to hide the actual details of an error from the user. In this example, the default error output is replaced with a simple "Access denied" message that uses its own template.

fastapi-vs-flask-image-8

As with FastAPI, Flask gives you free rein to decide how to build your front end. If you use Flask to provide API endpoints, you can use full-fat frontend frameworks. If you are pre-rendering pages, you can use UI libraries like React with pre-built component libraries, or CSS libraries like Bootstrap or Tailwind.

How to deploy Flask

Like FastAPI, you can deploy Flask to your own servers, using docker, and to manage cloud platforms like AWS Elastic Beanstalk, Azure App Service, and Google App Engine.

FastAPI vs. Flask feature comparison

If you're still unclear on whether Flask or FastAPI is best suited for your project, you can directly compare features to help decide. 

 

Flask

FastAPI

Purpose/use case

General-purpose web framework

API development only

Community and ecosystem

Large, established community

Growing community as the framework continues to gain traction

Scalability

High, suitable for vertical and horizontal scaling

Very high, suitable for microservices applications and horizontal scalability

Performance

High

Very high, with better CPU and memory usage

Concurrency/async support

Synchronous requests only

Can handle multiple concurrent requests

Automatic documentation

No

Yes

Built-in validation/sanitization

Yes

No

Built-in security/authentication

Limited (CORS, XSS in templates)

Yes (authentication, CORS, XSS, sanitization)

Supported front ends

Any (SSR from Flask, or build an SPA to connect to Flask)

Any (front ends developed separately using the tools of your choice)

Companies that have used FastAPI to build the APIs that power their products include Uber, Netflix, and Microsoft, while Reddit, Trivago, and Patreon use Flask to build their web applications. Many larger organizations use both in conjunction, with each serving the different use cases best suited to their feature set.

One important thing to keep in mind when choosing your development tools is that the most performant, or the one with the longest feature list, may not actually be the best for your project and team. Any performance gains resulting from choosing a platform that lacks certain features, or that are simply not a good "fit" for your team and development style, could come at a greater cost than they are worth in the long run. Similarly, a long feature list that lacks a specific feature you need may not be as suitable as a more focused tool.

You need to figure out what to prioritize that will balance cost and ease of development; for example, prioritizing FastAPI for its performance, but having to manually implement a feature that could be provided by a Flask extension, may not be a sensible tradeoff.

You may not need to code your back end at all

Content management and delivery, including uploading, organizing, and optimizing text, images, and videos, is a core function of many web applications. Using Flask or FastAPI to build content management tools can add significant overheads to development, and reduce the resources your team has available to perfect the unique functionality and value your apps provide.

With Contentful, you create and curate content for your websites, applications, newsletters, billboards, and any other front end. You can then deliver this content to your apps using Contentful's GraphQL or REST APIs and its high-speed global content delivery network. You can speed up development even more by integrating Contentful directly into your Flask or FastAPI apps using our Python SDK.

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Meet the authors

David Fateh

David Fateh

Software Engineer

Contentful

David Fateh is a software engineer with a penchant for web development. He helped build the Contentful App Framework and now works with developers that want to take advantage of it.

Related articles

Images are an effective means to express ideas or explain a concept.
Guides

Always look your best: WebP, source sets, and the Contentful Images API

September 20, 2022

Need help choosing between Tailwind vs. Bootstrap for your project’s CSS framework? This article explains why the choice is important and helps you decide. 
Guides

Tailwind vs. Bootstrap: Comparing CSS frameworks

August 15, 2024

When deciding between Svelte vs. React for your project, you need to weigh up the performance and developer features of each. This guide will help you choose.
Guides

Svelte vs. React: Choosing the best for features and performance

March 9, 2023

Contentful Logo 2.5 Dark

Ready to start building?

Put everything you learned into action. Create and publish your content with Contentful — no credit card required.

Get started