Write a Route Handler that partner sites can call from the browser, with CORS and a preflight
Two partner sites, https://brewbox.example and https://kettleclub.example, show Leaf and Kettle's stock on their own pages. A script there calls GET https://leafandkettle.com/api/stock/<slug> with fetch, sending the partner's key, which tells the shop whose page the call came from, as Authorization: Bearer <key>. Write app/api/stock/[slug]/route.ts so those pages can read the answers, and no other site's can.
GET: without a key that isPartnerKey from @/lib/partners accepts, answer 401 with { error: "Send a partner key." }. For a tea getStock from @/lib/stock doesn't find, answer 404 with { error: "No tea called rooibos." }, with the slug in it. Otherwise answer 200 with { slug, name, boxes }.
A page on another origin may read a response only when the response names its origin in Access-Control-Allow-Origin. When the request's Origin header is one of PARTNER_ORIGINS, add that header, set to the origin, to every GET answer, the 401 and 404 included, so the partner's script can read the error too; for any other origin, or none, leave it out. Never answer *. The answer now depends on Origin, so every response, with the header or without, also carries Vary: Origin, and a cache in between never hands one site's answer to another.
The Authorization header makes the browser ask first: before the GET, it sends an OPTIONS request, the preflight, with Origin, Access-Control-Request-Method: GET, and Access-Control-Request-Headers: authorization. Next.js answers OPTIONS itself when the file doesn't export it, with 204 and an Allow header but no CORS headers, so the browser would stop there. Export OPTIONS: for a partner's origin, answer 204 with Access-Control-Allow-Origin (the origin), Access-Control-Allow-Methods: GET, Access-Control-Allow-Headers: Authorization, Access-Control-Max-Age: 600, and Vary: Origin; for any other origin, answer 403 with only Vary: Origin.
// OPTIONS /api/stock/mint, Origin: https://brewbox.example
// → 204, Access-Control-Allow-Origin: https://brewbox.example, Access-Control-Allow-Methods: GET,
// Access-Control-Allow-Headers: Authorization, Access-Control-Max-Age: 600, Vary: Origin
// GET /api/stock/oolong, Origin: https://kettleclub.example, Authorization: Bearer kc-77d2
// → 200 { slug: "oolong", name: "Oolong tea", boxes: 4 }, Access-Control-Allow-Origin: https://kettleclub.example
// GET /api/stock/oolong, Origin: https://evil.example, Authorization: Bearer kc-77d2
// → 200 { slug: "oolong", name: "Oolong tea", boxes: 4 }, and no Access-Control-Allow-Origin: the page can't read it
app/api/stock/[slug]/route.tsimport { NextResponse, type NextRequest } from "next/server";
import { PARTNER_ORIGINS, isPartnerKey } from "@/lib/partners";
import { getStock } from "@/lib/stock";
export async function GET(request: NextRequest, context: RouteContext<"/api/stock/[slug]">) {
const { slug } = await context.params;
const stock = await getStock(slug);
return NextResponse.json({ slug, ...stock });
}
lib/stock.ts (read-only)export type Stock = { name: string; boxes: number };
const stock: Record<string, Stock> = {
green: { name: "Green tea", boxes: 12 },
mint: { name: "Mint tea", boxes: 0 },
oolong: { name: "Oolong tea", boxes: 4 },
};
/** The tea's name and how many boxes are left, or undefined when the shop doesn't sell it. */
export async function getStock(slug: string): Promise<Stock | undefined> {
return stock[slug];
}
lib/partners.ts (read-only)/** The sites whose pages may read the stock API from the browser, by origin. */
export const PARTNER_ORIGINS = ["https://brewbox.example", "https://kettleclub.example"];
const keys = new Set(["bb-41c9", "kc-77d2"]);
/** Whether this is one of the partners' API keys. */
export function isPartnerKey(key: string): boolean {
return keys.has(key);
}
- answers a partner's preflight with the CORS headers the browser asks for
- refuses the preflight of any other site, even one whose address starts the same
- gives a partner's page the stock, readable from its origin
- leaves Access-Control-Allow-Origin out for other sites and for requests with no Origin
- answers 401 without a partner key, readable by the partner's page
- answers 404 for a tea the shop doesn't sell, readable by the partner's page
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…