Skip to main content

FastAPI

Setting up a FastAPI Live App on MINEO is an efficient way to deploy highly performant web applications. Follow the steps below to create your own FastAPI Live App:

Step 1: Create the main.py File​

Create a file named main.py in your project directory. Keep in mind that paths in the entrypoint command are relative to the Live App's folder. This file will contain your FastAPI application code. Here's a straightforward example:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List


app = FastAPI()

items = [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]

class Item(BaseModel):
id: int
name: str

@app.get("/", tags=["Default"])
def read_root():
return {"message": f"Welcome to my FastAPI application"}

@app.get("/items", response_model=List[Item], tags=["Items"])
def get_items():
return items

@app.get("/items/{item_id}", response_model=Item, tags=["Items"])
def get_item(item_id: int):
for item in items:
if item["id"] == item_id:
return item
raise HTTPException(status_code=404, detail="Item not found")

@app.post("/items", response_model=Item, tags=["Items"], status_code=201)
def create_item(item: Item):
if any(existing_item["id"] == item.id for existing_item in items):
raise HTTPException(
status_code=400, detail="Item with this ID already exists"
)
items.append(item.dict())
return item

@app.delete("/items/{item_id}", status_code=204, tags=["Items"])
def delete_item(item_id: int):
for item in items:
if item["id"] == item_id:
items.remove(item)
return
raise HTTPException(status_code=404, detail="Item not found")

This example sets up a basic FastAPI app with a root endpoint that returns a simple message.

Write your routes exactly as you would locally. The --root-path flag in the entrypoint (Step 2) is what makes /docs, url_for() and mounted static files work behind MINEO's proxy — see Static files and the path prefix.

Step 2: Set Up a New Live App​

Next, you need to configure a new Live App on MINEO and define an entrypoint command to run your application.

Here's the entrypoint command structure:

pip install -q --no-cache-dir fastapi uvicorn && uvicorn main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips='*' --root-path "${MINEO_LIVE_APP_URL_PATH%/}"

Adding Dependencies​

If your application requires additional packages, use one of these methods to include them:

  1. Direct pip install: Add necessary packages directly into the entrypoint command.
pip install -q --no-cache-dir fastapi uvicorn pydantic && uvicorn main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips='*' --root-path "${MINEO_LIVE_APP_URL_PATH%/}"
  1. Using requirements.txt: List your dependencies in a requirements.txt file and reference it in the entrypoint command.
pip install -q --no-cache-dir -r requirements.txt && uvicorn main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips='*' --root-path "${MINEO_LIVE_APP_URL_PATH%/}"

Example requirements.txt:

fastapi
uvicorn
pydantic

Configuration Needed to Share Your App​

Important Launch Settings​

MINEO serves every Live App behind a reverse proxy at the path stored in MINEO_LIVE_APP_URL_PATH (for example /live-app/your-org/your-app/). The proxy strips that prefix before the request reaches your app, so configure these uvicorn flags in the entrypoint to keep routing, HTTPS redirects, and /docs working:

FlagWhy it matters
--host 0.0.0.0Accept external connections.
--port 8000The port MINEO routes to.
--proxy-headers --forwarded-allow-ips='*'Trust the proxy so redirects and absolute URLs use https instead of http.
--root-path "${MINEO_LIVE_APP_URL_PATH%/}"Tells the app the public prefix, so /docs, openapi.json and url_for() generate URLs that include it. ${...%/} strips the trailing slash, which --root-path must not have.

Static files and the path prefix​

Mount static files as you would locally. With --root-path in the entrypoint, uvicorn puts the prefix back in front of every request path, so mounts, /docs and url_for() all work:

from fastapi.staticfiles import StaticFiles

app = FastAPI()
app.mount("/assets", StaticFiles(directory="assets"), name="assets")

What --root-path does not change is a URL you write yourself: a link, a <form action> or a RedirectResponse that starts with / leaves the app. Build those from MINEO_LIVE_APP_URL_PATH.

Common mistakes
  • Do not pass root_path= to the FastAPI() constructor. It overwrites whatever --root-path set, so a wrong value there silently beats the correct one from your entrypoint. Set the prefix in the entrypoint only.
  • Do not pin an old uvicorn. --root-path puts the prefix back since uvicorn 0.26; with an older one (a pinned requirements.txt, a custom image) mounted static files answer 404.
  • MINEO_LIVE_APP_URL_PATH ends with a slash and root_path must not, which is why the entrypoint uses ${...%/}.

Troubleshooting​

SymptomCause
/docs loads but stays emptySwagger is requesting /openapi.json at the site root. Add --root-path to the entrypoint.
Images and CSS return 404, the API worksMissing --root-path in the entrypoint, or the HTML links them with absolute paths (/assets/…), so the browser never sends the prefix. Prepend MINEO_LIVE_APP_URL_PATH, or use url_for(). Relative paths (assets/…) work only from the app's root page.
A <form action="/…"> or your own RedirectResponse("/…") lands on a MINEO error pageA URL you write starting with / leaves the app. Build it from MINEO_LIVE_APP_URL_PATH, for example RedirectResponse(os.environ["MINEO_LIVE_APP_URL_PATH"], status_code=303).
Form data requires "python-multipart" to be installed in the logsFastAPI's Form and UploadFile need python-multipart. Add it to requirements.txt.

Launch and Share Your Live App​

Once your main.py and entrypoint command are set up, click the Execute button. This will deploy your FastAPI application, making it accessible.

Share your Live App URL and develop in a reliable and secure environment on the MINEO platform. Enjoy your FastAPI Live App!