Guide

Upload files in SvelteKit with a form action

A form action that sends the file to Fileboy on the server, with a form that works without JavaScript.

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

src/routes/avatar/+page.server.ts
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" })
    }
  },
}
src/routes/avatar/+page.svelte
<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

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

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.