Upload route and lifecycle orchestration
The server SDK turns a named file route into a controlled upload flow. Developers declare the files a route accepts, validate request input, attach request-specific metadata, and define the work that runs after success or failure. The shared handler then applies that contract before it asks UploadThing to prepare an upload.
This page is a child of Core server SDK. The framework entry points that call the shared handler are described in Server runtime adapters, while the browser-side requests and direct file transfer are described in Upload client protocol.
Route declarations
Builder contract
The createUploadthing builder creates a route factory. A route has a name in the file router, a file policy, optional request input, middleware, and lifecycle callbacks. The builder carries the types from each step into the next one, so route input, middleware metadata, callback output, and error output stay linked.
The route becomes usable only after onUploadComplete is set. The route factory can also apply a shared error formatter so the handler can turn internal errors into the shape returned to the client.
References: packages/…/server.ts• L28-30 packages/…/upload-builder.ts• L61-86 packages/…/types.ts• L107-172
File policy
The file policy defines which file types a route accepts and the limits applied to each type. A short list uses the default limits. An object form lets a route set size and count bounds plus storage-facing properties such as access control and content disposition.
The server expands the short or partial form into a full policy before it checks an upload. It matches each file to an allowed type, checks its size, and checks the count for that type. These checks run before the server requests upload URLs, so a bad upload fails without starting a transfer.
References: packages/…/types.ts• L146-178 packages/…/types.ts• L181-220 packages/…/types.ts• L236-238 packages/…/utils.ts• L43-92 packages/…/route-config.ts• L54-115
Request input
An optional input parser validates the JSON value sent with an upload. The parsed value reaches middleware rather than the raw value, which lets the route use one trusted input shape when it applies access checks or builds metadata.
An input parse error stops the route before middleware and before upload preparation. The handler reports it as a client input error.
References: packages/…/types.ts• L78-87 packages/…/types.ts• L107-120 packages/…/handler.ts• L485-509
Middleware metadata
Middleware is the route's request-time policy step. It receives the adapter's request context, the parsed input, and the files named by the client. It can reject the request or return JSON metadata that is stored with the upload and later passed to onUploadComplete.
Two symbol keys carry control data in the middleware result. UTFiles lets middleware add custom IDs and file labels without changing the original file size used for policy checks. UTRegion can select the preferred ingest region for the upload. A UTFiles override must contain one entry per input file.
References: packages/…/types.ts• L34-63 packages/…/types.ts• L121-137 packages/…/handler.ts• L409-472 examples/…/uploadthing.ts• L37-50
Completion callbacks
onUploadComplete is the success hook for application work that must run after UploadThing has accepted a file. It receives the adapter context, the uploaded file record, and the metadata returned by middleware. Its return value becomes server data for the client when the route waits for server data.
Routes wait for this result by default. Setting awaitServerData to false lets the client finish as soon as the file transfer completes, so the client result has no server callback data even though the server callback still runs.
References: packages/…/types.ts• L89-98 packages/…/types.ts• L151-171 packages/…/types.ts• L190-197 packages/…/handler.ts• L303-407
Failure callbacks
onUploadError is the failure hook for an upload that UploadThing reports as failed. It receives the adapter context, the file key, and an UploadThingError that describes the failure. The callback is for recording or repairing application state; its own failure is logged and does not create a second upload flow.
The server accepts error hooks only after it verifies the signed request from UploadThing. This keeps an external caller from directly triggering application failure work.
References: packages/…/types.ts• L100-105 packages/…/handler.ts• L241-301
Request lifecycle
Upload negotiation
The upload action is the first server step after a client selects a route. The handler parses input, runs middleware, expands and checks the file policy, then asks UploadThing for presigned URLs. It returns those URLs and file keys to the client; the client uses them for the direct transfer described in Upload client protocol.
The handler also sends the middleware metadata and route choices to the ingest service. This binds the prepared files to the route and gives the service the information needed to send a later completion or error hook.
The sequence below shows the boundary between route code, the server handler, UploadThing, and the client.
sequenceDiagram
participant Client
participant Handler as Server route handler
participant Route as File route
participant UT as UploadThing service
participant Storage as Storage
Client->>Handler: Upload request with slug, files, and input
Handler->>Route: Parse input and run middleware
Handler->>Handler: Apply file policy
Handler->>UT: Register metadata and request signed URLs
UT-->>Handler: Presigned URLs and file keys
Handler-->>Client: Upload instructions
Client->>Storage: Transfer file directly
Storage-->>UT: Upload result
References: packages/…/handler.ts• L152-207 packages/…/handler.ts• L474-603 packages/…/handler.ts• L617-737
Callback processing
After the transfer, UploadThing sends a signed callback or error request to the route handler. The handler selects the route by slug, verifies the signature, and starts the matching lifecycle hook with the adapter context.
Completion work runs as a background task so a slow application callback does not hold the service request open. The handler sends the callback result back to UploadThing when the completion hook returns. The handleDaemonPromise setting controls whether the server waits for this background task, leaves it running, or passes it to a platform task manager. Development mode uses the callback stream to forward these events to the local handler.
flowchart TD
A[File transfer completes] --> B[Signed service callback]
B --> C{Hook type}
C -->|callback| D[Run onUploadComplete]
C -->|error| E[Run onUploadError]
D --> F[Send server data to service]
E --> G[Finish failure handling]
References: packages/…/handler.ts• L169-207 packages/…/handler.ts• L241-301 packages/…/handler.ts• L303-407 packages/…/types.ts• L69-82
Route discovery
The handler exposes route metadata separately from upload actions. It extracts the full policy for each route so a client can learn the allowed file types and limits before it starts an upload. The POST path then uses the route slug to select the same route definition for validation and callback dispatch.
An unknown slug is a route error. The client and server must therefore use the same router keys and endpoint URL.
References: packages/…/server.ts• L32-44 packages/…/server.ts• L46-47 packages/…/handler.ts• L103-126 packages/…/handler.ts• L152-167
Change checklist
When these files or sections are changed, remember to consider these:
- When a route policy changes, check both the metadata exposed to clients and the server-side file checks. A route can fail early if these views drift.
- When middleware metadata or
UTFileschanges, check the metadata sent during negotiation and the fields expected byonUploadComplete. - When
onUploadCompleteoutput changes, checkawaitServerData. The route option controls whether that output crosses back to the client. - When adapter context changes, check every runtime adapter and every route callback that reads request or response data. The shared handler passes that context to middleware and both callbacks.
- When callback hosting changes, check the callback URL and daemon handling. These settings decide whether signed completion events can reach the route and whether background work survives the response.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Client sends a slug that is not in the router | The handler returns a not-found error and does not contact UploadThing | Keep the client endpoint registry and server router keys aligned |
| Input fails its route parser | The request stops before middleware and URL generation | Treat the parser as the single boundary for the route's request input |
| File type, size, or count violates policy | The upload is rejected before the service prepares URLs | Keep route limits valid and reflect them in client route metadata |
Middleware throws or returns a UTFiles list with the wrong length | The upload request fails and no upload preparation follows | Return one file override per input file and throw only for a real policy failure |
| Callback or error request has no valid signature | The lifecycle hook does not run | Let UploadThing send callbacks and keep the route token available to the handler |
| Completion work throws or the process ends before its daemon runs | Server data may not reach UploadThing and application completion work may be incomplete | Use handleDaemonPromise with the host platform's background-task facility when needed |
Key files
| File | Why to read it |
|---|---|
| packages/…/server.ts | Shows the public server SDK entry points for route builders, handlers, and route metadata. |
| packages/…/upload-builder.ts | Shows how a route is assembled from policy, input, middleware, and lifecycle hooks. |
| packages/…/types.ts | Defines the contract and type flow for middleware metadata and callback input and output. |
| packages/…/route-config.ts | Shows how route policy is expanded and how files are checked before upload preparation. |
| packages/…/handler.ts | Follows the shared request path from route selection through negotiation and signed callbacks. |
| packages/…/types.ts | Defines file policy and route-level options such as server-data waiting and URL lifetime. |
| examples/…/uploadthing.ts | Gives a compact route declaration with file policy, custom IDs, metadata, and completion work. |