Backend Adapters
Express

Getting started with Express

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/express";
 
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;

Create an API route using the FileRouter

⚠️

Note: You need to serve this API from /api/uploadthing on your application.

src/index.ts
import express from "express";
 
import { createUploadthingExpressHandler } from "uploadthing/express";
 
import { uploadRouter } from "./uploadthing";
 
const app = express();
 
app.use(
  "/api/uploadthing",
  createUploadthingExpressHandler({
    router: uploadRouter,
  }),
);

Use the FileRouter in your app

Please refer to client side examples: