Published
What you are building
A SvelteKit form action reads the file from the form, sends it to Fileboy on the server and returns the ID. The form works without JavaScript, and use:enhance makes it smoother with it.
1. Install and set your keys
npm install @cd2/fileboy-nodeimport { Fileboy } from "@cd2/fileboy-node"
import { env } from "$env/dynamic/private"
export const fileboy = Fileboy({
publicKey: env.FILEBOY_PUBLIC_KEY,
privateKey: env.FILEBOY_PRIVATE_KEY, // optional
})Anything under src/lib/server can't be imported from browser code, so the keys stay on the server.
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 { fail } from "@sveltejs/kit"
import type { Actions } from "./$types"
import { fileboy } from "$lib/server/fileboy"
export const actions: Actions = {
default: async ({ request }) => {
const form = await request.formData()
const file = form.get("avatar")
if (!(file instanceof File) || file.size === 0) {
return fail(400, { error: "Choose a file" })
}
try {
const uploaded = await fileboy.uploadFile(file)
// save uploaded.id against the user here
return { id: uploaded.id }
} catch (e) {
return fail(502, { error: e instanceof Error ? e.message : "The upload failed" })
}
},
}<script lang="ts">
import { enhance } from "$app/forms"
let { form } = $props()
</script>
<form method="POST" enctype="multipart/form-data" use:enhance>
<input type="file" name="avatar" accept="image/*" required />
<button>Upload</button>
{#if form?.error}<p role="alert">{form.error}</p>{/if}
{#if form?.id}<p>Saved as {form.id}</p>{/if}
</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 "./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" },
],
})
}With adapter-node, request bodies are limited to 512 kB by default: set BODY_SIZE_LIMIT to raise it, or upload big files from the browser with the Svelte uploaders. 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.