Upload client protocol
The UploadThing browser client gives web and mobile clients a typed way to start file uploads, send file bytes to storage, track each file, and receive the result of the server-side upload flow. It separates route negotiation from file transfer so the application server can check route rules and add request metadata before the client sends bytes to storage.
Client API surface
The public client API binds an uploader to a server file router. The generated functions keep the route name, input type, and returned server data aligned with the route definition. See the core server SDK for the server-side route model.
Generated uploaders
genUploader returns uploadFiles for a one-step upload and createUpload for an upload that the caller can control. Both accept files, optional route input, custom headers, and progress callbacks. The route can be passed as a string or through routeRegistry, which gives an IDE link to the matching server route. future_genUploader exposes the same main shape with a richer per-file event and state model; its API is marked as not covered by semver.
The generated uploader can infer its API URL from the browser origin or a deployment URL. A caller can instead provide a full endpoint URL and a custom fetch function. Custom headers support application auth and can be supplied as a value or as a function that resolves at request time.
References: packages/…/client.ts• L85-122 packages/…/client.ts• L255-299 packages/…/client-future.ts• L57-75 packages/…/types.ts• L94-164 packages/…/types.ts• L200-237
Upload controls
createUpload starts negotiation and returns controls that wait for one file or for the full group. A caller can pause and resume selected files or the whole group. The future API also supports aborting files and resolving each aborted file as a failed file with an UploadAbortedError; the one-step API maps an aborted signal to that error.
The returned done promise is the boundary between an active upload and its client result. The legacy API rejects when a transfer or negotiation fails. The future API records transfer failures in the file state and resolves with uploaded or failed files, while negotiation failures still leave the start operation failed.
References: packages/…/client.ts• L94-225 packages/…/client.ts• L231-287 packages/…/client-future.ts• L67-141 packages/…/client-future.ts• L143-250
Upload negotiation
Negotiation asks the application’s UploadThing route to authorize a set of files and return temporary storage URLs. The request carries only file metadata and route input. The client uses the returned data to direct the later byte transfer.
Negotiation request
The client sends a POST request to the configured API URL. It identifies the file route with the slug query parameter and selects upload negotiation with actionType=upload. The JSON body contains the files’ names, sizes, MIME types, and modification times, plus the route input when the route defines one.
The request includes custom application headers and client identity headers. x-uploadthing-package and x-uploadthing-version identify the client package and version. b3 and traceparent carry one trace identity across negotiation and transfer so related requests can be found together in service logs.
References: packages/…/ut-reporter.ts• L17-35 packages/…/ut-reporter.ts• L46-78 packages/…/shared-schemas.ts• L49-58 packages/…/shared-schemas.ts• L144-149
Presigned URL response
The route returns one presigned URL for each requested file. A presigned URL is a temporary storage URL that lets the client upload bytes without exposing storage credentials. Each result also gives the file key, its name, and an optional custom ID set by route middleware.
The client pairs response items with the input files by position. The server route must therefore return a complete result set in the same order as the request. The application server can reject route input, file type, size, or count before any storage transfer starts. See core upload routing for the rules that shape this response.
The following diagram shows the boundary between route negotiation and direct storage transfer.
sequenceDiagram
participant Browser
participant App as UploadThing route
participant Store as Storage service
Browser->>App: POST ?actionType=upload&slug=route
App-->>Browser: Presigned URL, key, custom ID
Browser->>Store: HEAD presigned URL
Store-->>Browser: Existing byte range
Browser->>Store: PUT file bytes with Range
Store-->>Browser: Upload result and server data
References: packages/…/shared-schemas.ts• L109-116 packages/…/types.ts• L192-200 packages/…/handler.ts• L152-187 packages/…/handler.ts• L723-737
File transfer
After negotiation, the browser sends each file to the storage URL rather than sending file bytes through the application route. The transfer uses a range check and an XHR request so the client can resume an interrupted transfer and report byte progress.
Range transfer
Before the PUT, the client sends a HEAD request to read x-ut-range-start. A missing value means that transfer starts at byte zero. A non-zero value means that storage already has that prefix, so the client sends the remaining slice with Range: bytes=<start>-.
The PUT body is multipart form data with a file field. The client repeats the upload version and trace headers on this request. Browser XHR provides the progress signal; the client adds the known range start to the reported byte count.
References: packages/…/upload-browser.ts• L22-109 packages/…/upload-browser.ts• L111-147 packages/…/client-future.ts• L393-419 packages/…/client-future.ts• L463-492
Transfer result
Successful storage response data contains the file URLs, a content hash, and the server data associated with the route. The client combines this data with the original file metadata and the negotiated key and custom ID to create its result.
The main current upload path can transfer several files at once while limiting active transfers to six. Progress reports include both the bytes and percentage for the current file and the aggregate upload. The future API emits typed events instead of only a progress callback.
References: packages/…/types.ts• L204-220 packages/…/upload-browser.ts• L149-172 packages/…/upload-browser.ts• L197-240 packages/…/client-future.ts• L421-452 packages/…/client-future.ts• L604-641
Upload state
The client exposes upload work as either callback data or a per-file state machine. Both forms let a UI show that negotiation is pending, bytes are moving, a file is complete, or a file cannot finish.
File states
The future API attaches state to each File object. A pending file has no storage key until negotiation completes. An uploading file has its key and sent-byte count. An uploaded file adds its URL, hash, and route output. A failed file keeps the bytes sent so far and the typed reason for failure.
These states describe one file inside a group. The group may contain uploaded, uploading, pending, and failed files at the same time, which lets a UI retry or report one file without hiding the state of the others.
References: packages/…/client-future.ts• L74-186 packages/…/client-future.ts• L231-281
Upload events
The future API emits one event stream for the group. It reports presigned data, the start of a file transfer, progress, completion, failure, and abort. Each event identifies the active file when one exists and includes the full group so a caller can derive a consistent view without a separate store.
The current API keeps the simpler callback contract: onUploadBegin runs after negotiation for each file, and onUploadProgress reports file and group totals. The callback values use bytes for loaded, delta, and totalLoaded; percentage values are separate fields.
References: packages/…/client-future.ts• L285-375 packages/…/types.ts• L94-127 packages/…/upload-browser.ts• L211-238
Client error contract
Errors have two sources: the application route can reject negotiation, or the storage and network path can reject a transfer. The client keeps these sources distinct so an application can show route errors differently from storage or connectivity errors.
Negotiation errors
The route returns structured errors for invalid input, an unknown route slug, and other route failures. The client turns an HTTP failure or an invalid response into an UploadThing error. The future API wraps a route failure in UTServerError, which retains the error data produced by the server formatter.
A client and server version mismatch is reported by the server as a warning. It is not itself a transfer failure, but mismatched versions can break the shared request and response contract. Keep the client package and server SDK versions aligned.
References: packages/…/ut-reporter.ts• L74-112 packages/…/client-future.ts• L506-583 packages/…/handler.ts• L145-167 packages/…/handler.ts• L209-227
Transfer errors
The future API classifies transfer failures as network errors or storage errors and stores the result on the affected file. A storage error means the storage service returned an error response. An XHR error means the browser could not complete the network request. An abort is a separate client action and uses UploadAbortedError.
The current API reports a failed transfer by rejecting the upload promise. Callers that need per-file recovery should use the future API or keep the file list and error context around the rejected operation.
References: packages/…/client-future.ts• L25-72 packages/…/client-future.ts• L429-461 packages/…/client-future.ts• L494-502 packages/…/upload-browser.ts• L49-80 packages/…/client.ts• L162-176
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the negotiation payload,
NewPresignedUrlresponse, and server route handler aligned. The client pairs files and presigned results by position. - Keep
x-ut-range-startand theRangerequest behavior aligned with the storage service. A change can silently disable resume support or duplicate bytes. - Keep
UploadPutResultfields aligned withClientUploadedFileDataand the future uploaded-file state. URL migrations must preserveufsUrland its deprecated aliases until their contract is removed. - Keep client error mapping aligned with the server error formatter.
UTServerError.datais the typed boundary used by future clients. - Keep trace and version headers on both negotiation and storage requests so service logs can join the full upload.
- Keep the legacy callbacks and future event names aligned with the state transitions when upload controls change.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The route returns fewer or reordered presigned results | A file can receive another file’s storage URL or remain without a transfer | Preserve one result per input file and keep response order stable |
The slug does not match a server file route | Negotiation returns a not-found error before transfer | Use the generated routeRegistry or a route name that exists on the server |
| File metadata violates route size, type, or count rules | The negotiation promise fails and no storage upload starts | Validate against the route configuration before presenting the upload as ready |
The storage service reports an existing range but the client omits it from Range or the body slice | Resume can overwrite or duplicate the already stored prefix | Treat x-ut-range-start as the start of both the request range and the file slice |
| Client and server package versions drift | Shared request or response assumptions can fail; the server logs a version warning | Release compatible client and server SDK versions together |
| A caller treats a future failed file as a rejected group promise | A failed file can be missed because future transfer failures are returned in file state | Check each UploadedFile or FailedFile result and its reason |
Key files
| File | Why to read it |
|---|---|
| packages/…/client.ts | Defines the main generated uploader, one-step upload result, progress callback, and pause/resume controls |
| packages/…/client-future.ts | Defines the newer generated uploader and the public per-file state, event, abort, and error model |
| packages/…/ut-reporter.ts | Defines the browser-to-route request URL, JSON payload, headers, and response error mapping |
| packages/…/upload-browser.ts | Defines the range check, direct storage PUT, XHR progress, and current transfer result mapping |
| packages/…/client-future.ts | Defines future transfer events, state transitions, typed failures, and presigned URL negotiation |
| packages/…/shared-schemas.ts | Defines the file metadata, presigned response, and uploaded-file data shared by client and server |
| packages/…/handler.ts | Shows how the server route receives the upload action and returns ordered presigned results |
| packages/…/client.test.ts | Shows the supported browser contract for presigned PUT uploads, headers, route validation, callbacks, and returned data |