---
source: https://iinventedlight.com/library/notes/fastapi-background-tasks.md
title: "Background tasks in FastAPI"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE); prose restates or closely paraphrases cited source lines"
license_family: fastapi-mit
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Background Tasks - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE); FastAPI docs, Middleware - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: FastAPI"
tags: ['fastapi', 'background-tasks', 'middleware']
sources:
  - id: S1
    title: "Background Tasks - FastAPI"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/background-tasks/
    accessed: 2026-11-04
    license: "MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE)"
  - id: S2
    title: "Middleware - FastAPI"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/middleware/
    accessed: 2026-11-04
    license: "MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE)"
---
# Background tasks in FastAPI

**Answer:** 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. (S1)

## Examples

- As sending an email tends to be "slow" (several seconds), you can return the response right away and send the email notification in the background. (S1)
- If you receive a file that must go through a slow process, you can return a response of "Accepted" (HTTP 202) and process the file in the background. (S1)
- FastAPI will create the object of type BackgroundTasks for you and pass it as that parameter. (S1)

## Ordering with middleware

- A "middleware" is a function that works with every request before it is processed by any specific path operation. (S2)
- If you have dependencies with yield, the exit code will run after the middleware. (S2)
- If there were any background tasks, they will run after all the middleware. (S2)

## Claims
- id: C1
  claim: Background tasks run after the response.
  source: S1
  evidence: "You can define background tasks to be run after returning a response."
- id: C2
  claim: FastAPI creates the BackgroundTasks object.
  source: S1
  evidence: "you can declare a parameter of type BackgroundTasks at multiple levels"
- id: C3
  claim: Middleware works with every request.
  source: S2
  evidence: "And also with every response before returning it."
- id: C4
  claim: Background tasks run after middleware.
  source: S2
  evidence: "they will run after all the middleware."

## Sources
- [S1] Background Tasks - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/tutorial/background-tasks/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.
- [S2] Middleware - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/tutorial/middleware/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
