Fastapi Python


Background Tasks¶. You can define background tasks to be run after returning a response. This is useful for operations that need to happen after a request, but that the client doesn't really have to be waiting for the operation to complete before receiving the response. FastAPI simple security Installation Usage Creating an application API key creation through docs API key creation in python Configuration Contributing Running the dev environment Needed contributions Mar 15, 2020 FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. The key features are: Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). FastAPI is a Python class that provides all the functionality for your API.

The simplest FastAPI file could look like this:

Copy that to a file

Run the live server:


The command uvicorn main:app refers to:

  • main: the file (the Python 'module').
  • app: the object created inside of with the line app = FastAPI().
  • --reload: make the server restart after code changes. Only use for development.

In the output, there's a line with something like:

That line shows the URL where your app is being served, in your local machine.

Check it¶

Open your browser at

You will see the JSON response as:

Interactive API docs¶

Now go to

You will see the automatic interactive API documentation (provided by Swagger UI):

Alternative API docs¶

And now, go to

You will see the alternative automatic documentation (provided by ReDoc):

Fastapi Python Jsonapi Serializer


Fastapi Python

FastAPI generates a 'schema' with all your API using the OpenAPI standard for defining APIs.


A 'schema' is a definition or description of something. Not the code that implements it, but just an abstract description.

API 'schema'¶

In this case, OpenAPI is a specification that dictates how to define a schema of your API.

This schema definition includes your API paths, the possible parameters they take, etc.

Data 'schema'¶

The term 'schema' might also refer to the shape of some data, like a JSON content.

In that case, it would mean the JSON attributes, and data types they have, etc.

OpenAPI and JSON Schema¶

OpenAPI defines an API schema for your API. And that schema includes definitions (or 'schemas') of the data sent and received by your API using JSON Schema, the standard for JSON data schemas.

Check the openapi.json

If you are curious about how the raw OpenAPI schema looks like, FastAPI automatically generates a JSON (schema) with the descriptions of all your API.

You can see it directly at:

It will show a JSON starting with something like:

What is OpenAPI for¶

The OpenAPI schema is what powers the two interactive documentation systems included.

And there are dozens of alternatives, all based on OpenAPI. You could easily add any of those alternatives to your application built with FastAPI.

You could also use it to generate code automatically, for clients that communicate with your API. For example, frontend, mobile or IoT applications.

Recap, step by step¶

Step 1: import FastAPI

FastAPI is a Python class that provides all the functionality for your API.

Technical Details

FastAPI is a class that inherits directly from Starlette.

Fastapi python code

You can use all the Starlette functionality with FastAPI too.

Step 2: create a FastAPI 'instance'¶

Here the app variable will be an 'instance' of the class FastAPI.

This will be the main point of interaction to create all your API.

This app is the same one referred by uvicorn in the command:

If you create your app like:

And put it in a file, then you would call uvicorn like:

Step 3: create a path operation


'Path' here refers to the last part of the URL starting from the first /.

So, in a URL like:

...the path would be:


A 'path' is also commonly called an 'endpoint' or a 'route'.

While building an API, the 'path' is the main way to separate 'concerns' and 'resources'.


'Operation' here refers to one of the HTTP 'methods'.

One of:

  • POST
  • GET
  • PUT

...and the more exotic ones:

  • HEAD

In the HTTP protocol, you can communicate to each path using one (or more) of these 'methods'.

When building APIs, you normally use these specific HTTP methods to perform a specific action.

Normally you use:

  • POST: to create data.
  • GET: to read data.
  • PUT: to update data.
  • DELETE: to delete data.

So, in OpenAPI, each of the HTTP methods is called an 'operation'.

We are going to call them 'operations' too.

Define a path operation decorator

The @app.get('/') tells FastAPI that the function right below is in charge of handling requests that go to:

  • the path /
  • using a get operation

@decorator Info

That @something syntax in Python is called a 'decorator'.

