SvelteKit
Mount the Files gateway in a SvelteKit +server.ts endpoint. createRouteHandler returns { GET, POST, PUT } over the Web Request, on Node and edge alike.
files-sdk/sveltekit mounts a createFilesRouter in a SvelteKit +server.ts endpoint. SvelteKit hands each route a Web Request on event.request and wants named per-method exports, so — like files-sdk/next — createRouteHandler returns { GET, POST, PUT }: GET serves downloads, POST the JSON verbs, and PUT the upload byte path. The handlers are Web-native, so the route runs on the Node and edge adapters alike.
import { createFiles } from "files-sdk";
import { r2 } from "files-sdk/r2";
import { createFilesRouter } from "files-sdk/api";
import { createRouteHandler } from "files-sdk/sveltekit";
const router = createFilesRouter({
files: createFiles({ adapter: r2({ bucket: "uploads" }) }),
allowedOrigins: ["https://app.example.com"],
authorize: async ({ req }) => {
/* throw to deny, or return a per-user constraint — see /ui/server/authorization */
},
});
export const { GET, POST, PUT } = createRouteHandler(router);
SvelteKit settings that reach the gateway
SvelteKit runs a few checks of its own before a request gets to the gateway. They apply to SvelteKit 2 and 3 alike.
- CSRF. SvelteKit returns 403 for a cross-origin
POSTorPUTwhoseContent-Typelooks like a form submission (text/plain,multipart/form-data, orapplication/x-www-form-urlencoded). SvelteKit 3 also rejects one with noContent-Type. Explicit-key uploads send the file’s own type, so a cross-origin upload of a.txtfile is rejected beforeauthorizeever runs. If the gateway serves other origins, list them incsrf.trustedOriginsas well as inallowedOrigins. That goes insvelte.config.json SvelteKit 2, and in thesveltekit()plugin options invite.config.json SvelteKit 3. - Request body size.
@sveltejs/adapter-nodecaps request bodies at 512 KB unless you raiseBODY_SIZE_LIMIT. Explicit-keyPUTuploads stream through this route, so raise the limit to cover your largest file. Keyless uploads go straight to the provider through a signed URL and never hit the limit. - Disconnects. On
@sveltejs/adapter-node6 (SvelteKit 3),request.signalaborts when the client disconnects. The gateway passes that signal to everyFilescall, so an abandoned upload or proxied download stops instead of running to completion.
See the gateway options for the full configuration and the authorize model for locking it down.