// One serverless function for the whole API. vercel.json sends /api/* here: // GET /api/v1/health GET /api/v1/gpus GET /api/v1/quote GET /api/v1/market GET /api/v1/credits/quote // POST /api/v1/auth/* /api/v1/me /api/v1/ssh-keys /api/v1/instances /api/v1/credits/claim /api/v1/ledger // /api/v1/api-keys GET /api/v1/tick (the cron) POST /api/rpc (the scoped relayer) // GET /api/v1/offers GET /api/v1/route GET /api/v1/openapi.json POST /api/mcp (the MCP server) // What each one answers depends on the environment: see lib/config.mjs. With none, only the public reads, the routing and the keyless MCP tools are open. import { appFromEnv } from "../lib/config.mjs"; import { CACHEABLE } from "../lib/routes.mjs"; let app; export default async function handler(req, res) { app ??= appFromEnv(); const url = new URL(req.url, "http://x"); const query = Object.fromEntries(url.searchParams); // The rewrite passes the original path in ?path=; a direct call carries it in the URL. const path = "/" + String(query.path ?? url.pathname.replace(/^\/api\/?/, "")).replace(/^\/+|\/+$/g, ""); delete query.path; const ip = (req.headers["x-forwarded-for"]?.toString().split(",")[0] ?? "?").trim(); // Vercel parses a JSON body; anything else arrives as text or not at all. let body; try { body = req.body; } catch { body = "{"; } if (Buffer.isBuffer(body)) body = body.toString("utf8"); const r = await app.handle({ method: req.method, path, query, ip, origin: req.headers.origin, auth: req.headers.authorization, headers: req.headers, body }); for (const [k, v] of Object.entries(r.headers)) res.setHeader(k, v); // Only the public reads (the rows marked `cache` in routes.mjs) may sit in the network cache, and only briefly. Anything signed in, the tick and // the relayer are never cached: a cached account or a cached tick would be a wrong answer to someone. if (!r.headers["Cache-Control"]) res.setHeader("Cache-Control", req.method === "GET" && r.status === 200 && CACHEABLE.has(path) ? "public, s-maxage=10, stale-while-revalidate=20" : "no-store"); res.status(r.status).send(r.body); }