How this page was checked: The handler body was executed on Node 24 against the in-memory API with minimal stand-ins for onCall, HttpsError and defineSecret (the firebase-functions package is not installed on the build machine): the result, the invalid-argument mapping and the unbound-secret 401 are from that run. Deploying to Firebase was not exercised.
Store the key as a secret
firebase functions:secrets:set ASTRONODE_API_KEY
The CLI prompts for the value and stores it in Google Cloud Secret Manager. Deploying functions requires the project to be on the Blaze (pay-as-you-go) plan, which is also what allows outbound calls to non-Google hosts such as api.astronode.dev.
The function
functions/src/index.ts (Node 18+ runtime, so fetch is built in):
import { onCall, HttpsError } from "firebase-functions/v2/https";
import { defineSecret } from "firebase-functions/params";
const astronodeKey = defineSecret("ASTRONODE_API_KEY");
const ASTRONODE = process.env.ASTRONODE_BASE_URL ?? "https://api.astronode.dev";
export const lifePath = onCall({ secrets: [astronodeKey], region: "asia-south1" }, async (request) => {
if (!request.auth) throw new HttpsError("unauthenticated", "Sign in first.");
const { dob, name } = request.data as { dob: string; name?: string };
const res = await fetch(`${ASTRONODE}/v1/numerology/pythagorean`, {
method: "POST",
headers: {
Authorization: `Bearer ${astronodeKey.value()}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ dob, number: "life-path", ...(name ? { name } : {}) }),
});
const json = await res.json();
if (!res.ok) {
throw new HttpsError(res.status === 400 ? "invalid-argument" : "unavailable", json.error.message);
}
return json.data.result; // { value, is_master, reduction_chain }
});Throwing HttpsError is what makes the client see a meaningful code; any other exception reaches the client as a generic internal error. Mapping AstroNode's 400 to invalid-argument lets the app highlight the date field, and everything else becomes unavailable, which the client can retry.
Calling it and what comes back
Web or React Native client (modular SDK):
import { getFunctions, httpsCallable } from "firebase/functions";
const lifePath = httpsCallable(getFunctions(app, "asia-south1"), "lifePath");
const { data } = await lifePath({ dob: "1973-04-24" });
// data = { value: 3, is_master: false, reduction_chain: [12, 3] }{ dob: "1973-04-24" }returned `{ value: 3, is_master: false, reduction_chain: [12, 3] }` - the chain shows the intermediate sum, which is worth displaying ("12 → 3") because users check numerology by hand.is_masteristruefor 11, 22 and 33, which are not reduced further; branch on it rather than on the value.{ dob: "24/04/1973" }came back asinvalid-argument("Request validation failed"):dobmust beYYYY-MM-DD. A call without a signed-in user is refused withunauthenticatedbefore AstroNode is called.
The mistake that bites first: a secret the function cannot see
defineSecret declares the secret; listing it in the function's secrets option is what mounts it into that function. Forget the option (or add a second function that reuses astronodeKey without listing it) and astronodeKey.value() has nothing to return, so the request goes out with an empty bearer token. AstroNode's answer, from the run with the secret unbound:
HTTP 401
{ "error": { "code": "UNAUTHORIZED", "message": "Missing or invalid Authorization header" } }If one function works and another returns 401 with the same code, compare their secrets arrays first. Details of the 401 are on the UNAUTHORIZED page.
Questions
- Which AstroNode endpoints does the Firebase Cloud Functions example call?
- 1 endpoint: Pythagorean Numerology (
POST /v1/numerology/pythagorean). - Was the Firebase Cloud Functions code on this page actually run?
- The handler body was executed on Node 24 against the in-memory API with minimal stand-ins for
onCall,HttpsErroranddefineSecret(thefirebase-functionspackage is not installed on the build machine): the result, theinvalid-argumentmapping and the unbound-secret 401 are from that run. Deploying to Firebase was not exercised.