fastapi-lifespan-di

Initialize and tear down async resources in FastAPI via lifespan context managers.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/dragonkid/dotfiles --skill fastapi-lifespan-di
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-lifespan-di
Source: https://github.com/dragonkid/dotfiles/tree/main/claude/skills/learned/fastapi-lifespan-di
Command: npx skills add https://github.com/dragonkid/dotfiles --skill fastapi-lifespan-di

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers safely manage the lifecycle of async resources in FastAPI applications by using a lifespan context manager and simple, type-safe dependency injection to ensure resources are initialized on startup and cleaned up on shutdown.

Core Features & Use Cases

  • Lifespan-based resource lifecycle management (initialize on startup, cleanup on exit) for resources like HTTP clients and database connections.
  • Type-safe dependency injection through getter functions to provide resources to route handlers.
  • Shared resources across multiple routes with predictable initialization and teardown, reducing boilerplate and errors.

Quick Start

Create a FastAPI app that uses a lifespan context to initialize resources and exposes getter functions for handlers. For example, define a lifespan that creates an HTTP client and a database service, then use get_http_client and get_db_service in route handlers to access them.

Frequently Asked Questions about fastapi-lifespan-di

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage shared async resources in a FastAPI application?

To manage shared async resources in FastAPI, use a lifespan context manager to initialize components like HTTP clients on startup and tear them down on shutdown. This approach provides predictable resource lifecycle management across multiple routes.

What is the best way to inject HTTP clients into FastAPI route handlers?

The best way to inject HTTP clients into FastAPI route handlers is by using type-safe getter functions. Define these functions within your lifespan context to access shared resources, reducing boilerplate and preventing initialization errors.

Does FastAPI lifespan dependency injection work for database connections?

Yes, FastAPI lifespan dependency injection works for database connections. You can initialize database services during the startup phase and ensure they are properly closed during the shutdown phase using the lifespan context manager.

How do I set up FastAPI resource cleanup on application exit?

To set up FastAPI resource cleanup on application exit, implement a lifespan context manager. This ensures that shared async resources like HTTP clients and database services are safely torn down when the application stops.

Can I share a single HTTPX client across multiple FastAPI routes?

Yes, you can share a single HTTPX client across multiple FastAPI routes by initializing it within a lifespan context manager. Type-safe getter functions then provide this shared client to your route handlers, ensuring efficient resource reuse.