Guide

Upload files in Remix with an action

Read the form in a route action, send the file to Fileboy, and show a resized image from the stored 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 Remix route's action reads the form, sends the file to Fileboy and returns its ID. In React Router 7's framework mode the same steps work, but import from react-router and return a plain object (or data()) in place of json().

1. Install and set your keys

npm install @cd2/fileboy-node
app/fileboy.server.ts
import { Fileboy } from "@cd2/fileboy-node"

export const fileboy = Fileboy({
  publicKey: process.env.FILEBOY_PUBLIC_KEY!,
  privateKey: process.env.FILEBOY_PRIVATE_KEY, // optional
})

The .server.ts name keeps the file, and your keys, out of the browser bundle.

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/routes/avatar.tsx
import type { ActionFunctionArgs } from "@remix-run/node"
import { json } from "@remix-run/node"
import { Form, useActionData } from "@remix-run/react"
import { fileboy } from "~/fileboy.server"

export async function action({ request }: ActionFunctionArgs) {
  const form = await request.formData()
  const file = form.get("avatar")
  if (!(file instanceof File) || file.size === 0) {
    return json({ error: "Choose a file" }, { status: 400 })
  }
  try {
    const uploaded = await fileboy.uploadFile(file)
    // save uploaded.id against the user here
    return json({ id: uploaded.id })
  } catch (e) {
    return json({ error: e instanceof Error ? e.message : "The upload failed" }, { status: 502 })
  }
}

export default function Avatar() {
  const data = useActionData<typeof action>()
  return (
    <Form method="post" encType="multipart/form-data">
      <input type="file" name="avatar" accept="image/*" required />
      <button>Upload</button>
      {data && "error" in data && <p role="alert">{data.error}</p>}
      {data && "id" in data && <p>Saved as {data.id}</p>}
    </Form>
  )
}

request.formData() holds the whole upload in memory. That suits images and documents. For files of hundreds of megabytes, upload from the browser instead, with the uploader components and a token from your server.

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

app/avatar.server.ts
import { fileboy } from "~/fileboy.server"

export function avatarUrl(id: string, size = 96) {
  return fileboy.buildImageUrl(id, {
    public: true, // no 10-second private-key token in a URL that goes into a page
    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.