How this page was checked: The exported GET(request) in api/moon.ts was executed on Node 24 against the in-memory API with web-standard Request objects - the same signature Vercel calls. Not run: the Vercel CLI, deployment, the CDN cache and region placement.
The function
For a project that is not Next.js (a static site, a Vite app), a file in api/ that exports a function named after an HTTP method becomes an endpoint at that path. It runs on Node.js, so process.env works as usual.
api/moon.ts - served at /api/moon:
const BASE = process.env.ASTRONODE_BASE_URL ?? "https://api.astronode.dev";
export async function GET(request: Request): Promise<Response> {
const at = new URL(request.url).searchParams.get("at") ?? new Date().toISOString();
if (Number.isNaN(Date.parse(at))) {
return Response.json({ error: "at must be an ISO 8601 date-time" }, { status: 400 });
}
const res = await fetch(`${BASE}/v1/western/lunar-metrics`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.ASTRONODE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ at }),
});
const json = await res.json();
if (!res.ok) {
console.error("astronode", res.status, json.error.code);
return Response.json({ error: "upstream" }, { status: 502 });
}
const { phase_name, illumination, moon_sign, void_of_course } = json.data;
return Response.json(
{ at, phase: phase_name, illumination_pct: Math.round(illumination * 1000) / 10, moon_sign, void_of_course },
// Vercel's CDN honours s-maxage on function responses: one call per URL per hour.
{ headers: { "Cache-Control": "public, s-maxage=3600, stale-while-revalidate=600" } },
);
}Validate at before forwarding it: the function is public, so check that it parses as a date and return your own 400 when it does not, rather than spending a call on it.
What it returned
| at | phase | illumination_pct | moon_sign |
|---|---|---|---|
| 2026-12-05T00:00:00Z | waning_crescent | 14.8 | Libra |
| 2026-12-09T00:00:00Z | new_moon | 0 | Sagittarius |
| 2026-12-24T00:00:00Z | full_moon | 100 | Cancer |
moon_sign here is tropical (this is a Western endpoint), so it can differ from the sidereal Moon sign a Vedic endpoint gives for the same instant - see ayanamsa. void_of_course was false at all three instants. The endpoint takes only at (no place: the phase is the same everywhere at a given instant), and it needs the Hobby plan or above.
Environment variables are per environment
vercel env add ASTRONODE_API_KEY production # your aie_live_ key vercel env add ASTRONODE_API_KEY preview # a separate key for branch deployments vercel env pull .env.local # development values, for vercel dev
- Production, Preview and Development are separate scopes. A key added only to Production leaves every pull-request preview with
undefined; the function above then logsastronode 401 UNAUTHORIZEDand returns502(that is what the run without a key produced). - A separate Preview key keeps branch deployments - which anyone with the link can open - off your production quota, and lets you revoke it without touching production.
- Changing a variable does not change running deployments. Redeploy after adding or rotating the key.
Region and caching
vercel.json:
{
"regions": ["fra1"],
"functions": {
"api/moon.ts": { "maxDuration": 10 }
}
}Functions run in one region unless you configure more, and new projects default to iad1 (Washington, D.C.). Pick the region closest to most of your users. With s-maxage=3600 the CDN serves repeat requests for the same URL from its cache, so the function - and AstroNode - is called roughly once an hour per distinct at, and stale-while-revalidate lets the CDN keep answering while it refreshes. Requests without at share the bare /api/moon URL, so "the Moon now" is at most an hour old, which suits a phase display: illumination changes by a few per cent a day.
The mistake that bites first: the front-end prefix
Projects that use api/ functions usually have a front-end build in the same repository, and that build has its own rule for exposing variables - Vite inlines anything prefixed VITE_ into the JavaScript it ships. Naming the key VITE_ASTRONODE_API_KEY so the page can call AstroNode directly puts it in a public file. Keep the name unprefixed, call /api/moon from the page, and if a prefixed key was ever built and deployed, revoke it and issue a new one.
Questions
- Which AstroNode endpoints does the Vercel Functions example call?
- 1 endpoint: Moon Phase and Void-of-Course (
POST /v1/western/lunar-metrics). - Was the Vercel Functions code on this page actually run?
- The exported
GET(request)inapi/moon.tswas executed on Node 24 against the in-memory API with web-standardRequestobjects - the same signature Vercel calls. Not run: the Vercel CLI, deployment, the CDN cache and region placement.