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 / core-server-sdk

Core server SDK

The Core server SDK connects an application server to UploadThing. It lets an application define typed file routes, exposes those routes through a common request handler, and coordinates the steps that turn a client upload request into stored file data and server callback data. Framework adapters keep this contract consistent across supported runtimes.

Server SDK responsibilities

The SDK has three related responsibilities. Route definitions describe what an application accepts and what it does after an upload. The shared handler turns framework requests into one upload protocol. The server utility API gives trusted server code access to file operations and storage metadata.

Route definitions

The route builder is the application’s declaration point for upload policy and server work. A route can validate client input, add metadata or file identifiers, and return data after UploadThing stores the file. The resulting FileRouter maps a public route slug to its contract and is also the type source for client upload APIs. See Upload route and lifecycle orchestration for the route stages and callback rules.

The builder carries the route’s input, metadata, output, and error types as the definition is composed. This keeps the client and server sides aligned without making the request handler know the application’s business types. Route options also decide whether the client waits for server callback data.

References: packages/…/server.ts• L28-30 packages/…/upload-builder.ts• L16-30 packages/…/types.ts• L107-171

Request handler

The request handler is the shared server boundary for all runtimes. Each adapter supplies the native request context and converts the incoming request to a web Request; the shared handler then applies the same route lookup, protocol checks, upload processing, callback processing, and error response rules. See Server runtime adapters for the framework-specific boundary behavior.

The handler exposes GET for route metadata and POST for upload actions and signed callback messages. It checks the route slug, validates request bodies, verifies UploadThing signatures for callback messages, and returns formatted errors with HTTP status codes. The handler also adds the SDK version to responses so client and server version drift can be seen.

The following diagram shows how a framework request reaches the shared handler and how the handler connects the client, UploadThing services, and application callbacks.

flowchart LR
    A[Framework request] --> B[Runtime adapter]
    B --> C[Shared request handler]
    C --> D{Request kind}
    D -->|Route metadata| E[Return route configuration]
    D -->|Upload action| F[Validate input and files]
    F --> G[Request presigned upload URLs]
    G --> H[Return upload instructions to client]
    D -->|Signed callback| I[Run application callback]
    I --> J[Send callback result to UploadThing]

References: packages/…/handler.ts• L50-101 packages/…/handler.ts• L103-239 packages/…/types.ts• L89-92

Upload lifecycle

An upload starts when a client sends a route slug, validated input, and file descriptions to the application endpoint. The server parses the input, runs route middleware, checks file type, size, and count rules, then creates signed upload instructions for the selected ingest region. The client uses those instructions to send file bytes directly to UploadThing storage. See Upload client protocol for the client-facing request and transfer sequence.

After the files reach storage, UploadThing sends a signed callback to the application endpoint. The server runs onUploadComplete with the stored file data and route metadata, then reports the callback result back to UploadThing. Failed uploads use the signed error path and can invoke onUploadError.

The server starts callback work separately from the response path. Runtime configuration decides whether the server waits for that work, leaves it running in the background, or passes it to the host platform’s deferred-work facility. This choice matters for short-lived serverless requests because the host must keep deferred work alive.

References: packages/…/handler.ts• L474-603 packages/…/handler.ts• L672-737 packages/…/handler.ts• L241-407 packages/…/types.ts• L69-82

Server utility API

UTApi is the trusted server-side API for operations that do not begin with a browser route request. It supports direct file upload and upload-from-URL flows, file deletion, listing, renaming, URL access, access-control updates, usage checks, and signed URL creation. These operations use the application token and should stay on the server.

Direct server uploads create signed storage instructions and send file data without the browser route negotiation. The same API can download a remote file first and then upload it. API responses are checked and failures are surfaced as UploadThingError values so callers receive a consistent error model.

References: packages/…/server.ts• L20-22 packages/…/index.ts• L48-63 packages/…/index.ts• L127-186 packages/…/index.ts• L253-471 packages/…/utils.ts• L34-159

Runtime boundary

The core handler expects a web request and produces a web response. Runtime adapters preserve the host framework’s request context for route callbacks while translating response status, headers, and body back to the framework. This split lets the route and upload lifecycle remain shared even when the surrounding server model differs.

Supported runtimes

The package provides entry points for Next.js App Router, legacy Next.js API routes, Express, Fastify, H3, Remix, and Effect Platform. The adapters expose the same route builder and handler contract, but they return the shape required by each host. The Server runtime adapters page describes these boundaries in detail.

The adapter context is available to route middleware and callbacks. It can contain the native request, response, event, or action arguments that belong to the host runtime. The web request conversion also uses forwarded host and protocol data when it must build an absolute request URL.

References: packages/…/next.ts• L21-39 packages/…/next-legacy.ts• L22-53 packages/…/express.ts• L27-61 packages/…/fastify.ts• L22-55 packages/…/h3.ts• L22-40 packages/…/remix.ts• L21-39 packages/…/effect-platform.ts• L26-72

