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 / shared-foundations

Shared foundations

UploadThing's shared foundations provide the common contracts and small cross-runtime services used by its server SDK, browser client, and framework adapters. They keep file rules, file metadata, error results, request shapes, URL signing, and upload validation consistent across JavaScript runtimes instead of making each SDK define them again.

Foundation package

The @uploadthing/shared package is the common boundary for the shared model. Its public entry point re-exports the type contracts, file rules, error classes, validation helpers, cryptographic helpers, browser upload state helpers, and Effect-based request helpers used by the other SDK packages (packages/…/index.ts• L1-9). The package is published for both import and require consumers and treats its exports as side-effect free (packages/…/package.json• L11-30).

The package is shared by server code and several client adapters. The server SDK uses its route and error contracts. Browser-facing packages use its file checks, fetch shape, upload state, and component helpers. This shared boundary lets framework-specific code focus on its host framework while the upload behavior remains aligned (packages/…/server.ts• L1-22, packages/…/client.ts• L1-52, packages/…/types.ts• L1-18).

Shared contracts

The shared type model describes the values that cross the client, server, and storage-service boundaries. It includes JSON-safe data, file properties, request and response shapes, route configuration, endpoint metadata, file-size and time formats, and the small result and composition types used by SDK APIs (packages/…/types.ts• L5-24, packages/…/types.ts• L27-123, packages/…/types.ts• L139-238).

Route configuration is the main product contract. A route can accept broad file groups such as image or video, a precise MIME type, or the blob catch-all. Each configured type carries size, count, and delivery rules. Shared defaults turn short route input into a complete route description that both client controls and server checks can understand (packages/…/file-types.ts• L1-10, packages/…/types.ts• L139-178, packages/…/utils.ts• L32-91).

The shared request model uses a small fetch-compatible subset rather than requiring one platform's full Request and Response types. This allows adapters to provide their own fetch function while preserving the same input, output, and error expectations (packages/…/types.ts• L49-102).

Error model

The shared error model gives upload failures a stable code, message, optional structured data, and optional cause. UploadThingError is the main error that crosses SDK boundaries. It maps named error codes to HTTP status meanings and can be converted to a JSON-safe object for transport to a client (packages/…/error.ts• L6-29, packages/…/error.ts• L54-108).

The model separates product errors from local control-flow errors. Product errors use UploadThingError and can be formatted by a file router. Tagged errors describe local cases such as an unknown file type, an invalid route, a failed fetch, invalid JSON, an aborted upload, or a paused upload (packages/…/tagged-errors.ts• L4-18, packages/…/tagged-errors.ts• L28-99, packages/…/error-formatter.ts• L5-20).

When an error moves from the server to a client, serialization keeps the public code, message, and JSON data while leaving the internal cause behind. This gives adapters a common failure shape and lets a router choose the user-facing error data without exposing internal details (packages/…/error.ts• L54-108, packages/…/error-formatter.ts• L11-20).

File metadata

File metadata is the small, shared description used before a file body is transferred. It centers on the file name, byte size, MIME type, and optional modification time. The same shape is used when clients ask the server for upload authorization and when shared rules select a route (packages/…/types.ts• L27-36, packages/…/client.ts• L124-135).

Node-side file creation uses UTFile when a native File is not available. It gives a blob a name and upload metadata, supplies a MIME type from the name when needed, and preserves an optional custom identifier for later file operations (packages/…/ut-file.ts• L3-27).

MIME data is kept in the separate @uploadthing/mime-types package. The package maps MIME types to extensions and extensions back to MIME types. It is edge-compatible and exposes category entry points so browser components can use only the catalog data they need (packages/…/index.ts• L1-7, packages/…/index.ts• L23-38, packages/…/index.ts• L40-78, packages/…/package.json• L15-76). See the MIME type catalog for the catalog's role and lookup behavior.

Validation helpers

Shared validation turns flexible route input and browser file data into the same decisions on both sides of an upload. It fills route defaults, matches a file by its declared MIME type or its extension, converts human-readable limits to bytes, and reports a typed failure when the file cannot be accepted (packages/…/utils.ts• L26-41, packages/…/utils.ts• L64-136, packages/…/utils.ts• L138-158).

The validation flow applies the route contract before an upload proceeds:

flowchart LR
    A[Route input] --> B[Complete route rules]
    C[File metadata] --> D[Match file type]
    B --> E[Check size and count]
    D --> E
    E -->|valid| F[Continue upload]
    E -->|invalid| G[Return typed error]

The server checks the files before it requests or accepts the upload. The browser client also exposes early type and size checks so users can receive local feedback before network work starts. Server-side checking remains the final boundary because browser checks only improve feedback (packages/…/route-config.ts• L54-69, packages/…/route-config.ts• L70-115, packages/…/client.ts• L54-83).

Browser components use the same route description to build accepted MIME attributes and labels. Generic groups expand to a wildcard plus known catalog entries for browser compatibility. Specific MIME types include known file extensions so file pickers can match formats that do not honor the MIME value alone. A blob route leaves the picker unrestricted because it is the catch-all type (packages/…/component-utils.ts• L30-81, packages/…/component-utils.ts• L83-134).

Cryptographic helpers

Shared cryptographic helpers protect signed requests and create stable file keys across runtimes. Payload signing uses HMAC-SHA-256 with a protected secret. Signed URLs carry an expiry time and optional data, then include a signature over the URL before that signature is appended (packages/…/crypto.ts• L11-13, packages/…/crypto.ts• L31-87, packages/…/crypto.ts• L139-169).

File keys combine an application identity with a seed derived from file metadata. The seed can use selected metadata and a custom hash function, which allows callers to choose between unique uploads and repeatable keys for the same file. Verification checks that a key belongs to the expected application identity (packages/…/crypto.ts• L89-137, packages/…/types.ts• L34-46, packages/…/types.ts• L199-220).

These helpers support two distinct trust decisions:

DecisionShared resultSystem use
Is this URL or callback payload authentic?Signature verificationAccept or reject signed data
Does this file key belong to this app?Application-key verificationAccept or reject a key for the app

The server uses the signing helpers while it creates upload URLs and checks signed client events. The same URL-signing behavior is also available to SDK file operations (packages/…/handler.ts• L246-320, packages/…/utils.ts• L83-101, packages/…/index.ts• L417-469).

Runtime boundary

The shared package keeps browser request effects behind a fetch service contract. FetchContext supplies the fetch implementation, while the shared request helpers turn transport failures and non-JSON responses into tagged errors. Server code uses its platform HTTP layer instead of these browser-only wrappers (packages/…/effect.ts• L4-23, packages/…/effect.ts• L34-78).

This boundary is useful because framework adapters can provide the browser's fetch or a custom fetch without changing upload logic. For the broader lifecycle of these runtime helpers, see Shared runtime utilities.

Change checklist

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

  • Changes to RouteConfig, file-type names, or size and time formats must stay aligned with route parsing, server validation, browser checks, and generated client metadata.
  • Changes to SerializedUploadThingError or error codes must stay compatible with server error formatting and every client adapter that receives upload errors.
  • Changes to MIME lookup or category exports must preserve both extension-based file matching and browser accept-list generation.
  • Changes to file hash inputs or signing behavior must be reviewed with both key verification and signed URL or callback verification.
  • Changes to FetchEsque or response parsing must remain usable by custom fetch implementations in browser and framework packages.

Important failure modes

TriggerConsequenceGuard
A route omits a required value while defaults are being filledThe route cannot produce a complete contract for upload checksfillInputRouteConfig returns InvalidRouteConfigError
A file has no known MIME type and the route does not allow blobType matching stops before an upload startsmatchFileType returns UnknownFileTypeError
A file exceeds the configured size or countThe upload is rejected before upload work continuesServer-side assertFilesMeetConfig checks size and count
A signed payload has a missing or invalid signatureThe event or URL is not trustedverifySignature returns false on bad input or verification failure
A response is not valid JSON or has a non-success statusThe client cannot treat the response as a successful upload resultparseResponseJson returns InvalidJsonError or BadRequestError
A blob route is mixed with narrower file typesThe browser picker cannot narrow the accepted MIME setgenerateMimeTypes returns an unrestricted accept list

Key files

FileWhy to read it
packages/…/index.tsShows the public shared foundation surface.
packages/…/types.tsDefines the cross-SDK file, route, request, response, and JSON contracts.
packages/…/error.tsDefines the serializable UploadThing error model and status mapping.
packages/…/tagged-errors.tsDefines typed local failures used by validation and transport flows.
packages/…/utils.tsConnects route defaults, MIME matching, size parsing, and URL resolution.
packages/…/crypto.tsDefines payload signatures, signed URLs, and application-bound file keys.
packages/…/component-utils.tsConnects route rules to browser accept lists and shared component behavior.
packages/…/effect.tsDefines the browser fetch boundary and response error handling.
packages/…/route-config.tsShows how shared rules become the server's upload gate.
packages/…/ut-file.tsShows the common file object used when a runtime lacks a native File.
packages/…/index.tsDefines the MIME and extension lookup contract used by shared validation.