Guide

Upload files in Nuxt with a server route

A Nitro route that reads the multipart body and sends the file to Fileboy, and a page that posts to it.

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

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-node
server/utils/fileboy.ts
import { 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

server/api/avatar.post.ts
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",
    })
  }
})
pages/avatar.vue
<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

server/utils/avatar.ts
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.