Svelte SDK
The Svelte SDK is the Svelte-facing library for UploadThing. It connects a typed UploadThing file router to Svelte components or custom file controls, then manages file selection, upload progress, completion, errors, and cancellation in the browser. The package is designed for Svelte and SvelteKit applications; its server route remains part of the main UploadThing server integration.
Package boundary
The package exposes a small public surface that separates ready-made UI from lower-level upload control. @uploadthing/svelte exports typed helper generation, upload components, the dropzone action factory, and the types that connect a client endpoint to its server file router (packages/…/index.ts• L1-7).
The package relies on the shared UploadThing client contract rather than defining a separate upload protocol. Its peer dependencies keep the Svelte runtime and the main uploadthing package aligned, while the published package also provides an optional stylesheet entry (packages/…/package.json• L24-42).
The Svelte adapter is one part of the framework SDK family. Use the framework SDK overview for the shared product model, then compare the Vue SDK, Solid SDK, Nuxt SDK, and Expo SDK pages for other client integrations.
Typed helper layer
generateSvelteHelpers is the main integration point for an application. The application supplies its FileRouter type and may set the UploadThing API URL or a custom fetch; the returned helpers then keep endpoint names, endpoint input, output data, and error shapes tied to that router (packages/…/create-uploadthing.ts• L146-171, packages/…/types.ts• L20-50).
The helper layer supports two usage paths:
createUploaderpackages an endpoint and its upload callbacks into theuploaderobject expected by the built-in components (packages/…/create-uploadthing.ts• L139-144).createUploadThingcreates an imperative upload handle for a custom Svelte control. ItsstartUploadmethod accepts selected files and endpoint input when the route defines input (packages/…/create-uploadthing.ts• L158-163, packages/…/types.ts• L52-120).
The upload handle uses the shared client uploader to request and perform uploads. It exposes a readable isUploading store and a route configuration store, so Svelte code can react to upload state and to the file limits reported by the server (packages/…/create-uploadthing.ts• L35-49, packages/…/create-uploadthing.ts• L60-136).
Upload lifecycle
The Svelte client turns a selected file set into an UploadThing request and reports each important stage through typed callbacks. The lifecycle begins with optional file changes and ends with either client completion, an upload error, or an abort signal.
sequenceDiagram
participant User
participant SvelteUI as Svelte UI or custom control
participant Helpers as Typed helper
participant API as UploadThing API route
participant Store as File storage
User->>SvelteUI: Select, drop, or paste files
SvelteUI->>SvelteUI: Apply client file rules
SvelteUI->>Helpers: startUpload(files, input)
Helpers->>API: Request upload details
API-->>Helpers: Return upload details and route data
Helpers->>Store: Upload files
Store-->>Helpers: Report progress and results
Helpers-->>SvelteUI: Update state and callbacks
SvelteUI-->>User: Show progress, completion, error, or cancel state
Before the request starts, the client can change the files through onBeforeUploadBegin. During the transfer it can report file progress with a chosen granularity and notify the start of each file. On completion it returns the uploaded file data; on failure it provides a typed UploadThing error. An AbortSignal cancels the transfer and built-in components map that result to onUploadAborted (packages/…/create-uploadthing.ts• L60-123, packages/…/types.ts• L52-120).
Built-in upload controls
The package provides controls for the two common selection patterns. Both controls use the same typed uploader contract and the same upload lifecycle, so the choice changes the user interaction rather than the server integration (packages/…/index.ts• L1-3, packages/…/types.ts• L122-200).
| Control | User interaction | Upload behavior |
|---|---|---|
UploadButton | Opens the browser file picker | Uploads on selection in auto mode or waits for a second action in manual mode (packages/…/UploadButton.svelte• L185-213) |
UploadDropzone | Accepts file-picker selection, drag and drop, and optional paste | Applies the endpoint's accepted types and count before it starts an automatic or manual upload (packages/…/UploadDropzone.svelte• L117-140, packages/…/UploadDropzone.svelte• L159-183) |
Uploader | Shows both the button and dropzone | Serves as a combined example-level control that passes one uploader to both child controls (packages/…/Uploader.svelte• L14-32) |
The components load endpoint metadata before they present the final file rules. This lets the UI show the permitted file types and content limits from the server route instead of relying only on duplicated client settings (packages/…/create-uploadthing.ts• L27-33, packages/…/UploadButton.svelte• L103-109, packages/…/UploadDropzone.svelte• L127-142).
File selection and dropzone behavior
The dropzone layer is a Svelte action-based file selection system. createDropzone combines a hidden file input with a root action that handles keyboard activation, drag state, document-level drag protection, and accepted-file filtering (packages/…/create-dropzone.ts• L34-52, packages/…/create-dropzone.ts• L279-356).
The selection state is kept separate from the upload state. A drop or file change first produces accepted files based on type, size, and quantity rules. The owning control then decides whether to store those files for manual submission or pass them to the upload helper at once (packages/…/create-dropzone.ts• L187-228, packages/…/UploadDropzone.svelte• L117-125). Clipboard files follow the same path when paste support is enabled (packages/…/shared.ts• L1-11, packages/…/UploadDropzone.svelte• L159-183).
UI state and customization
The built-in controls present a small state model: disabled, loading route information, ready for files, or uploading. Progress and cancellation are part of the upload state, while the selected files remain available for manual mode and change callbacks (packages/…/UploadButton.svelte• L106-110, packages/…/UploadDropzone.svelte• L185-200).
Each control supports named slots and appearance fields for its main visual regions. Appearance callbacks receive the current files, endpoint file types, drag state where relevant, upload progress, and Svelte runtime identity. Applications can therefore replace content or classes without changing the upload contract (packages/…/UploadButton.svelte• L36-53, packages/…/UploadDropzone.svelte• L36-55). The package's stylesheet entry supplies its Tailwind utility base, while the components also accept a class-list merger for projects that need custom class conflict handling (packages/…/styles.css• L1-3, packages/…/types.ts• L173-183).
SvelteKit integration
In a SvelteKit application, the server and client meet through a normal route handler. The server defines and exports the UploadThing file router, creates GET and POST handlers for the API route, and keeps the UploadThing token on the server (examples/…/uploadthing.ts• L3-17, examples/…/+server.ts• L1-14).
The client imports the router type into a small helper module. It generates typed helpers once, creates an endpoint-specific uploader, and passes that object to either built-in control. A custom file input can instead call startUpload directly (examples/…/uploadthing.ts• L1-6, examples/…/+page.svelte• L1-47).
flowchart LR
Router[Server FileRouter] --> Handler[SvelteKit API route]
Router --> Helpers[generateSvelteHelpers]
Helpers --> Uploader[Endpoint uploader]
Uploader --> Controls[UploadButton or UploadDropzone]
Helpers --> Custom[Custom file control]
Controls --> Handler
Custom --> Handler
Handler --> Storage[UploadThing storage]
This arrangement keeps the secret token and route rules on the server while allowing the client to use the router type and public route metadata. The example imports @uploadthing/…/styles.css when it uses the packaged controls (examples/…/+page.svelte• L1-8).
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to
UploadthingComponentPropsmust stay aligned with both built-in controls and the endpoint input inferred from the server router. - Changes to the helper URL or
fetchcontract must preserve the same request behavior forcreateUploadThing,createUploader, and the built-in controls. - Changes to route metadata loading must keep displayed file limits aligned with the server route and with dropzone acceptance rules.
- Changes to abort or progress handling must preserve callback behavior in both manual and automatic upload modes.
- Changes to the package exports or stylesheet export must remain usable from the published
@uploadthing/svelteentry points.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The client points to the wrong UploadThing API URL | Route metadata and upload requests fail before files can upload | Set the helper URL to the application's UploadThing route or use the supported inferred URL (packages/…/types.ts• L20-30). |
| The server route and client router type use different endpoint names or input shapes | The UI may target a missing route or send invalid endpoint input | Generate helpers from the same FileRouter type that the server exports (examples/…/uploadthing.ts• L1-6). |
| A drop or paste includes files outside the route rules | The selection is rejected and the upload receives no accepted files | Let the dropzone apply type, size, and quantity checks before starting the upload (packages/…/create-dropzone.ts• L187-212). |
| A custom control ignores the returned abort signal or upload callbacks | Users lose cancellation or state feedback even though the transfer is active | Use the helper lifecycle and handle UploadAbortedError separately from upload errors (packages/…/create-uploadthing.ts• L99-122). |
| The server token is exposed through client code | Anyone who receives the token can misuse the UploadThing API | Read the token in the server route and pass it only to createRouteHandler (examples/…/+server.ts• L1-14). |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.ts | Shows the public Svelte SDK surface. |
| packages/…/create-uploadthing.ts | Defines typed helper creation and the shared upload lifecycle. |
| packages/…/types.ts | Defines the client and component contracts that connect endpoint types to Svelte usage. |
| packages/…/UploadButton.svelte | Shows file-picker upload behavior and manual versus automatic mode. |
| packages/…/UploadDropzone.svelte | Shows drag-and-drop UI and its connection to the upload lifecycle. |
| packages/…/create-dropzone.ts | Defines the reusable Svelte file selection action and acceptance state. |
| examples/…/uploadthing.ts | Shows the server file router that supplies the client types and route rules. |
| examples/…/+server.ts | Shows the SvelteKit API boundary that handles UploadThing requests. |
| examples/…/+page.svelte | Shows built-in controls and a custom file input using the SDK. |