Skip to content

Enable CORS

Let a browser app on another origin call your Flare HTTP API with host.http.cors and automatic preflight.

AI generated, pending review Updated 10 days ago · Flare 0.3

You have a browser front end on https://app.example.com calling a Flare API on a different origin, and the browser is blocking the responses. Declare one CORS policy with host.http.cors(config). You don’t write an OPTIONS handler and you don’t set Access-Control-* headers yourself.

  1. Build a CorsConfig: at minimum, the origins you allow.
  2. Register it once on the arc with host.http.cors(config).
  3. Leave preflight to Flare.
import { FlareHost, FlareResponse } from "@flare-ts/core";
import { node } from "@flare-ts/core/node";
const host = new FlareHost(node);
host.http.cors({
origins: ["https://app.example.com"],
credentials: true,
});
host.http.get("/users", () => new FlareResponse(200, { users: [] }));
const app = host.build();
app.run();

Every route on the arc is covered. Cross-origin requests from allowed origins get Access-Control-Allow-Origin, and preflight gets 204 with the right headers before your handler runs.

Only origins is required.

FieldRequired?What it does
originsYes'*', a single origin, an allowlist array, or (origin) => boolean | Promise<boolean>
methodsNoAllowed methods; omitted → derived from handlers on each path at build()
headersNoAllowed request headers on preflight
exposeNoResponse headers exposed to client JS
credentialsNoWhether credentials are allowed. Incompatible with origins: '*'
maxAgeNoPreflight cache seconds (default 7200)
import { FlareHost, FlareResponse } from "@flare-ts/core";
import { node } from "@flare-ts/core/node";
const host = new FlareHost(node);
host.http.cors({
origins: ["https://app.example.com"],
credentials: true,
});
host.http.get("/users", () => new FlareResponse(200, { users: [] }));
host.http.group("/public", (g) => {
g.cors({ origins: "*", credentials: false });
g.get("/status", () => new FlareResponse(200, { ok: true }));
return g.register();
});
const app = host.build();
app.run();

Routes under /public use the wildcard policy. Everything else keeps the arc policy.

  • A denied origin gets 204 with no Access-Control-Allow-Origin, not 403. The browser enforces the block.
  • Leave methods off unless you need to override. Flare derives the set from registered handlers per path.