skillfed

FastAPI-SQLAlchemy

Adds simple SQLAlchemy support to FastAPI

fastapi-sqlalchemy v0.2.1 109.4K downloads/30d#12,516 on PyPI756
Permissive license MIT DORMANT released

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-sqlalchemy

uv

uv add fastapi-sqlalchemy

poetry

poetry add fastapi-sqlalchemy

Installing 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

Development Status :: 4 - BetaEnvironment :: Web EnvironmentFramework :: AsyncIOIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: Implementation :: CPythonTopic :: Internet :: WWW/HTTP :: Dynamic ContentTopic :: Internet :: WWW/HTTP :: HTTP ServersTopic :: Software Development :: Libraries :: Python Modules

Tags

fastapi sqlalchemy integrationfastapi database session middlewaresqlalchemy fastapi ormfastapi async databasefastapi db session management
fastapisqlalchemyorm

More Python Modules packages