How this page was checked: The function body was executed unchanged on Node 24 behind a two-line Deno.env / Deno.serve shim, against the in-memory API - both the OPTIONS preflight and the POST. Not exercised: the Supabase runtime, CLI deploy and JWT verification.
Create it and set the secret
supabase functions new natal-planets supabase secrets set ASTRONODE_API_KEY=aie_live_... supabase functions deploy natal-planets
Secrets set this way are exposed to every function as environment variables (Deno.env.get). For local work, supabase functions serve reads them from an env file you pass with --env-file - keep that file out of git.
The function
supabase/functions/natal-planets/index.ts:
const corsHeaders = {
"Access-Control-Allow-Origin": "https://your-app.example.com",
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
"Access-Control-Allow-Methods": "POST, OPTIONS",
};
const ASTRONODE = Deno.env.get("ASTRONODE_BASE_URL") ?? "https://api.astronode.dev";
Deno.serve(async (req) => {
// The browser's preflight carries no body and no JWT - answer it first.
if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders });
const { date, time, tz_offset_minutes, lat, lng } = await req.json();
const res = await fetch(`${ASTRONODE}/v1/western/natal/planets`, {
method: "POST",
headers: {
Authorization: `Bearer ${Deno.env.get("ASTRONODE_API_KEY")}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ date, time, tz_offset_minutes, lat, lng, house_system: "placidus" }),
});
const json = await res.json();
const payload = res.ok
? json.data.planets.map((p) => ({ planet: p.planet, sign: p.sign, degree: p.degree_in_sign, house: p.house, retrograde: p.retrograde }))
: { error: json.error.code };
return new Response(JSON.stringify(payload), {
status: res.ok ? 200 : res.status,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
});Destructuring the five birth fields does two jobs: it drops anything else the client sent (the API rejects unknown keys), and it documents the contract in one line. The Authorization header that arrives from the browser is the user's Supabase JWT; the one sent to AstroNode is your API key - two different credentials on two different hops.
Calling it from the app
const { data, error } = await supabase.functions.invoke("natal-planets", {
body: { date: "1973-04-24", time: "17:25:00", tz_offset_minutes: 330, lat: 19.076, lng: 72.8777 },
});| planet | sign | degree | house |
|---|---|---|---|
| Sun | Taurus | 4.16 | 7 |
| Moon | Capricorn | 20.28 | 4 |
| Mercury | Aries | 10.49 | 6 |
| Venus | Taurus | 7.97 | 7 |
These are Western (tropical) positions, which is why the Sun is in Taurus here and in Aries on the Vedic endpoints for the same birth - the gap is the ayanamsa (explained here). supabase.functions.invoke attaches the signed-in user's session automatically.
The mistakes that bite first
- CORS preflight. A browser POST with a JSON body and an
Authorizationheader triggers anOPTIONSrequest first. If the function does not answer it with the CORS headers, the browser blocks the real call and you see a CORS error, not your function's error. HandleOPTIONSbefore reading the body, and addcorsHeadersto every response, errors included. - 401 before your code runs. Functions verify the caller's JWT by default. Calling with
fetchand noAuthorizationheader is rejected by the platform; usesupabase.functions.invoke, or deploy with--no-verify-jwtonly if the function is meant to be public - and then add your own abuse limits, because every call spends your AstroNode quota. - Logging the request.
console.log(req.headers)while debugging writes the user's JWT to your logs; logjson.error.codeandres.statusinstead.
Questions
- Which AstroNode endpoints does the Supabase Edge Functions example call?
- 1 endpoint: Western Natal Planets (
POST /v1/western/natal/planets). - Was the Supabase Edge Functions code on this page actually run?
- The function body was executed unchanged on Node 24 behind a two-line
Deno.env/Deno.serveshim, against the in-memory API - both theOPTIONSpreflight and the POST. Not exercised: the Supabase runtime, CLI deploy and JWT verification.