Questions tagged [fastapi]

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. FastAPI is released under the terms of the MIT license.

Some notable features of FastAPI are:

  • Based on open standards like OpenAPI and JSON Schema.
  • Automatic docs including Swagger UI and ReDoc
  • Just Modern Python (based on standard Python 3.6 type declarations)
  • Editor support (especially auto-completion)
  • Written on top of Starlette, inherits all its features and provides native ASYNC support
  • Uses Pydantic to validate request and response which is Pythonic and developer does not need to learn another micro-framework to write validation schemas for requests and responses
  • Native Dependency Injection System
  • Useful Plugins

And other features described in detail here.

Documentation: https://fastapi.tiangolo.com

Source Code: https://github.com/tiangolo/fastapi

5670 questions
128
votes
14 answers

FastAPI throws an error (Error loading ASGI app. Could not import module "api")

I tried to run FastAPI using uvicorn webserver but it throws an error. I run this command, uvicorn api:app --reload --host 0.0.0.0 but there is an error in the terminal. Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) Started reloader…
Pokisutra
  • 1,301
  • 2
  • 5
  • 5
92
votes
10 answers

How do I return an image in fastAPI?

Using the python module fastAPI, I can't figure out how to return an image. In flask I would do something like this: @app.route("/vector_image", methods=["POST"]) def image_endpoint(): # img = ... # Create the image here return Response(img,…
Hooked
  • 84,485
  • 43
  • 192
  • 261
86
votes
4 answers

How can I run the fast-api server using Pycharm?

I have a simple API function as below, from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"Hello": "World"} I am starting the server using uvicorn command as, uvicorn main:app Since we are not calling…
JPG
  • 82,442
  • 19
  • 127
  • 206
82
votes
8 answers

How to read body as any valid json?

I haven't found the docs for that use case. How can I get the request body, ensure it's a valid JSON (any valid JSON, including numbers, string, booleans, and nulls, not only objects and arrays) and get the actual JSON. Using Pydantic forces the…
caeus
  • 3,084
  • 1
  • 22
  • 36
77
votes
12 answers

Python: FastAPI error 422 with POST request when sending JSON data

I'm building a simple API to test a database. When I use GET request everything works fine, but if I change to POST, I get 422 Unprocessable Entity error. Here is the FastAPI code: from fastapi import FastAPI app = FastAPI() @app.post("/") def…
Smith
  • 1,037
  • 1
  • 6
  • 15
73
votes
4 answers

Pydantic enum field does not get converted to string

I am trying to restrict one field in a class to an enum. However, when I try to get a dictionary out of class, it doesn't get converted to string. Instead it retains the enum. I checked pydantic documentation, but couldn't find anything relevant to…
ierdna
  • 5,753
  • 7
  • 50
  • 84
70
votes
6 answers

Is it possible to use FastAPI with Django?

I'm a Django developer and recently stumbled onto the FastAPI framework. Then I decided to give it a shot. But usually when you talk about building RESTful APIs with Django you usually use the Django Rest Framework (DRF). Is anybody aware if it is…
Leonardo Guerreiro
  • 901
  • 1
  • 9
  • 18
63
votes
2 answers

What are the best practices for structuring a FastAPI project?

The problem that I want to solve related the project setup: Good names of directories so that their purpose is clear. Keeping all project files (including virtualenv) in one place, so I can easily copy, move, archive, remove the whole project, or…
Lord-shiv
  • 1,103
  • 2
  • 9
  • 19
54
votes
2 answers

How can I send an HTTP request from my FastAPI app to another site (API)?

I am trying to send 100 requests at a time to a server http://httpbin.org/uuid using the following code snippet from fastapi import FastAPI from time import sleep from time import time import requests import asyncio app = FastAPI() URL=…
john mich
  • 2,477
  • 3
  • 17
  • 32
53
votes
3 answers

How can I enable CORS in FastAPI?

I'm trying to enable CORS in this very basic FastAPI example, however it doesn't seem to be working. from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware app = FastAPI() app.add_middleware( CORSMiddleware, …
user270199
  • 905
  • 1
  • 6
  • 12
52
votes
2 answers

How to give a Pydantic list field a default value?

I want to create a Pydantic model in which there is a list field, which left uninitialized has a default value of an empty list. Is there an idiomatic way to do this? For Python's built-in dataclass objects you can use field(default_factory=list),…
Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
50
votes
4 answers

FastAPI asynchronous background tasks blocks other requests?

I want to run a simple background task in FastAPI, which involves some computation before dumping it into the database. However, the computation would block it from receiving any more requests. from fastapi import BackgroundTasks, FastAPI app =…
Gary Ong
  • 788
  • 1
  • 5
  • 8
49
votes
11 answers

Make every field as optional with Pydantic

I'm making an API with FastAPI and Pydantic. I would like to have some PATCH endpoints, where 1 or N fields of a record could be edited at once. Moreover, I would like the client to only pass the necessary fields in the payload. Example: class…
nolwww
  • 1,355
  • 1
  • 15
  • 33
48
votes
3 answers

How to pass the default value to a variable if None was passed?

Can I make a default value in Pydantic if None is passed in the field? I have the following code, but it seems to me that the validator here only works on initialization of the model and not otherwise. My Code: class User(BaseModel): name:…
Manas Sambare
  • 1,158
  • 2
  • 11
  • 22
48
votes
5 answers

Architecture Flask vs FastAPI

I have been tinkering around Flask and FastAPI to see how it acts as a server. One of the main things that I would like to know is how Flask and FastAPI deal with multiple requests from multiple clients. Especially when the code has efficiency…
Justin Yeoh
  • 647
  • 1
  • 7
  • 8
1
2 3
99 100