Adding an API to a site
A site can come with a small backend. Put it in a folder called api at the top of your site, and it answers under /api on the site's address and its own domains. Everything else is still served as static files.
FastAPI
my-site/
index.html
api/
main.py
requirements.txtapi/main.py must define app. Requests reach it with the path unchanged, so routes start with /api:
from fastapi import FastAPI
app = FastAPI()
@app.get("/api/hello")
def hello(name: str = "world"):
return {"message": f"Hello, {name}!"}api/requirements.txt lists your packages and must include uvicorn, which runs the app:
fastapi
uvicornYour page can then call it with fetch("/api/hello?name=Sam"). Same address, so there's no CORS to set up.
Node.js
Instead of main.py, add api/package.json with a start script. The server must listen on the port in the PORT environment variable and handle paths that start with /api.
Keys and passwords
Never put keys in your files: .env files inside api/ are removed on deploy. Add them on the site's API page instead. They're encrypted in your browser and given to the backend as environment variables.
How it runs
- Dependencies are installed fresh on every deploy, and the code and packages are checked like any upload.
- The backend sleeps after 10 minutes without requests. The next request wakes it, which takes a few seconds, so make startup quick.
- A new deploy replaces the backend. Rolling back restores the backend of that deploy.
- Pull request previews and per-deploy addresses serve the static files only.
/apiruns on the production address. - Anything the backend prints shows up live on the site's API page.
Limits
| Backends per account | One per site, and they share the account's 3 slots with bots. |
|---|---|
| Memory | 256 MB |
| CPU | Half a core |
| Requests | About 20 a second per site (bursts up to 40), 30 seconds each, bodies up to 10 MB |
| Files | The code is read-only. Write lasting files to /data (also in LODEBIT_DATA_DIR): 100 MB, kept while the backend sleeps and across deploys. Over the limit it turns read-only until you reset it on the site's API page. It isn't backed up yet. /tmp (64 MB) is emptied whenever the backend sleeps. |
| Not supported | WebSockets, background jobs that outlive a request, and scheduled tasks |
Troubleshooting
- The deploy failed with a message about
api/. It says exactly which file is missing. The usual one isuvicorninrequirements.txt. /apianswers 502. The backend crashed or didn't start listening in time. The site's API page shows its output./apianswers 404 from FastAPI. The route needs the/apiprefix, for example@app.get("/api/hello").