Documentation

Guides and API reference for uploads, image transforms and video.

npm install @cd2/fileboy-react
API https://api.fileboy.io

Svelte integration

Drop-in file uploaders and responsive images for Svelte 5 and SvelteKit, with the same options and behaviour as the React package.

Installation

1
npm install @cd2/fileboy-svelte

Call setFileboy in a parent component, like your root layout:

1
2
3
4
5
6
7
8
<!-- src/routes/+layout.svelte -->
<script lang="ts">
  import { setFileboy } from "@cd2/fileboy-svelte"
  let { children } = $props()
  setFileboy({ publicKey: "YOUR_PUBLIC_KEY", uploadToken: "/api/fileboy/token" })
</script>

{@render children()}

uploadToken is optional: see Upload tokens below. <FileboyProvider publicKey="..."> does the same in markup, and getFileboy() returns the client (uploadFile, buildImageUrl, getFileData...) while a component initialises.

Uploaders

<FileUploader> holds one file and <MultiFileUploader> many. Both have a drop zone, and bind:value holds the file id (or ids).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script lang="ts">
  import { FileUploader, MultiFileUploader } from "@cd2/fileboy-svelte"
  let avatar = $state<string>()
  let photos = $state<string[]>([])
</script>

<FileUploader bind:value={avatar} allow="image/*" capture="user" onUploaded={file => save(file)} />
<MultiFileUploader
  bind:value={photos}
  paste
  urlImport
  folderPath="/photos"
  uploaderStyle="buttonWithProgress"
  onRemove={id => forget(id)}
/>

Props

allow string | string[], optional

Mime types and extensions allowed, like "image/*" or ["image/*", ".pdf"]. Other files are refused with "Disallowed file given" before anything uploads. It also sets the file picker's accept.

uploaderStyle "default" | "minimal" | "buttonOnly" | "buttonWithProgress", optional, default is "default"

Which built-in look to use. buttonWithProgress is for the multi-uploader.

folderId / folderPath string, optional

Upload into this folder. With the public key the folder must already exist: create it from your server.

fileName / metadata (info) => string / (info) => Record<string, string>, optional

The name and metadata to save each file with. info has file, rawName, baseName, ext and index.

paste boolean, optional

Upload files pasted anywhere on the page, like a screenshot. Pasted screenshots get a name with the time in it. Text pastes are left alone. Turn it on for one uploader per page.

capture boolean | "user" | "environment", optional

On phones, open the camera instead of the file picker: "environment" is the back camera, "user" the front one. Desktop browsers show the file picker.

urlImport boolean, optional

Show a field to import a file from a link. Fileboy fetches it, so it must be public. Links to localhost and private or cloud metadata addresses are refused, in the browser and again by the API on the address it connects to.

uploadToken string | () => Promise<string>, optional

Get a short-lived upload token before each upload: the URL of an endpoint on your server, or a function. Overrides the one given to the provider. See Upload tokens below.

theme { colors?: { button?: string, buttonText?: string }, fontFamily?: string }, optional

Colours and font for the built-in looks.

disabled boolean, optional

Stop new uploads.

namespace string, optional

Which provider to use, when the page has more than one app's key.

onUploaded (file: IFileData) or (files: IFileData[]), optional

Once a file, or every file of a batch, has uploaded.

onUploadStart / onUploadEnd () => void, optional

Around each upload, whether it worked or not.

onRemove (id: string) => void, optional

Giving it shows a remove button on each file. The file stays in Fileboy.

Your own look

A children snippet replaces the built-in look and gets the uploader's state: ids, files, error, isUploading, progress, dragging, openFileDialogue(), uploadFiles(files), uploadUrl(url), remove(id) and dropZone, the handlers for a drop target.

1
2
3
4
5
6
7
<MultiFileUploader>
  {#snippet children(u)}
    <button {...u.dropZone} class:over={u.dragging} onclick={u.openFileDialogue}>
      {u.isUploading ? `${Math.round(u.progress?.percent ?? 0)}%` : "Drop files or click"}
    </button>
  {/snippet}
</MultiFileUploader>

Images

<Image> (also exported as FileboyImage) builds the image URL. Give widths and sizes for a srcset, a saved preset, and placeholderColor (a colour, or "auto" for the image's own). Other attributes go on the <img>.

1
2
3
4
5
6
7
8
9
<Image
  id={avatar}
  preset="card"
  transforms={[{ type: "format", format: "auto" }]}
  widths={[320, 640, 1280]}
  sizes="(max-width: 640px) 100vw, 640px"
  placeholderColor="auto"
  alt="Profile photo"
/>

Upload tokens

The uploaders upload straight from the browser with your public key, and allowed origins keep other websites from using it. To have your server decide who uploads, have it hand out a short-lived upload token, made with the private key, and give the provider its URL. The uploaders POST to it (with the page's cookies) before each upload and expect { "token": "..." } back.

1
2
3
4
5
6
7
8
// app/api/fileboy/token/route.ts (Next.js), or any server route
import { createUploadTokenHandler } from "@cd2/fileboy-next"

export const runtime = "nodejs"
export const POST = createUploadTokenHandler({
  // publicKey and privateKey default to FILEBOY_PUBLIC_KEY and FILEBOY_PRIVATE_KEY
  authorize: async request => isSignedIn(request),
})

Tokens last 60 seconds and only allow uploads. Without Next.js, make one with fileboy.createAccessToken({ upload: true }) from the Node library. In SvelteKit the handler above works in a +server.ts too: export const POST = ({ request }) => handler(request).

Files uploaded with a token are signed (signed: true). A token doesn't turn the public key off, so anyone with it can still upload unsigned files. To keep only files your users uploaded, check one is signed on your server before you save its id:

1
2
const file = await fileboy.fetchFileData(id) // @cd2/fileboy-node, with the private key
if (!file.signed) throw new Error("Upload with a token")

Related guides