← Home

fastAPI things

23 March, 2025 - Categories: python

doing some experiments with fastAPI.

important findings:

if you use uv (as you should) and create a project and run

uv add fastapi ---extra standard

there will be a convenient command "fastapi" available. start your server with: uv run fastapi dev --port 0000

(port is like 8000 by default but it was in use on my server)

if you're in the project you've created with uv, and have fastapi installed, uv will know to look in the current project and use fastapi's CLI to run your server - very convenient, no need to mess around with uvicorn manually.

note that the dev server listens only on localhost by default. i'm running my reverse proxy on the same server so i was able to use 127.0.0.1 in it's Caddyfile and it works.

also: uv's docs contain some info about fastAPI. if following it, ensure you actually copy the code from the example project.

However, i'd argue when just getting started the whole "app" directory layout and setup is overkill - fastAPI itself say it is only for large projects.

another small learn: by default, fastAPI seems to expect to run right on the domain - so it serves docs on api.shiny.space/docs or similar.

In my case, i use caddy to point someservice.shiny.space to a normal web directory with html files, and only paths such as someservice.shiny.space/api/ get proxied to the fastAPI server.

To make fastAPI understand that, in the main.py file that gets started for the server, you can modify the app= variable. by default it's: app = FastAPI() change it to app = FastAPI(root_path="/api") and fastAPI will serve docs at someservice.shiny.space/api/docs.