Published
What you are building
A FastAPI endpoint takes an UploadFile and hands its file object to Fileboy. The call returns the file's details, and the id is what you store.
1. Install and set your keys
pip install fileboy fastapi python-multipartimport os
import fileboy
client = fileboy.Client(
public_key=os.environ["FILEBOY_PUBLIC_KEY"],
private_key=os.environ.get("FILEBOY_PRIVATE_KEY"), # optional
)python-multipart is what FastAPI needs to read form uploads.
You need a public key. Create a free app and copy it from the Keys page. Keep keys in environment variables, and never put a private key in code that reaches the browser. An upload needs only the public key; add the private key only if your app uses signed requests.
2. Upload the file
import fileboy
from fastapi import FastAPI, HTTPException, UploadFile
from fileboy_client import client
app = FastAPI()
@app.post("/avatar")
def upload_avatar(avatar: UploadFile):
# A plain def runs in a thread pool, so the blocking upload doesn't hold up other requests
try:
file = client.upload_file(avatar.file, name=avatar.filename, mime=avatar.content_type)
except fileboy.FileboyError as error:
raise HTTPException(status_code=502, detail=str(error))
# save file.id against the user here
return {"id": file.id}curl -F "avatar=@./photo.jpg" http://localhost:8000/avatarStore the id the upload returns and nothing else: it is the only thing you need to show the file again, at any size.
3. Show it at the size you need
from fileboy_client import client
def avatar_url(file_id: str, size: int = 96) -> str:
return client.build_image_url(
file_id,
transforms=[
{"type": "resize", "width": size * 2, "height": size * 2, "fit": "cover"},
{"type": "format", "format": "webp"},
],
)Reading a file or image needs no key, so these URLs work in <img> tags, emails and anywhere else, unless the app requires signed URLs.
Checked against the current libraries in November 2026. Found something that no longer works? Tell us.