Runtime configuration

The handler reads its token and service settings from explicit configuration or UPLOADTHING_ environment values. Configuration selects development behavior, the callback URL, the ingest endpoint, the fetch function, logging, and the policy for deferred callback work. Explicit options take priority over environment values.

The token identifies the application and authorizes signed URL and service requests. The selected region can change the ingest endpoint when middleware requests a preferred region. In development, the handler can stream callback messages through the local route so local uploads still exercise the callback lifecycle.

References: packages/…/types.ts• L42-87 packages/…/config.ts• L15-63 packages/…/config.ts• L65-99 packages/…/runtime.ts• L12-33 packages/…/handler.ts• L608-721

Client and service contract

The server SDK is one side of a two-part contract. The client first calls the application route to obtain presigned URLs, then uploads file bytes to storage and receives stored-file data. The server uses a separate service channel to register file metadata and to exchange callback results with UploadThing.

Route metadata

A GET request returns the expanded configuration for every route in the router. Clients use this information to understand permitted file groups and limits before upload. The server still validates the upload action itself, so client-side checks improve the user experience but do not replace the server boundary.

References: packages/…/handler.ts• L103-126 packages/…/route-config.ts• L117-125 packages/…/client.ts• L54-83

Upload negotiation

An upload action carries file descriptions and route input as JSON. The server responds with one presigned URL and storage key for each accepted file. The response also carries the name and custom identifier used by the client to associate each storage transfer with its original file.

The server registers metadata with the ingest service after it prepares these URLs. That registration includes route metadata, callback information, and the route’s server-data waiting policy. The client then uploads the bytes directly to the presigned URLs instead of sending file content through the application server. See Upload client protocol for the matching client behavior.

References: packages/…/shared-schemas.ts• L51-116 packages/…/shared-schemas.ts• L144-149 packages/…/handler.ts• L546-603 packages/…/handler.ts• L617-737

Callback exchange

UploadThing authenticates callback and error messages with a signature over the request body. The handler rejects an invalid signature before it invokes application code. A successful completion callback returns application server data to UploadThing, which makes that data available to the client when the route waits for server data.

The callback result is associated with the stored file key. This association allows multi-file uploads to receive the correct server result for each file. If callback work fails, the handler logs the failure and reports an error result rather than treating application callback failure as a successful server-data exchange.

References: packages/…/handler.ts• L241-407 packages/…/shared-schemas.ts• L75-100 packages/…/types.ts• L196-220

Change checklist

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

  • Keep the route builder’s inferred input, output, metadata, and error contracts aligned with FileRouter and the client uploader types.
  • Keep the upload action and callback schemas aligned with the client protocol and the UploadThing service endpoints.
  • When changing adapter request conversion, verify callback URL detection and preservation of the native context passed to middleware and callbacks.
  • When changing deferred callback handling, verify behavior for development servers, stateful servers, and serverless hosts.
  • When changing route configuration or region selection, verify both the presigned URL metadata and the ingest service registration.
  • When changing error formatting or status mapping, preserve the error shape expected by client callers and framework adapters.

Important failure modes

TriggerConsequenceGuard
Missing or invalid UPLOADTHING_TOKENThe handler cannot authorize service requests or verify callbacks.Set a valid token in configuration or the environment. The SDK reports a missing or invalid server configuration.
Client and server package versions differThe upload contract may behave in an incompatible way.Keep the client and server package versions synchronized. The handler logs a version mismatch.
An upload exceeds the route’s type, size, or count policyThe server rejects the upload before it requests storage URLs.Define complete route limits and rely on server validation rather than client checks alone.
A callback has an invalid signatureThe application callback is not run.Route callback traffic through UploadThing and preserve the signature header and request body.
A short-lived host drops deferred workonUploadComplete or onUploadError can stop before it reports its result.Use the host’s wait-until or after-response facility through handleDaemonPromise.
An adapter cannot build an absolute request URLDevelopment callbacks or callback URL detection can target the wrong endpoint.Preserve host and forwarded URL headers or set callbackUrl explicitly.

Key files

FileWhy to read it
packages/…/server.tsPublic server entry point for the route builder, common handler, server API, and router metadata.
packages/…/upload-builder.tsDefines the high-level route composition contract and its defaults.
packages/…/handler.tsShows the shared request boundary and the upload, callback, and error flow.
packages/…/types.tsDefines the public router, handler configuration, and client/server type relationships.
packages/…/config.tsExplains token, endpoint, region, development, and deferred-work settings.
packages/…/route-config.tsShows the server-side route policy checks and route metadata extraction.
packages/…/index.tsDefines the server utility API for storage and file-management operations.
packages/…/shared-schemas.tsDefines the request and response data exchanged by the client, server, and UploadThing services.
packages/…/next.tsIllustrates a web-native runtime adapter.
packages/…/express.tsIllustrates a Node request-body and response-stream adapter.