Published
What you are building
The page posts a file to a Nitro server route. The route reads the multipart body, sends the file to Fileboy and returns the ID.
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
})Nuxt auto-imports what is exported from server/utils, so the routes below use fileboy without an import.
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
export default defineEventHandler(async event => {
const parts = await readMultipartFormData(event)
const part = parts?.find(p => p.name === "avatar" && p.filename)
if (!part) {
throw createError({ statusCode: 400, statusMessage: "Choose a file" })
}
try {
const uploaded = await fileboy.uploadFile(part.data, {
name: part.filename,
mime: part.type,
})
// save uploaded.id against the user here
return { id: uploaded.id }
} catch (e) {
throw createError({
statusCode: 502,
statusMessage: e instanceof Error ? e.message : "The upload failed",
})
}
})<script setup lang="ts">
const id = ref<string>()
const error = ref<string>()
async function upload(event: Event) {
const file = (event.target as HTMLInputElement).files?.[0]
if (!file) return
const body = new FormData()
body.append("avatar", file)
try {
const result = await $fetch<{ id: string }>("/api/avatar", { method: "POST", body })
id.value = result.id
} catch (e: any) {
error.value = e?.statusMessage ?? "The upload failed"
}
}
</script>
<template>
<input type="file" accept="image/*" @change="upload" />
<p v-if="error" role="alert">{{ error }}</p>
<p v-if="id">Saved as {{ id }}</p>
</template>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
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.