Python2 minutes to read

How do I host a FastAPI app with Uvicorn?

Deploy a small FastAPI route on Bothosting, set the Python module and application object correctly, and test both valid and invalid input over HTTPS.

Give Uvicorn an application to import

Choose Python web app on Bothosting and run Uvicorn with the module path for your FastAPI application. If main.py contains app = FastAPI(), the target is main:app. Bind to 0.0.0.0 on SERVER_PORT so requests from the platform address reach your server. Python web deployment settings.

For a test you can repeat after every release, define a route with an integer parameter. A valid request will show that the app loaded, and an invalid one will exercise parameter validation. FastAPI path parameters.

main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/orders/{order_id}")
def read_order(order_id: int):
    return {"order": order_id}

Install the application and server packages

Put fastapi and uvicorn on separate lines in requirements.txt. When moving an existing app, use the package versions you tested together. Upload both files to the deployment root. Set RUN_COMMAND in Variables, save and start the deployment.

RUN_COMMAND for main.py
python -m pip install -r requirements.txt && python -m uvicorn main:app --host 0.0.0.0 --port "$SERVER_PORT"

If Console reports that it cannot import the module, compare the filename and the object name with the target. Moving the same code to service.py changes the target to service:app. Check the last installation message as well, since the server cannot import a dependency that failed to install.

Verify the deployed route

Open the HTTPS address in Settings with /orders/7 appended. Expect HTTP 200 and {"order":7}. Try /orders/blue next. This example should return 422 because the route requires an integer. If the first request returns 404, check whether you requested the correct path and deployed the expected file. The example defines no handler at the root URL.