Guide

Upload files in FastAPI and send them to Fileboy

Take an UploadFile in an endpoint and hand it to the Python package. Keep the returned ID.

Uploads guide · in this guide

  • What you are building
  • 1. Install and set your keys
  • 2. Upload the file
  • 3. Show it at the size you need

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-multipart
fileboy_client.py
import 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

main.py
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/avatar

Store 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

avatars.py
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.

Still choosing a tool?

Fileboy is small on purpose. This comparison says where it fits and where another option is the better call.

Get a key and run this code

Create a free app, copy your keys into the example and run it. The free plan needs no card.