Introducing a new way to review your PRs.

Read the announcement

pingdotgg / uploadthing

File uploads for modern web devs

View on GitHub

19 documents · Updated Oct 2, 2026

docs / react-sdk

React SDK

The React SDK is the browser-facing layer for UploadThing. It turns a typed UploadThing route into React hooks and upload controls that select files, track transfer state, and report results to application code. It also lets a Next.js server provide route metadata during render so these controls can become ready without an extra metadata request.

SDK boundary

The SDK sits between application components and the shared UploadThing client. An application binds its FileRouter to generated helpers or components, then supplies an endpoint and optional callbacks. The shared client performs the upload protocol while the React layer owns component state and browser events. packages/…/index.ts• L1-15 exposes the public surface.

The public entry points have distinct roles:

Entry pointRole
generateReactHelpersCreates typed hooks and client helpers for a router.
generateUploadButtonCreates a router-bound button component.
generateUploadDropzoneCreates a router-bound drag-and-drop component.
generateUploaderCreates a component that presents both upload modes.
useDropzoneSupplies file-selection behavior for custom React interfaces.
@uploadthing/react/next-ssr-pluginMakes route metadata available during Next.js server and client rendering.

The package supports React 17, 18, and 19. Next.js is optional for the core React package and is needed only for the Next.js metadata bridge. packages/…/package.json• L66-79 defines these integration boundaries.

Typed upload helpers

generateReactHelpers binds a FileRouter and an API URL or custom fetch function to the React upload flow. Its generated useUploadThing hook accepts a route name and returns startUpload, isUploading, and route configuration. The same generated object includes the lower-level client helpers for code that does not need React state. packages/…/use-uploadthing.ts• L160-205 defines this binding.

The hook keeps application state aligned with the client transfer. It can alter files before submission, pass route input and request options, report upload progress, and notify the application when the server result or an error is available. Progress represents the combined transfer as one percentage, and the default reporting mode reduces event noise. packages/…/use-uploadthing.ts• L66-145 and packages/…/types.ts• L52-120 define this contract.

For a route with server-defined input or output types, the router generic carries those types into the hook and its callbacks. This makes the React boundary part of the same type contract as the UploadThing route instead of a separate untyped upload API. packages/…/types.ts• L11-18 and packages/…/types.ts• L191-200 describe the route-derived types.

See React upload experience for the hook lifecycle and the built-in upload controls.

Component integration

The SDK provides ready-to-use controls for the two main file selection patterns. UploadButton uses a file picker and can upload as soon as files are selected or wait for an explicit upload action. UploadDropzone adds drag-and-drop and paste behavior while applying the route's allowed file types and quantity rules. Uploader combines both controls for a simple showcase or broad starting point. packages/…/index.tsx• L19-95 and packages/…/uploader.tsx• L10-35 define these component paths.

Both controls use the same upload hook and expose the same application callbacks. Their visible state moves from route metadata loading to ready, uploading, or disabled. During an upload they show progress and allow cancellation through an internal abort signal. packages/…/button.tsx• L99-179 and packages/…/dropzone.tsx• L135-233 show the shared state relationship.

Applications can use generated components to bind the router once. They can also use the raw components when they need direct control over the generic types, URL, or fetch function. Appearance and content hooks allow an application to replace labels and styles without taking ownership of the upload flow. packages/…/index.tsx• L21-95 and packages/…/types.ts• L122-200 define this boundary.

Route metadata flow

Route metadata tells the client which file types and quantities an endpoint accepts. The hook reads metadata from a server-provided global when available. If it is not available, it fetches the metadata from the UploadThing API URL and caches the result for that hook instance. packages/…/use-uploadthing.ts• L38-50 and packages/…/useFetch.ts• L20-98 define the two paths.

The metadata flow connects server route configuration to browser controls as follows:

flowchart LR
    R[UploadThing FileRouter] --> E[Extract route metadata]
    E --> P[NextSSRPlugin]
    P --> H[Server markup and client global]
    H --> U[React helpers and components]
    U --> C[Allowed file rules and ready state]
    C --> X[UploadThing client transfer]

If the metadata is absent, the controls remain in a loading state until the endpoint configuration is known. getRouteConfig is stricter: it reads only server-provided metadata and raises an error when the Next.js plugin was not used. packages/…/use-uploadthing.ts• L185-204 defines this server-data requirement.

Next.js rendering bridge

NextSSRPlugin is the Next.js App Router bridge for route metadata. It receives the extracted router configuration on the server, stores it for server-side consumers, and inserts one script during rendering so the browser starts with the same configuration. It renders no visible UI. packages/…/next-ssr-plugin.tsx• L12-41 defines this bridge.

The bridge keeps server and client views of route readiness consistent. A Next.js layout normally renders it near the application root and passes the same router configuration used by the UploadThing server route. playground/…/layout.tsx• L1-15 and playground/…/layout.tsx• L35-39 show this placement.

See React Next.js integration for the rendering setup and metadata contract.

Upload cancellation and errors

Each built-in control owns an abort controller for its active transfer. A user action during upload cancels that transfer, and the component maps the cancellation to onUploadAborted. Other failures become typed UploadThing errors when possible and reach onUploadError; unexpected failures are reported as fatal client errors. packages/…/use-uploadthing.ts• L121-145 and packages/…/button.tsx• L164-179 define this boundary.

The upload state is reset after completion or failure. Completion callbacks receive the server result after any configured server-side completion work, while the controls clear their selected files and progress for the next transfer. packages/…/types.ts• L97-107 and packages/…/dropzone.tsx• L147-168 define this lifecycle.

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to the route metadata shape must stay aligned between NextSSRPlugin, useRouteConfig, and the server-side router extraction that supplies routerConfig.
  • Changes to the generated helper or component generics must preserve endpoint input, output, and error types from FileRouter.
  • Changes to upload completion or abort behavior must preserve the callback distinction between a completed transfer, an UploadThing error, and an intentional cancellation.
  • Changes to the public exports or package subpaths must keep the main React entry point and next-ssr-plugin entry point usable by their separate runtimes.

Important failure modes

TriggerConsequenceGuard
A Next.js app omits NextSSRPlugin but calls getRouteConfigRoute configuration lookup throws and a custom UI cannot determine the endpoint rules from server data.Add the plugin with the extracted router configuration at the app root, or use the hook's client metadata path.
Server and client use different router metadataThe UI can show the wrong allowed file rules or readiness state for the endpoint.Pass the same extracted FileRouter configuration to NextSSRPlugin that serves the UploadThing route.
An upload is aborted without handling onUploadAbortedThe application may treat an intentional cancel as an upload error or leave its own UI state unchanged.Handle abort and error callbacks as separate outcomes.
The React and uploadthing package versions do not matchThe SDK warns about an invalid peer dependency and client behavior may be incompatible.Keep the package versions within the declared peer range.

Key files

FileWhy to read it
packages/…/index.tsShows the public React SDK surface.
packages/…/use-uploadthing.tsConnects typed React hooks to the UploadThing client and transfer state.
packages/…/types.tsDefines the route-derived input, callback, and component contracts.
packages/…/index.tsxShows how router-bound button, dropzone, and uploader components are generated.
packages/…/button.tsxIllustrates file-picker upload behavior and component state.
packages/…/dropzone.tsxIllustrates drag-and-drop, paste, validation, and custom dropzone behavior.
packages/…/next-ssr-plugin.tsxDefines the Next.js server-to-client route metadata bridge.
packages/…/useFetch.tsShows the fallback route metadata fetch and local cache behavior.
playground/…/layout.tsxShows Next.js root placement of the SSR plugin.
playground/…/uploader.tsxShows application-level generation of typed React helpers and components.