UploadThing
Overview
UploadThing is a file upload service for TypeScript apps. Define upload routes on the server (with auth, file type, and size validation), get pre-built React components for the frontend. Files go to S3-compatible storage. No infrastructure to manage — just define what's allowed and upload.
Instructions
Step 1: Setup
npm install uploadthing @uploadthing/react
Step 2: Server Routes
// server/uploadthing.ts — Define upload routes
import { createUploadthing, type FileRouter } from 'uploadthing/server'
import { getSession } from '@/lib/auth'
const f = createUploadthing()
export const uploadRouter = {
// Avatar upload: max 2MB image, authenticated users only
avatarUploader: f({ image: { maxFileSize: '2MB', maxFileCount: 1 } })
.middleware(async ({ req }) => {
const session = await getSession(req)
if (!session) throw new Error('Not authenticated')
return { userId: session.user.id }
})
.onUploadComplete(async ({ metadata, file }) => {
console.log(`Avatar uploaded for user ${metadata.userId}: ${file.url}`)
await db.user.update({
where: { id: metadata.userId },
data: { avatarUrl: file.url },
})
return { url: file.url }
}),
// Document upload: max 10MB, multiple files
documentUploader: f({
pdf: { maxFileSize: '10MB', maxFileCount: 5 },
'application/msword': { maxFileSize: '10MB', maxFileCount: 5 },
})
.middleware(async ({ req }) => {
const session = await getSession(req)
if (!session) throw new Error('Not authenticated')
return { userId: session.user.id }
})
.onUploadComplete(async ({ metadata, file }) => {
await db.document.create({
data: {
name: file.name,
url: file.url,
size: file.size,
userId: metadata.userId,
},
})
}),
} satisfies FileRouter
export type OurFileRouter = typeof uploadRouter
Step 3: React Components
// components/AvatarUpload.tsx — Pre-built upload button
import { UploadButton, UploadDropzone } from '@uploadthing/react'
import type { OurFileRouter } from '@/server/uploadthing'
// Simple button
export function AvatarUpload() {
return (
<UploadButton<OurFileRouter, 'avatarUploader'>
endpoint="avatarUploader"
=> {
console.log('Uploaded:', res[0].url)
}}
=> {
console.error('Upload failed:', error.message)
}}
/>
)
}
// Drag and drop zone
export function DocumentUpload() {
return (
<UploadDropzone<OurFileRouter, 'documentUploader'>
endpoint="documentUploader"
=> {
console.log(`${res.length} files uploaded`)
}}
/>
)
}
Guidelines
- Free tier: 2GB storage, 2GB transfer/month — enough for MVPs.
- UploadThing handles presigned URLs, multipart upload, and CDN delivery.
- Middleware runs on every upload — use for auth, rate limiting, and validation.
- For self-hosted alternative, use S3 + presigned URLs directly (more work, no vendor lock-in).
1---2name: uploadthing3description: >-4 Handle file uploads in TypeScript apps with UploadThing. Use when a user5 asks to implement file uploads, handle image uploads in Next.js, add drag6 and drop file upload, or integrate S3-backed file storage without managing7 infrastructure.8license: Apache-2.09compatibility: 'Next.js, SolidStart, Express, Fastify'10metadata:11 source: https://skillmd.com/skills/eliferjunior/uploadthing12 author: terminal-skills13 version: 1.0.014 category: development15 tags:16 - uploadthing17 - file-upload18 - s319 - nextjs20 - images21---2223# UploadThing2425## Overview2627UploadThing is a file upload service for TypeScript apps. Define upload routes on the server (with auth, file type, and size validation), get pre-built React components for the frontend. Files go to S3-compatible storage. No infrastructure to manage — just define what's allowed and upload.2829## Instructions3031### Step 1: Setup3233```bash34npm install uploadthing @uploadthing/react35```3637### Step 2: Server Routes3839```typescript40// server/uploadthing.ts — Define upload routes41import { createUploadthing, type FileRouter } from 'uploadthing/server'42import { getSession } from '@/lib/auth'4344const f = createUploadthing()4546export const uploadRouter = {47 // Avatar upload: max 2MB image, authenticated users only48 avatarUploader: f({ image: { maxFileSize: '2MB', maxFileCount: 1 } })49 .middleware(async ({ req }) => {50 const session = await getSession(req)51 if (!session) throw new Error('Not authenticated')52 return { userId: session.user.id }53 })54 .onUploadComplete(async ({ metadata, file }) => {55 console.log(`Avatar uploaded for user ${metadata.userId}: ${file.url}`)56 await db.user.update({57 where: { id: metadata.userId },58 data: { avatarUrl: file.url },59 })60 return { url: file.url }61 }),6263 // Document upload: max 10MB, multiple files64 documentUploader: f({65 pdf: { maxFileSize: '10MB', maxFileCount: 5 },66 'application/msword': { maxFileSize: '10MB', maxFileCount: 5 },67 })68 .middleware(async ({ req }) => {69 const session = await getSession(req)70 if (!session) throw new Error('Not authenticated')71 return { userId: session.user.id }72 })73 .onUploadComplete(async ({ metadata, file }) => {74 await db.document.create({75 data: {76 name: file.name,77 url: file.url,78 size: file.size,79 userId: metadata.userId,80 },81 })82 }),83} satisfies FileRouter8485export type OurFileRouter = typeof uploadRouter86```8788### Step 3: React Components8990```tsx91// components/AvatarUpload.tsx — Pre-built upload button92import { UploadButton, UploadDropzone } from '@uploadthing/react'93import type { OurFileRouter } from '@/server/uploadthing'9495// Simple button96export function AvatarUpload() {97 return (98 <UploadButton<OurFileRouter, 'avatarUploader'>99 endpoint="avatarUploader"100 onClientUploadComplete={(res) => {101 console.log('Uploaded:', res[0].url)102 }}103 onUploadError={(error) => {104 console.error('Upload failed:', error.message)105 }}106 />107 )108}109110// Drag and drop zone111export function DocumentUpload() {112 return (113 <UploadDropzone<OurFileRouter, 'documentUploader'>114 endpoint="documentUploader"115 onClientUploadComplete={(res) => {116 console.log(`${res.length} files uploaded`)117 }}118 />119 )120}121```122123## Guidelines124125- Free tier: 2GB storage, 2GB transfer/month — enough for MVPs.126- UploadThing handles presigned URLs, multipart upload, and CDN delivery.127- Middleware runs on every upload — use for auth, rate limiting, and validation.128- For self-hosted alternative, use S3 + presigned URLs directly (more work, no vendor lock-in).