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.

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 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 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 -r requirements.txt --no-cache-dir && 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%/}"Re-adds the public prefix to redirects (e.g. /items//items) and to the /docs and openapi.json links. ${...%/} strips the trailing slash, which --root-path must not have.

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!