Backend Adapters
Fastify

Getting started with Fastify

Added in v5.6

Package Setup

Install the package

npm install uploadthing

Add env variables

💡

If you don't already have a uploadthing secret key, sign up (opens in a new tab) and create one from the dashboard! (opens in a new tab)

UPLOADTHING_SECRET=... # A secret key for your app (starts with sk_live_)
UPLOADTHING_APP_ID=... # Your app id

Set Up A FileRouter

Creating your first FileRoute

All files uploaded to uploadthing are associated with a FileRoute. Following example is very minimalistic. To get full insight into what you can do with the FileRoutes, please refer to the File Router API.

src/uploadthing.ts
import { createUploadthing, type FileRouter } from "uploadthing/fastify";
 
const f = createUploadthing();
 
export const uploadRouter = {
  videoAndImage: f({
    image: {
      maxFileSize: "4MB",
      maxFileCount: 4,
    },
    video: {
      maxFileSize: "16MB",
    },
  }).onUploadComplete((data) => {
    console.log("upload completed", data);
  }),
} satisfies FileRouter;
 
export type OurFileRouter = typeof uploadRouter;

Register the UploadThing plugin

src/index.ts
import Fastify from "fastify";
 
import { fastifyUploadthingPlugin } from "uploadthing/fastify";
 
import { uploadRouter } from "./router";
 
const fastify = Fastify({ logger: true });
 
fastify
  /** ... */
  .register(fastifyUploadthingPlugin, {
    router: uploadRouter,
  });

Use the FileRouter in your app

Please refer to client side examples: