Skip to content

koxudaxi/fastapi-code-generator

Repository files navigation

fastapi-code-generator

Generate a FastAPI application from an OpenAPI document.

PyPI version Downloads PyPI - Python Version codecov license Code style: black

📣 💼 Maintainer update: Open to opportunities. 🔗 koxudaxi.dev

Documentation

The docs site is the source of truth for user-facing documentation:

Use this README as a quick start. The full examples, templating details, and development workflow live in docs/.

Installation

uv tool install fastapi-code-generator
pip install fastapi-code-generator

Quick Start

fastapi-codegen --input openapi.yaml --output app --output-model-type pydantic_v2.BaseModel

fastapi-code-generator uses datamodel-code-generator to build the model layer. See the overview guide for a complete end-to-end example, custom templates, custom visitors, and router generation.

Command Help Snapshot

This block is generated from the current CLI so the README and docs overview stay aligned. For tested option scenarios and examples, use the CLI reference page.

Usage: fastapi-codegen [OPTIONS]

Options:
  -e, --encoding TEXT             [default: utf-8]
  -i, --input TEXT                [required]
  -o, --output PATH               [required]
  -m, --model-file TEXT
  -t, --template-dir PATH
  --model-template-dir PATH
  --enum-field-as-literal [all|one]
  -r, --generate-routers
  --specify-tags TEXT
  -c, --custom-visitor PATH
  --disable-timestamp
  -d, --output-model-type [pydantic.BaseModel|pydantic_v2.BaseModel|dataclasses.dataclass|typing.TypedDict|msgspec.Struct]
                                  [default: pydantic.BaseModel]
  -p, --python-version [3.9|3.10|3.11|3.12|3.13|3.14]
                                  [default: 3.10]
  -V, --version
  --install-completion            Install completion for the current shell.
  --show-completion               Show completion for the current shell, to
                                  copy it or customize the installation.
  --help                          Show this message and exit.

Common Tasks

  • Generate a single-file app from an OpenAPI spec: fastapi-codegen --input openapi.yaml --output app
  • Generate router modules for larger applications: fastapi-codegen --input openapi.yaml --output app --template-dir modular_template --generate-routers
  • Limit router regeneration to specific tags: fastapi-codegen --input openapi.yaml --output app --template-dir modular_template --generate-routers --specify-tags "Wild Boars, Fat Cats"
  • Target Pydantic v2 output: fastapi-codegen --input openapi.yaml --output app --output-model-type pydantic_v2.BaseModel

Further Reading

About

This code generator creates FastAPI app from an openapi file.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages