How this page was checked: The server/api/dasha.post.ts handler was executed on Node 24 against the in-memory API, with defineEventHandler, readBody, useRuntimeConfig, createError and $fetch replaced by small stand-ins (h3 and ofetch are not installed on the build machine). The table and the 401 below come from that run. nuxt.config.ts and the .vue page were not run or rendered.
The key goes in private runtimeConfig
nuxt.config.ts:
export default defineNuxtConfig({
runtimeConfig: {
// Server-only. Declared here so NUXT_ASTRONODE_API_KEY can override it at runtime.
astronodeApiKey: "",
astronodeBaseUrl: "https://api.astronode.dev",
public: {
// Serialised into every rendered page. Nothing secret goes here.
},
},
});# .env - read by `nuxi dev`, NOT by the built server NUXT_ASTRONODE_API_KEY=aie_live_...
- Keys at the top level of
runtimeConfigexist only on the server. Keys underpublicare sent to the browser with every page. NUXT_ASTRONODE_API_KEYoverridesastronodeApiKeyonly because that key is declared, even as an empty string. An environment variable with no matching key innuxt.config.tsis ignored, and the handler sendsBearerwith nothing after it.- The production server (
node .output/server/index.mjs) does not load.env. Set the variable in the host's environment.
The server route
server/api/dasha.post.ts - the .post suffix makes it answer POST only:
type Period = { lord: string; start: string; end: string; years: number };
type Vimshottari = { birth_balance: { lord: string; remaining_years: number }; mahadasha: Period[] };
const BIRTH_FIELDS = ["date", "time", "tz_offset_minutes", "lat", "lng"] as const;
export default defineEventHandler(async (event) => {
const { astronodeApiKey, astronodeBaseUrl } = useRuntimeConfig(event);
const input = await readBody<Record<string, unknown>>(event);
// The birth input is strict: forward exactly five fields.
const birth = Object.fromEntries(BIRTH_FIELDS.map((k) => [k, input?.[k]]));
const res = await $fetch<{ data: Vimshottari }>("/v1/dasha/vimshottari", {
baseURL: astronodeBaseUrl,
method: "POST",
headers: { Authorization: `Bearer ${astronodeApiKey}` },
body: birth,
}).catch((err) => {
throw createError({
statusCode: err.statusCode ?? 502,
statusMessage: err.data?.error?.code ?? "UPSTREAM_ERROR",
});
});
// 11 KB of mahadasha + antardasha in, ten short rows out.
const now = Date.now();
return res.data.mahadasha.map((p) => ({
lord: p.lord,
from: p.start.slice(0, 10),
to: p.end.slice(0, 10),
current: Date.parse(p.start) <= now && now < Date.parse(p.end),
}));
});$fetchJSON-encodes the object body and throws on any non-2xx status; the error carries AstroNode's envelope inerr.data, so thecatchcan pass itscodeon throughcreateError.- The raw dasha response is about 11 KB (every mahadasha with its nine antardashas). The route returns ten short rows, which is all the page shows.
- Copying five named fields drops whatever else the form posts. The run sent an extra
labelfield and still got a 200; passing the body through unchanged would have been a400.
What the route returned
| Lord | From | To | current |
|---|---|---|---|
| Rahu | 1984-11-02 | 1996-08-13 | false |
| Jupiter | 1996-08-13 | 2012-08-13 | false |
| Saturn | 2012-08-13 | 2031-08-13 | true |
| Mercury | 2031-08-13 | 2048-08-13 | false |
| Ketu … Mars | 2048-08-13 | 2098-08-13 | false (five rows) |
| Rahu | 2098-08-13 | 2116-08-14 | false |
The first row is the balance of the dasha running at birth (birth_balance: Rahu, 11.78 years left), not a full 18-year Rahu period, which is why the cycle comes round to Rahu again in the tenth row. current is worked out in the route from the dates, so the page needs only the Free-plan endpoint. If you also want the antardasha running on a given date, /v1/dasha/lookup answers that directly and needs the Hobby plan. How the balance is computed: Vimshottari dasha calculation.
The page: useFetch on submit
pages/dasha.vue:
<script setup lang="ts">
const birth = reactive({ date: "", time: "", tz_offset_minutes: 330, lat: 0, lng: 0 });
const { data: periods, error, status, execute } = useFetch("/api/dasha", {
method: "POST",
body: birth,
immediate: false, // nothing to fetch until the form is submitted
watch: false, // and no refetch on every keystroke
});
</script>
<template>
<form @submit.prevent="execute()">
<input v-model="birth.date" type="date" required />
<input v-model="birth.time" type="time" required />
<input v-model.number="birth.tz_offset_minutes" type="number" />
<input v-model.number="birth.lat" type="number" step="any" />
<input v-model.number="birth.lng" type="number" step="any" />
<button :disabled="status === 'pending'">Show dashas</button>
</form>
<p v-if="error" role="alert">{{ error.statusMessage }}</p>
<ol v-else-if="periods">
<li v-for="p in periods" :key="p.from" :aria-current="p.current ? 'step' : undefined">
{{ p.lord }}: {{ p.from }} to {{ p.to }}
</li>
</ol>
</template>useFetch normally runs while the page renders and re-runs when a reactive option changes. With a form behind it you want neither, hence immediate: false and watch: false. The .number modifiers matter: without them Vue hands the route strings such as "330", and AstroNode rejects a string where it expects a number with 400 VALIDATION_ERROR.
The mistake that bites first: public runtimeConfig
Moving astronodeApiKey under public, or setting NUXT_PUBLIC_ASTRONODE_API_KEY, makes useRuntimeConfig() return the key in components too, so a direct call from the page suddenly works. It works because Nuxt has serialised the key into the HTML of every page it renders. Anyone can read it with View Source. Rotate any key that has been there.
The opposite problem looks like this: the route answers 401 with statusMessage: "UNAUTHORIZED" (from the run, with the variable empty). That means the server had no key. Check the variable name against the declared key (NUXT_ plus the key in upper snake case) and check that the production process has it. See unauthorized.
Questions
- Which AstroNode endpoints does the Nuxt example call?
- 1 endpoint: Vimshottari Dasha (
POST /v1/dasha/vimshottari). - Was the Nuxt code on this page actually run?
- The
server/api/dasha.post.tshandler was executed on Node 24 against the in-memory API, withdefineEventHandler,readBody,useRuntimeConfig,createErrorand$fetchreplaced by small stand-ins (h3 and ofetch are not installed on the build machine). The table and the 401 below come from that run.nuxt.config.tsand the.vuepage were not run or rendered.