You put it on top of a function. Like a pretty decorative hat (I guess that's where the term came from).

A 'decorator' takes the function below and does something with it.

In our case, this decorator tells FastAPI that the function below corresponds to the path/ with an operationget.

It is the 'path operation decorator'.

You can also use the other operations:

  • @app.put()
  • @app.delete()

And the more exotic ones:

  • @app.options()
  • @app.head()
  • @app.patch()
  • @app.trace()


You are free to use each operation (HTTP method) as you wish.

FastAPI doesn't enforce any specific meaning.

The information here is presented as a guideline, not a requirement.

For example, when using GraphQL you normally perform all the actions using only POST operations.


Step 4: define the path operation function

This is our 'path operation function':

  • path: is /.
  • operation: is get.
  • function: is the function below the 'decorator' (below @app.get('/')).

This is a Python function.

It will be called by FastAPI whenever it receives a request to the URL '/' using a GET operation.

In this case, it is an async function.

You could also define it as a normal function instead of async def:


If you don't know the difference, check the Async: 'In a hurry?'.

Step 5: return the content¶

You can return a dict, list, singular values as str, int, etc.

You can also return Pydantic models (you'll see more about that later).

There are many other objects and models that will be automatically converted to JSON (including ORMs, etc). Try using your favorite ones, it's highly probable that they are already supported.


Fastapi Pythonanywhere

  • Import FastAPI.
  • Create an app instance.
  • Write a path operation decorator (like @app.get('/')).
  • Write a path operation function (like def root(): ... above).
  • Run the development server (like uvicorn main:app --reload).


Python has an embarrasment of riches when it comes to web frameworks, each with their own particular strengths. FastAPI is a new entrant that has been quickly gaining popularity as a performant and easy to use toolchain for building RESTful web services. In this episode Sebastián Ramirez shares the story of the frustrations that led him to create a new framework, how he put in the extra effort to make the developer experience as smooth and painless as possible, and how he embraces extensability with lightweight dependency injection and a straightforward plugin interface. If you are starting a new web application today then FastAPI should be at the top of your list.

Fastapi Pypy

Do you want to try out some of the tools and applications that you heard about on Podcast.__init__? Do you have a side project that you want to share with the world? With Linode’s managed Kubernetes platform it’s now even easier to get started with the latest in cloud technologies. With the combined power of the leading container orchestrator and the speed and reliability of Linode’s object storage, node balancers, block storage, and dedicated CPU or GPU instances, you’ve got everything you need to scale up. Go to today and get a $100 credit to launch a new cluster, run a server, upload some data, or… And don’t forget to thank them for being a long time supporter of Podcast.__init__!

Fastapi Python Online


  • Hello and welcome to Podcast.__init__, the podcast about Python and the people who make it great.
  • When you’re ready to launch your next app or want to try a project you hear about on the show, you’ll need somewhere to deploy it, so take a look at our friends over at Linode. With 200 Gbit/s private networking, node balancers, a 40 Gbit/s public network, fast object storage, and a brand new managed Kubernetes platform, all controlled by a convenient API you’ve got everything you need to scale up. And for your tasks that need fast computation, such as training machine learning models, they’ve got dedicated CPU and GPU instances. Go to to get a $20 credit and launch a new server in under a minute. And don’t forget to thank them for their continued support of this show!
  • Your host as usual is Tobias Macey and today I’m interviewing Sebastián Ramirez about FastAPI, a framework for building production ready APIs in Python 3


  • Introductions
  • How did you get introduced to Python?
  • Can you start by describing what FastAPI is?
    • What are the main frustrations that you ran into with other frameworks that motivated you to create an entirely new one?
  • What are some of the main use cases that FastAPI is designed for?
  • Many web frameworks focus on managing the end-to-end functionality of a website, including the UI. Why did you focus on just API capabilities?
    • What are the benefits of building an API only framework?
    • If you wanted to integrate a presentation layer, what would be involved in that effort?
  • What API formats does FastAPI support?
    • What would be involved in adding support for additional specifications such as GraphQL or JSON-LD?
  • There are a huge number of web frameworks available just in the Python ecosystem. How does FastAPI fit into that landscape and why might someone choose it over the other options?
  • Can you share your design philosophy for the project?
    • What are your main sources of inspiration for the framework?
    • You have also built the Typer CLI library which you refer to as the little sibling of FastAPI. How have your experiences building these two projects influenced their counterpart’s evolution?
  • What are the benefits of incorporating type annotations into a web framework and in what ways do they manifest in its functionality?
  • What is the workflow for a developer building a complex application in FastAPI?
  • Can you describe how FastAPI itself is architected and how its design has evolved since you first began working on it?
    • What are the extension points that are available for someone to build plugins for FastAPI?
  • What are some of the challenges that you have faced in building an async framework that is leveraging the new ASGI specification?
  • What are some sharp edges that users should keep an eye out for?
  • What are some unique or underutilized features of FastAPI that users might not be aware of?
  • What are some of the most interesting, unexpected, or innovative ways that you have seen FastAPI used?
  • When is FastAPI the wrong choice?
  • What are some of the most interesting, unexpected, or challenging lessons that you have learned in the process of building and maintaining FastAPI?
  • What do you have planned for the future of the project?

Keep In Touch

@tiangolo on Twitter. @tiangolo on GitHub.


  • Tobias
    • Once Upon A Time TV Show
  • Sebastián

Closing Announcements

  • Thank you for listening! Don’t forget to check out our other show, the Data Engineering Podcast for the latest on modern data management.
  • Visit the site to subscribe to the show, sign up for the mailing list, and read the show notes.
  • If you’ve learned something or tried out a project from the show then tell us about it! Email [email protected]) with your story.
  • To help other people find the show please leave a review on iTunes and tell your friends and co-workers
  • Join the community in the new Zulip chat workspace at


  • fastapi-utils
  • Hug API framework
  • Click CLI Framework
  • Tom Christie
  • ASGI

The intro and outro music is from Requiem for a Fish The Freak Fandango Orchestra / CC BY-SA