โ† Integrations

Serverless ยท TypeScript

Call the astrology API from a Supabase Edge Function

If your app already runs on Supabase, an Edge Function is the natural home for the AstroNode key: it is server-side, it scales to zero, and supabase-js can call it with the user's session attached. Here is a function that returns tropical natal planets, run against the API.

Updated

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

bash
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:

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

ts
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 },
});
First four rows the function returned (tropical zodiac, Placidus houses)
planetsigndegreehouse
SunTaurus4.167
MoonCapricorn20.284
MercuryAries10.496
VenusTaurus7.977

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 Authorization header triggers an OPTIONS request 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. Handle OPTIONS before reading the body, and add corsHeaders to every response, errors included.
  • 401 before your code runs. Functions verify the caller's JWT by default. Calling with fetch and no Authorization header is rejected by the platform; use supabase.functions.invoke, or deploy with --no-verify-jwt only 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; log json.error.code and res.status instead.

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.serve shim, against the in-memory API - both the OPTIONS preflight and the POST. Not exercised: the Supabase runtime, CLI deploy and JWT verification.