FastAPI REST API with PostgreSQL database using Aspire Python support.
This sample demonstrates Aspire 13's polyglot platform support for Python applications, showcasing a simple CRUD API built with FastAPI and PostgreSQL.
aspire run # Run locally
aspire deploy # Deploy to Docker Compose
aspire do docker-compose-down-dc # Teardown deploymentThe application consists of:
- Aspire AppHost - Orchestrates the Python API and PostgreSQL database
- FastAPI API - Python web API with CRUD operations for users
- PostgreSQL - Database for storing user data
- pgAdmin - Web-based PostgreSQL administration tool
The AppHost configuration demonstrates Aspire 13's Python support:
var builder = DistributedApplication.CreateBuilder(args);
builder.AddDockerComposeEnvironment("dc");
var postgres = builder.AddPostgres("postgres")
.WithPgAdmin()
.AddDatabase("db");
builder.AddUvicornApp("api", "./api", "main:app")
.WithExternalHttpEndpoints()
.WaitFor(postgres)
.WithReference(postgres);
builder.Build().Run();Key features:
- Python Polyglot Support: Uses
AddUvicornAppto run FastAPI applications - PgAdmin Integration:
.WithPgAdmin()adds a web-based database management tool - Startup Dependencies:
.WaitFor(postgres)ensures the database is ready before starting the API - Database Connection: Aspire provides connection properties via
POSTGRES_*environment variables - External HTTP Endpoints: Enables external access to the API
- Docker Compose Deployment: Ready for containerized deployment
The FastAPI application provides the following endpoints:
GET /- API informationGET /health- Health check with database connectivity testGET /users- List all usersGET /users/{id}- Get a specific userPOST /users- Create a new userDELETE /users/{id}- Delete a user
- Virtual Environment: Aspire automatically creates a
.venvdirectory and installs dependencies fromrequirements.txt - Modular Architecture: The API is organized into separate modules:
models.py- Pydantic models for data validationdatabase.py- Database connection and repository patternmain.py- FastAPI routes and application setup
- Database Initialization: On startup, the API creates the
userstable if it doesn't exist - Connection Management: Aspire provides PostgreSQL connection via
POSTGRES_*environment variables (non-.NET connection property pattern) - Startup Dependencies: The API waits for PostgreSQL to be ready before starting
- Development Experience: Zero configuration - Aspire handles virtual environment, dependency installation, and process management
This sample includes VS Code configuration for Python development:
.vscode/settings.json: Configures the Python interpreter to use the Aspire-created virtual environment- After running
aspire run, open the sample in VS Code for full IntelliSense and debugging support - The virtual environment at
api/.venvwill be automatically detected
The sample uses Docker Compose for deployment. Run:
aspire deployThis will:
- Generate a Dockerfile for the Python application
- Install dependencies and create a container image
- Generate Docker Compose files with PostgreSQL
- Deploy the complete application stack
To teardown:
aspire do docker-compose-down-dc