Skip to content

✨ Add public-url option and env var#381

Open
YuriiMotov wants to merge 4 commits intomainfrom
public_url-option
Open

✨ Add public-url option and env var#381
YuriiMotov wants to merge 4 commits intomainfrom
public_url-option

Conversation

@YuriiMotov
Copy link
Copy Markdown
Member

@YuriiMotov YuriiMotov commented Apr 24, 2026

fastapi run command output includes URLs of server and docs:

    server   Server started at http://0.0.0.0:8000
    server   Documentation at http://0.0.0.0:8000/docs

The 0.0.0.0 in URL here doesn't make sense - links don't work..

This PR adds --public-url option and FASTAPI_PUBLIC_URL env variable to configure the base of these URLs:

$ fastapi run --public-url https://myapp.example.com/api

    server   Server started at https://myapp.example.com/api
    server   Documentation at https://myapp.example.com/api/docs

We can also describe this config option in the readme:

Details


## Public URL

When you run behind a reverse proxy or in a container - typically when binding to `0.0.0.0` - the URLs printed at startup (`http://0.0.0.0:8000/docs`) aren't the URLs that users actually use to reach the server.

You can use `--public-url` (or the `FASTAPI_PUBLIC_URL` environment variable) to tell the CLI which URL to display:

<div class="termy">

```console
$ fastapi run main.py --public-url https://api.example.com

   FastAPI   Starting production server 🚀

             ...

    server   Server started at https://api.example.com
    server   Documentation at https://api.example.com/docs
```

</div>

This flag is display-only - it doesn't change the host, port, or routing. Set it to the address your users (or your proxy's public frontend) actually reach.

@YuriiMotov YuriiMotov added the feature New feature or request label Apr 24, 2026
@YuriiMotov YuriiMotov marked this pull request as ready for review April 24, 2026 08:56
@YuriiMotov YuriiMotov requested a review from Copilot April 24, 2026 09:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a CLI-level way to override the “public” base URL shown in fastapi dev / fastapi run startup output, so the printed server/docs links are usable when binding to 0.0.0.0 or running behind a proxy.

Changes:

  • Add --public-url option (and FASTAPI_PUBLIC_URL env var) to fastapi dev and fastapi run.
  • Use public_url (normalized for trailing /) to build the displayed server/docs URLs.
  • Add CLI tests and help-output assertions covering the new option/env var.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/fastapi_cli/cli.py Adds public_url plumbing and uses it to form the displayed startup URLs; exposes the option/env var on dev and run.
tests/test_cli.py Adds tests for --public-url and FASTAPI_PUBLIC_URL, plus help text assertions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/fastapi_cli/cli.py
)

url = f"http://{host}:{port}"
url = public_url.rstrip("/") if public_url else f"http://{host}:{port}"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can ignore this for now and add such guard if user report they need this (I think they will not)

Comment thread src/fastapi_cli/cli.py Outdated
Comment thread src/fastapi_cli/cli.py Outdated
Comment thread tests/test_cli.py
Comment thread tests/test_cli.py
Comment thread src/fastapi_cli/cli.py
Comment on lines +193 to 195
url = public_url.rstrip("/") if public_url else f"http://{host}:{port}"
url_docs = f"{url}/docs"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this

YuriiMotov and others added 2 commits April 24, 2026 12:46
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants