DEV Community

ShardCloud
ShardCloud

Posted on AI-assisted

What a FastAPI App Needs Before You Deploy It (with a ShardCloud Walkthrough)

You run python -m uvicorn main:app, open /docs, and try an endpoint. Everything works. Then you deploy the same app and the public URL returns 502 Bad Gateway. The startup log might even say Uvicorn is running. The next thing to check is the address and port in that log.

This walkthrough takes a small FastAPI app from a local virtual environment to a public URL on ShardCloud. Along the way, you'll see how to tell a Python startup error from a server the platform can't reach.

What to check before deploying

Make sure the entry point starts a server. app = FastAPI() creates the application object. Uvicorn serves it over HTTP. The command python -m uvicorn main:app imports app from main.py and starts the server. If your platform runs python main.py directly, the file needs to start Uvicorn itself. FastAPI deployment docs

Listen where the proxy can reach you. By default, Uvicorn listens on 127.0.0.1:8000. That works in your browser locally. Inside a container, 127.0.0.1 is reachable only within that container. Bind to 0.0.0.0 and read the port from the environment so the platform's proxy can connect. Uvicorn uses UVICORN_PORT for its own environment setting, so the example reads the platform's PORT explicitly. Uvicorn settings

Install from requirements.txt in a fresh environment. Include both FastAPI and the server you use to run it. If your app only works in a virtual environment you've been using for months, it may rely on packages you installed along the way. A fresh environment catches those missing dependencies before upload.

Build and run the sample locally

Create a folder named hello-fastapi. In it, save these two files. You'll need Python 3.11 or newer to run the example and a ShardCloud account for the deployment step.

requirements.txt

fastapi
uvicorn
Enter fullscreen mode Exit fullscreen mode

main.py

import os

import uvicorn
from fastapi import FastAPI

app = FastAPI(title="Hello FastAPI")


@app.get("/")
def hello():
    return {"message": "Hello from FastAPI"}


@app.get("/health")
def health():
    return {"status": "ok"}


if __name__ == "__main__":
    port = int(os.environ.get("PORT", "8000"))
    uvicorn.run(app, host="0.0.0.0", port=port)
Enter fullscreen mode Exit fullscreen mode

The if __name__ == "__main__" block runs when Python executes the file directly. If another tool imports app, the block is skipped.

On macOS or Linux, run these commands from the project folder.

python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python main.py
Enter fullscreen mode Exit fullscreen mode

On Windows, use PowerShell.

py -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
.\.venv\Scripts\python.exe main.py
Enter fullscreen mode Exit fullscreen mode

Open http://127.0.0.1:8000/health to see {"status":"ok"}. Then open http://127.0.0.1:8000/docs and try GET / in FastAPI's interactive API docs.

Deploy to ShardCloud

Add a file named .shardcloud next to main.py and requirements.txt.

DISPLAY_NAME=Hello FastAPI
MAIN=main.py
MEMORY=512
VERSION=recommended
SUBDOMAIN=yourfastapiapp
Enter fullscreen mode Exit fullscreen mode

MAIN=main.py tells ShardCloud which Python file to execute. ShardCloud installs the packages listed in requirements.txt and sets PORT=80 in its Python runtime. The same code listens on port 8000 locally when PORT is unset. Replace yourfastapiapp with an available name using letters and numbers. ShardCloud Python hosting guide ยท ShardCloud config reference

  1. Put main.py, requirements.txt, and .shardcloud at the root of a ZIP file. Include the dotfile explicitly and leave .venv/, .env, and __pycache__/ out of the archive.
  2. In the ShardCloud dashboard, go to Applications, choose New app, and upload the ZIP.
  3. Open the app logs and look for Uvicorn listening on 0.0.0.0:80.
  4. Open /health on your app's https://<subdomain>.shardweb.app address and check for HTTP 200 with {"status":"ok"}. Then open /docs and try GET / again to confirm requests reach the deployed app.

If the deploy fails, start with the logs

What you see What to check
The process exits without a Uvicorn startup line. Check that MAIN points to the script that calls uvicorn.run(). A file containing only routes and app = FastAPI() finishes as soon as Python reaches the end.
ModuleNotFoundError for fastapi or uvicorn. Make sure the package is in requirements.txt at the root of the ZIP. Check the install log for dependency errors.
Uvicorn reports that it can't import main or find app. If you're starting Uvicorn with a command such as python -m uvicorn main:app, run it from the project root and check that the module and application object match the import string.
Uvicorn starts, but the public URL returns 502. Look for 0.0.0.0:80 in the startup log. A server on 127.0.0.1 or port 8000 can be up while the ShardCloud proxy still can't connect.

If you're deploying an app that needs a database URL or API key, set it in the app's environment and read it with os.environ. Keep /health lightweight. In this sample, a 200 confirms the API process is reachable. An app that needs to check whether its database is ready can use a separate readiness endpoint.

Top comments (0)