FastAPI-SQLAlchemy
Adds simple SQLAlchemy support to FastAPI
What it is and what it does
FastAPI-SQLAlchemy is a thin integration layer that attaches SQLAlchemy session management to FastAPI applications via middleware. It provides a global `db` object that gives routes automatic access to a request-scoped database session, and also supports out-of-request database access (e.g., in background tasks) by using the same `db` object as a context manager. The package relies on Python 3.7+ ContextVar to isolate sessions per request, ensuring thread-safe and async-safe behavior.
The package is designed for developers who want to use SQLAlchemy's ORM with FastAPI without manually managing session lifecycles in each route. It abstracts away the boilerplate of creating, binding, and cleaning up sessions, letting you focus on query logic. However, because the package entered dormancy after 2020, it may not reflect current best practices or compatibility with recent FastAPI and SQLAlchemy releases.
Use it for:
- Add SQLAlchemy ORM support to a new FastAPI REST API without writing custom session management code.
- Query a database from FastAPI route handlers using the global `db.session` object.
- Access the database in background tasks or scheduled jobs using `db()` as a context manager.
- Migrate a Flask-SQLAlchemy application to FastAPI while keeping similar session patterns.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides middleware and helpers to integrate SQLAlchemy database sessions with FastAPI applications, enabling both request-scoped and background task database access.
Yes, if you are starting a new FastAPI project with SQLAlchemy and are comfortable with a dormant package. The integration is straightforward and low-friction. No, if you require active maintenance, up-to-date compatibility with the latest FastAPI and SQLAlchemy versions, or ongoing support—consider alternatives or manage sessions manually. The lack of updates since 2020 is a significant concern for long-term projects.
Install
fastapi-sqlalchemy on PyPI
pip
pip install fastapi-sqlalchemyuv
uv add fastapi-sqlalchemypoetry
poetry add fastapi-sqlalchemyInstalling FastAPI-SQLAlchemy
Before you install
Low friction install with just two runtime dependencies (starlette and SQLAlchemy). However, the package is dormant—last release was 2020-09-25 and last commit 2024-04-09—so expect no active maintenance or support for newer FastAPI or SQLAlchemy versions.
License in practice
MIT license (permissive) means you can use, modify, and distribute this package freely in both open-source and commercial projects with minimal restrictions.
Quickstart
pip install fastapi-sqlalchemy
from fastapi import FastAPI
from fastapi_sqlalchemy import DBSessionMiddleware, db
app = FastAPI()
app.add_middleware(DBSessionMiddleware, db_url="sqlite://")
@app.get("/users")
def get_users():
users = db.session.query(User).all()
return users
Requires Python 3.7 or later; session access relies on ContextVar, which is built into Python 3.7+.
Verify before relying
- Compatibility with FastAPI versions released after 2020-09-25 is unclear given the dormant maintenance status.
- Whether the package works with current SQLAlchemy 2.x API changes is not documented in the fact sheet.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — starlette, SQLAlchemy |
| Maintenance | dormant — 2,149 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 109,417/month — #12,516 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: FastAPI_SQLAlchemy-0.2.1-py3-none-any.whl
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
fastapi-paginationAdds pagination support to FastAPI applications…
permissive · top 5,000 on PyPI
fastapi-users-db-sqlalchemyProvides a SQLAlchemy ORM adapter for FastAPI…
permissive · top 5,000 on PyPI
fastcrudFastCRUD provides async CRUD operations and…
permissive · top 15,000 on PyPI
Flask-SQLAlchemyFlask-SQLAlchemy integrates SQLAlchemy database…
permissive · top 1,000 on PyPI
fastapi-injectableEnables FastAPI's Depends() dependency…
permissive · top 15,000 on PyPI
fastapi-utilitiesProvides reusable utilities for FastAPI…
permissive · top 15,000 on PyPI
fastapi-utilsProvides reusable utilities and base classes…
permissive · top 5,000 on PyPI
ormarAn async ORM for Python that unifies database…
permissive · top 15,000 on PyPI
databasesProvides async database access for PostgreSQL,…
permissive · top 5,000 on PyPI
fastapi-restfulProvides utilities and base classes to reduce…
permissive · top 15,000 on PyPI