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
| Component | Technology |
|---|---|
| Framework | FastAPI |
| ORM | SQLAlchemy 2.0 |
| Validation | Pydantic |
| Documentation | OpenAPI/Swagger |
| Testing | pytest |
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 --reloadProject 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.txtKey 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 = TrueRoute 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=trueSettings
# 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
| Command | Description |
|---|---|
uvicorn main:app --reload | Start dev server |
pytest | Run tests |
alembic upgrade head | Run migrations |
alembic revision --autogenerate | Create 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
Related
Last updated on