Skip to Content
πŸš€ APSO is now in public beta. Get started β†’
GuidesFrameworksPython (FastAPI)Overview

Python (FastAPI)

Coming soon. Python code generation is not yet available. TypeScript (NestJS) is the only stable language template today. The information below is a preview of the planned Python support.

The Python template uses FastAPI, a modern, fast web framework for building APIs with Python 3.7+.

Overview

ComponentTechnology
FrameworkFastAPI
ORMSQLAlchemy 2.0
ValidationPydantic
DocumentationOpenAPI/Swagger
Testingpytest

Quick Start

# Create a new Python service apso server new my-api --template python # Navigate to project cd my-api # Create virtual environment python -m venv venv source venv/bin/activate # or venv\Scripts\activate on Windows # Install dependencies pip install -r requirements.txt # Start development server uvicorn main:app --reload

Project Structure

my-api/ β”œβ”€β”€ app/ β”‚ β”œβ”€β”€ entities/ # SQLAlchemy models β”‚ β”‚ β”œβ”€β”€ project.py β”‚ β”‚ └── task.py β”‚ β”œβ”€β”€ routers/ # API routes β”‚ β”‚ β”œβ”€β”€ projects.py β”‚ β”‚ └── tasks.py β”‚ β”œβ”€β”€ schemas/ # Pydantic schemas β”‚ β”‚ β”œβ”€β”€ project.py β”‚ β”‚ └── task.py β”‚ β”œβ”€β”€ services/ # Business logic β”‚ β”œβ”€β”€ extensions/ # Your custom code β”‚ β”œβ”€β”€ core/ # Config, security β”‚ └── main.py # Entry point β”œβ”€β”€ migrations/ # Alembic migrations β”œβ”€β”€ tests/ # Test files β”œβ”€β”€ .apsorc # APSO schema └── requirements.txt

Key Technologies

FastAPI

Modern async Python framework:

  • Automatic OpenAPI documentation
  • Type hints and validation
  • Async/await support
  • Dependency injection

SQLAlchemy 2.0

Type-safe database access:

from sqlalchemy import Column, String, ForeignKey from sqlalchemy.orm import relationship from app.core.database import Base class Project(Base): __tablename__ = "projects" id = Column(String, primary_key=True) name = Column(String, nullable=False) organization_id = Column(String, nullable=False) tasks = relationship("Task", back_populates="project")

Pydantic

Request/response validation:

from pydantic import BaseModel, Field class ProjectCreate(BaseModel): name: str = Field(..., max_length=200) description: str | None = None class ProjectResponse(BaseModel): id: str name: str description: str | None organization_id: str created_at: datetime class Config: from_attributes = True

Route Example

# app/routers/projects.py from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from app.core.deps import get_db, get_current_user from app.schemas.project import ProjectCreate, ProjectResponse from app.services import projects as project_service router = APIRouter(prefix="/projects", tags=["projects"]) @router.get("/", response_model=list[ProjectResponse]) async def list_projects( skip: int = 0, limit: int = 20, db: AsyncSession = Depends(get_db), user = Depends(get_current_user), ): return await project_service.get_all( db, user.organization_id, skip=skip, limit=limit ) @router.post("/", response_model=ProjectResponse, status_code=201) async def create_project( project: ProjectCreate, db: AsyncSession = Depends(get_db), user = Depends(get_current_user), ): return await project_service.create(db, project, user.organization_id)

Configuration

Environment Variables

# .env DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5432/db JWT_SECRET=your-secret-key DEBUG=true

Settings

# app/core/config.py from pydantic_settings import BaseSettings class Settings(BaseSettings): database_url: str jwt_secret: str debug: bool = False class Config: env_file = ".env" settings = Settings()

Commands

CommandDescription
uvicorn main:app --reloadStart dev server
pytestRun tests
alembic upgrade headRun migrations
alembic revision --autogenerateCreate migration

Features

Auto-Generated

  • SQLAlchemy models
  • CRUD routers
  • Pydantic schemas
  • OpenAPI documentation
  • Authentication middleware

Customizable

  • Add custom routes in extensions
  • Override service functions
  • Add middleware
  • Integrate external libraries
Last updated on