Guide

Upload files in Flask and send them to Fileboy

Read request.files in a view and hand the stream 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 Flask view reads the file from request.files and hands its stream 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 flask
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
)

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

app.py
import fileboy
from flask import Flask, jsonify, request

from fileboy_client import client

app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 20 * 1024 * 1024  # refuse bodies over 20 MB with a 413


@app.post("/avatar")
def upload_avatar():
    avatar = request.files.get("avatar")
    if avatar is None or not avatar.filename:
        return jsonify(error="Choose a file"), 400
    try:
        file = client.upload_file(avatar.stream, name=avatar.filename, mime=avatar.mimetype)
    except fileboy.FileboyError as error:
        return jsonify(error=str(error)), 502
    # save file.id against the user here
    return jsonify(id=file.id)
curl -F "avatar=@./photo.jpg" http://localhost:5000/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, size=96):
    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.