Published
What you are building
A form posts a file to a server action. The action sends it to Fileboy and returns the file's ID. There is no upload route to write and no storage to set up.
1. Install and set your keys
npm install @cd2/fileboy-nodeimport { Fileboy } from "@cd2/fileboy-node"
export const fileboy = Fileboy({
publicKey: process.env.FILEBOY_PUBLIC_KEY!,
privateKey: process.env.FILEBOY_PRIVATE_KEY, // optional
})FILEBOY_PUBLIC_KEY=your_public_keyYou 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
A server action gets the form as FormData, and the file in it is a web File, which uploadFile takes as it is and uses its name and type.
"use server"
import { fileboy } from "@/lib/fileboy"
export async function uploadAvatar(_previous: unknown, formData: FormData) {
const file = formData.get("avatar")
if (!(file instanceof File) || file.size === 0) {
return { error: "Choose a file" }
}
try {
const uploaded = await fileboy.uploadFile(file)
// save uploaded.id against the user here, then:
return { id: uploaded.id }
} catch (e) {
return { error: e instanceof Error ? e.message : "The upload failed" }
}
}"use client"
import { useActionState } from "react"
import { uploadAvatar } from "./actions"
export function AvatarForm() {
const [state, action, pending] = useActionState(uploadAvatar, null)
return (
<form action={action}>
<input type="file" name="avatar" accept="image/*" required />
<button disabled={pending}>{pending ? "Uploading..." : "Upload"}</button>
{state?.error && <p role="alert">{state.error}</p>}
{state?.id && <p>Saved as {state.id}</p>}
</form>
)
}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
import { fileboy } from "@/lib/fileboy"
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" },
],
})
}To serve the image through next/image instead, pass the file ID as its src with the loader in Next.js image CDN with Fileboy: the loader builds the URL for each width.
Server actions accept request bodies of up to 1 MB by default. Raise serverActions.bodySizeLimit under experimental in next.config, or for big files upload from the browser with the uploader components and a token from your server. 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.