Skip to content
Back to skills

Python Web Api

ASecurity

Use when building or extending a FastAPI web service — router, service, and repository layering, pydantic request and response models, async boundaries, and HTTP error handling.

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 3, 2026
developmentpythonrustgofastapiapidocumentation

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 3 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add missing-bits/claude-plugins --skill python-web-api --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Python Web Api?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Python Web Api
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/missing-bits-python-web-api/badge)](https://www.skillsdirectory.com/skills/missing-bits-python-web-api)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: python-web-api
description: Use when building or extending a FastAPI web service — router, service, and repository layering, pydantic request and response models, async boundaries, and HTTP error handling.
---

# Python web APIs (FastAPI)

Tightly scoped to FastAPI services. Cite rules in review findings as
`(standard: python-web-api, rule: <id>)`. Each rule names its source:
a PEP, an RFC, an official doc, or `this standard` (a recorded house
decision).

## Layering

`router → service → repository`, dependencies point one way only:

| Layer | Owns | May import |
|---|---|---|
| router | HTTP: paths, status codes, request/response models, DI wiring | service |
| service | business logic, transactions, domain rules | repository, domain types |
| repository | data access (DB, external APIs) | domain types, drivers |

- FastAPI dependency injection (`Depends`) lives at the **router layer
  only** — services receive plain arguments, so they test without HTTP.
- A router importing a repository, or a service importing `fastapi`,
  fails review. Worked example:
  [reference/layering.py](reference/layering.py).
(id: `web-layering`; severity: important; source: this standard; DI
mechanics per FastAPI documentation — "Dependencies")

## pydantic v2 models

- Request and response models are **separate classes from domain
  objects** — API shape and domain shape evolve independently.
- Validation happens at the boundary (pydantic on the way in); inside
  the service layer, data is already trusted and typed.
- Response models are declared on the route (`response_model=`), so
  accidental field leaks are impossible.
(id: `web-boundary-models`; severity: minor; source: FastAPI
documentation — "Response Model" — and pydantic documentation; the
separate-models rule is this standard)

Sub-rules:
- a route with no `response_model` declared (id:
  `web-boundary-models.missing-response-model`; severity: important)

## Async boundaries

- Route handlers are `async def` (PEP 492 coroutines); **no blocking IO
  inside an async path**. The named offenders: sync HTTP clients
  (`requests`), `time.sleep`, unpooled/sync DB drivers, blocking file
  IO on large files.
- Genuinely sync work (CPU-bound, legacy driver) is offloaded:
  `run_in_threadpool` / `asyncio.to_thread`, or the handler is honest
  and declared plain `def` (FastAPI threads it).
(id: `web-async-boundaries`; severity: critical; kind: defect; source:
PEP 492; offloading mechanics per FastAPI documentation — "Async")

## HTTP error handling

- Domain exceptions map to statuses in ONE place — exception handlers
  registered on the app; routes and services never build
  `HTTPException` from business logic.
- Every error response is an **RFC 9457 Problem Details** document
  (`Content-Type: application/problem+json`): `type` (a stable URI
  reference — relative `/errors/<slug>` is fine until external
  consumers need dereferenceable docs), `title`, `status`, `detail`,
  plus our extension member `code` — the short machine-readable slug
  clients switch on. Model and wiring in
  [reference/error-handling.py](reference/error-handling.py).
- Stack traces never reach a response body; they go to the log with the
  request id.
(id: `web-error-envelope`; severity: minor; source: RFC 9457 "Problem
Details for HTTP APIs"; the `code` extension member and the
no-traceback rule are this standard)

Sub-rules:
- a stack trace reaching a response body (id:
  `web-error-envelope.stack-trace-leak`; severity: important)

Files in this skill

  • SKILL.md3.5 KB
  • reference/error-handling.py2.5 KB
  • reference/layering.py1.8 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.