Created
August 17, 2025 07:38
-
-
Save marchelbling/ee4b2d08e3bae7b5dee5bbc74b9eb519 to your computer and use it in GitHub Desktop.
FastAPI template API script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S uv run --script | |
| # /// script | |
| # requires-python = ">=3.10" | |
| # dependencies = [ | |
| # "fastapi", | |
| # "uvicorn[standard]" | |
| # ] | |
| # /// | |
| from fastapi import FastAPI | |
| from fastapi.responses import PlainTextResponse | |
| from pydantic import BaseModel, Field | |
| from typing import Annotated | |
| import os | |
| app = FastAPI(title="Template API", version="0.1.0") | |
| @app.get("/health", response_class=PlainTextResponse, tags=["health"]) | |
| def health() -> str: | |
| """ | |
| Liveness/readiness probe. Returns plain 'ok' with HTTP 200. | |
| """ | |
| return "ok" | |
| class Payload(BaseModel): | |
| # Adjust the schema to your needs. These constraints are just examples. | |
| name: Annotated[str, Field(min_length=1, description="Non-empty name")] | |
| count: Annotated[int, Field(ge=1, description="Must be >= 1")] | |
| tags: list[str] = Field(default_factory=list, description="Optional tags") | |
| @app.post("/validate", response_model=Payload, tags=["validation"]) | |
| def validate(payload: Payload) -> Payload: | |
| """ | |
| Validates incoming JSON against the Payload schema. | |
| On success, echoes the validated payload. | |
| """ | |
| return payload | |
| if __name__ == "__main__": | |
| # Simple, env-driven runner (override as needed) | |
| host = os.getenv("HOST", "0.0.0.0") | |
| port = int(os.getenv("PORT", "8000")) | |
| reload = os.getenv("RELOAD", "0") == "1" | |
| import uvicorn | |
| uvicorn.run(app, host=host, port=port, reload=reload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment