Embedding in FastAPI¶
Standalone app¶
create_app returns a configured FastAPI app with the router mounted at
/v1:
import uvicorn
from mstransfer import create_app
app = create_app(output_dir="/data/ms", store_as="mzml")
uvicorn.run(app, host="0.0.0.0", port=1319)
| Argument | Default | Description |
|---|---|---|
output_dir |
./received |
Where uploads are stored (created if missing) |
store_as |
"msz" |
"msz" or "mzml" |
auth |
None |
An AuthProvider, a raw FastAPI dependency, or None for no auth |
file_provider |
None |
A FileProvider; defaults to scanning output_dir |
upload_auth |
None |
Like auth, but only for upload and transfer status (keyword-only) |
download_auth |
None |
Like auth, but only for file list and download (keyword-only) |
Mounting as a sub-application¶
from fastapi import FastAPI
from mstransfer import create_app
main_app = FastAPI()
main_app.mount("/transfer", create_app(output_dir="/data/ms"))
# Endpoints are now at /transfer/v1/...
Including the router¶
To share your app's middleware, dependencies, and OpenAPI schema, include
the router directly. Pass an AppState so the router does not depend on
your app's app.state:
from pathlib import Path
from fastapi import FastAPI
from mstransfer import AppState, make_router
from mstransfer.server.fileprovider import DirectoryFileProvider
out = Path("/data/ms")
out.mkdir(parents=True, exist_ok=True)
state = AppState(output_dir=out, store_as="msz", files=DirectoryFileProvider(out))
app = FastAPI()
app.include_router(make_router(state=state), prefix="/transfer/v1")
make_router(auth_dep=...) takes a FastAPI dependency to protect every route
except /health. To protect only one direction, pass upload_auth_dep
(upload and transfer status) and/or download_auth_dep (file list and
download) instead; a group without a dependency is left open:
from mstransfer import APIKeyAuthProvider, make_auth_dependency
auth = make_auth_dependency(APIKeyAuthProvider("s3cret"))
app.include_router(
make_router(state=state, upload_auth_dep=auth), prefix="/transfer/v1"
)
auth_dep can't be combined with the per-direction arguments. See
Authentication.
Uploads are written to state.output_dir regardless of the file provider,
so create that directory before serving.