Metadata-Version: 2.4
Name: prettyopenapi
Version: 0.1.0
Summary: A clean API documentation page for FastAPI: docs, code samples in cURL / Python / Node.js / Go and a Try it form, built from your OpenAPI document.
Author: laconal
License-Expression: Apache-2.0
Keywords: openapi,swagger,fastapi,api-documentation,api-docs
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: THIRD_PARTY_LICENSES.md
Requires-Dist: fastapi>=0.100
Dynamic: license-file

# prettyopenapi

A clean API documentation page for **FastAPI**. It reads your OpenAPI document and shows the parameters,
fields, types, enums and errors, code samples (cURL, Python, Node.js, Go) and a **Try it** form that sends a real
request from the browser and shows what was actually sent as cURL.

FastAPI builds the document, this package serves the page. Python 3.9 or newer.

## Install

```bash
pip install prettyopenapi
```

## Use

```python
from fastapi import FastAPI
from prettyopenapi import setup_docs

app = FastAPI(docs_url=None)   # turn off FastAPI's own Swagger UI, it uses the same /docs

# ... include your routers ...

setup_docs(app, "/docs")
```

Open `http://localhost:8000/docs`. The document itself is at `/docs/openapi.json`.
Call `setup_docs` after the routers are included, so the path check sees every route.

Not for production? Register it conditionally:

```python
import os
if os.getenv("ENV") != "production":
    setup_docs(app, "/docs")
```

### Next to Swagger UI

You can keep FastAPI's Swagger UI and put this page on another path:

```python
app = FastAPI()                 # Swagger UI stays at /docs
setup_docs(app, "/docs/v2")     # this page: /docs/v2/
```

If the path is already taken (for example `setup_docs(app, "/docs")` while Swagger UI is on), `setup_docs` raises
`PathConflictError` at startup and tells you what to change. Without the check the page would silently never be
reached, because the first matching route wins.

## `setup_docs()`

```python
setup_docs(app, path="/docs", *, title=None, lang=None, theme=None, base_url=None)
```

| Argument   | Default | What it does |
| ---------- | ------- | ------------ |
| `app`      |         | The FastAPI application. |
| `path`     | `"/docs"` | Where the page lives: `"docs"`, `"/docs"` and `"/docs/"` mean the same. |
| `title`    | `info.title` of the document | The name in the sidebar and in the browser tab. |
| `lang`     | the browser's language: Russian gives `"ru"`, anything else `"en"` | `"en"` or `"ru"`. The language the page opens in. |
| `theme`    | follows the system: `flame` (light) or `midnight` (dark) | The theme the page opens in. An unknown name falls back to the default. |
| `base_url` | see below | The address **Try it** sends requests to. An absolute URL with `http://` or `https://`. |

What it serves (for `path="/docs"`):

| URL                  | What |
| -------------------- | ---- |
| `/docs`              | Redirects to `docs/` (a relative redirect, so it works behind a proxy with a prefix). |
| `/docs/`             | The documentation page. |
| `/docs/openapi.json` | The OpenAPI document of the application. |
| `/docs/assets/…`     | The files of the page. |

These routes are left out of the OpenAPI document itself.

`lang` and `theme` are starting values. The reader can switch them on the page; that choice is saved in their
browser and wins from then on. Themes: light `flame`, `lavender`, `mint`, `serika`, `sky`; dark `midnight`,
`dracula`, `nord`, `gruvbox`, `catppuccin`.

### Where does Try it send requests?

1. `base_url`, if set. It replaces every `servers` entry of the document.
2. The `servers` of the document. Behind a proxy with `root_path` (`uvicorn --root-path /api`), FastAPI-style
   `root_path` is added there, and a relative entry is resolved against the address of the document.
3. The address the docs page is served from. Right when the API and the docs live in one application.

## Notes

- Authorization values typed into **Try it** stay in the browser's memory and are not saved.
- Browsers do not allow a page to set the `Cookie` header. For a cookie scheme the browser sends the stored cookies
  itself when the API is at the same address as the page and you are signed in.
- OAuth flows are not run: for oauth2 and OpenID Connect schemes, paste an access token you already have.
- Optional fields written the OpenAPI 3.1 way (`anyOf` with `null`), which is what FastAPI with Pydantic v2 emits,
  are shown as ordinary optional fields.

## License

Copyright 2026 laconal. Licensed under the Apache License, Version 2.0, see [LICENSE](LICENSE).
The licenses of the bundled third-party packages are in [THIRD_PARTY_LICENSES.md](THIRD_PARTY_LICENSES.md).
