Documentation

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

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

Next.js integration

Route handlers for the App Router that check webhooks and hand out upload tokens, and the React uploaders ready for Server Component pages.

Installation

1
npm install @cd2/fileboy-next @cd2/fileboy-node @cd2/fileboy-react

Put your keys in .env.local. Only the public key may reach the browser:

1
2
3
NEXT_PUBLIC_FILEBOY_PUBLIC_KEY=...
FILEBOY_PRIVATE_KEY=...
FILEBOY_WEBHOOK_SECRET=...

The handlers need the Node.js runtime (the Node library signs with node:crypto), so each route file sets export const runtime = "nodejs".

Webhooks

1
2
3
4
5
6
7
8
9
10
11
12
13
// app/api/fileboy/webhook/route.ts
import { createWebhookHandler } from "@cd2/fileboy-next"

export const runtime = "nodejs"

export const POST = createWebhookHandler({
  on: {
    "file.uploaded": async event => {
      await db.files.create({ id: event.resource.id, name: event.resource.name })
    },
    "video.ready": async event => markReady(event.resource.id),
  },
})

It reads the raw body (parsing the JSON first would break the signature), checks the signature and timestamp, runs onEvent and the handler for the event's type, then answers 200. A bad signature or an old timestamp gets a 400 with the reason. If a handler throws it answers 500, so Fileboy retries the delivery: retries keep the event's id, so use it to skip an event you have already handled. Events are typed: event.resource is a file or a video to suit the type.

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.

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")

authorize is required, so the endpoint can't be left open by mistake, and only a real true lets a request through. Then point the provider at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// app/upload/page.tsx, a Server Component
import { FileboyProvider } from "@cd2/fileboy-next/client"
import { Uploader } from "./Uploader"

export default function Page() {
  return (
    <FileboyProvider
      publicKey={process.env.NEXT_PUBLIC_FILEBOY_PUBLIC_KEY}
      uploadToken="/api/fileboy/token"
    >
      <Uploader />
    </FileboyProvider>
  )
}

Client components

@cd2/fileboy-next/client re-exports the React package's components from a "use client" module, so a Server Component can render them without a wrapper file. Callbacks can't be passed from a Server Component, so put an uploader that has them in a client component of your own:

1
2
3
4
5
6
7
8
// app/upload/Uploader.tsx
"use client"
import { MultiFileUploader } from "@cd2/fileboy-next/client"
import { saveFiles } from "./actions"

export function Uploader() {
  return <MultiFileUploader paste urlImport onFilesUploaded={files => saveFiles(files.map(f => f.id))} />
}

Every option of the React uploaders works, including paste, capture and urlImport.

Other frameworks

The handlers take a Web Request and return a Response, so they work wherever routes do: Remix, Astro, Hono, or SvelteKit with export const POST = ({ request }) => handler(request). The Pages Router's API routes use Node's request instead: use verifyWebhook from the Node library there.

Related guides