# A numerology callable on Firebase Cloud Functions

> For a Firebase app the API key belongs in Secret Manager and the call in a Cloud Function. A callable (`onCall`) function gives you the signed-in user for free and a typed error channel back to the client. This one returns a Pythagorean life-path number.

Source: https://astronode.dev/integrations/firebase-cloud-functions · Updated: 2026-12-01

**How this page was verified:** 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

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

```ts
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):

```ts
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_master` is `true` for 11, 22 and 33, which are not reduced further; branch on it rather than on the value.
- `{ dob: "24/04/1973" }` came back as `invalid-argument` ("Request validation failed"): `dob` must be `YYYY-MM-DD`. A call without a signed-in user is refused with `unauthenticated` before 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:

```json
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](https://astronode.dev/docs/errors/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`, `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.

## Endpoints

- [`POST /v1/numerology/pythagorean`](https://astronode.dev/capabilities/numerology/numerology-pythagorean) - Pythagorean Numerology API
