Astro
Mount the Files gateway in an Astro endpoint. createRouteHandler returns { GET, POST, PUT } over the Web Request - runs on Node and edge adapters.
files-sdk/astro mounts a createFilesRouter in an Astro endpoint. Astro hands each route a Web Request on context.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 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/astro";
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 prerender = false;
export const { GET, POST, PUT } = createRouteHandler(router);
Cross-origin uploads
Astro’s security.checkOrigin (on by default) returns 403 for a cross-origin POST or PUT whose Content-Type is form-like (text/plain, multipart/form-data, application/x-www-form-urlencoded) or missing. Explicit-key uploads send the file’s own type, so a cross-origin upload of a .txt file never reaches the gateway. Same-origin apps are unaffected. If the gateway serves other origins, Astro has no per-origin allowlist, so set security: { checkOrigin: false } in astro.config.mjs and let allowedOrigins do the origin check (this turns Astro’s check off for every route). Avoid Astro 7.3.6, whose rewritten check also rejected cross-origin JSON requests; 7.3.7 fixed it.
See the gateway options for the full configuration and the authorize model for locking it down.