Getting Started with Tanstack/Start
Package Setup
Install the packages
npm install uploadthing @uploadthing/react
Add env variables
UPLOADTHING_TOKEN=... # A token for interacting with the SDK
If you don't already have a uploadthing token, sign up ↗ and create one from the dashboard! ↗
Tanstack Start is currently in beta, this guide works at the time of writing
(v1.95.0) but there might be some breaking changes introduced in the
framework that we have not kept up to date with.
Set Up A FileRouter
Creating your first FileRoute
All files uploaded to uploadthing are associated with a FileRoute. The following is a very minimalistic example, with a single FileRoute "imageUploader". Think of a FileRoute similar to an endpoint, it has:
- Permitted types ["image", "video", etc]
- Max file size
- How many files are allowed to be uploaded
- (Optional)
inputvalidation to validate client-side data sent to the route - (Optional)
middlewareto authenticate and tag requests onUploadCompletecallback for when uploads are completed
To get full insight into what you can do with the FileRoutes, please refer to the File Router API.
app/server/uploadthing.ts
import { createUploadthing, UploadThingError } from "uploadthing/server";
import type { FileRouter } from "uploadthing/server";
const f = createUploadthing();
const auth = (req: Request) => ({ id: "fakeId" }); // Fake auth function
// FileRouter for your app, can contain multiple FileRoutes
export const uploadRouter = {
// Define as many FileRoutes as you like, each with a unique routeSlug
imageUploader: f({
image: {
/**
* For full list of options and defaults, see the File Route API reference
* @see https://docs.uploadthing.com/file-routes#route-config
*/
maxFileSize: "4MB",
maxFileCount: 1,
},