# A Vimshottari dasha timeline in Nuxt 3, with the key in runtimeConfig

> Nuxt gives you a server inside the same project: anything under `server/api` runs in Nitro and never ships to the browser. That is where the AstroNode key and the call belong; the page only talks to `/api/dasha`.

Source: https://astronode.dev/integrations/nuxt · Updated: 2026-12-05

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

```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.
    },
  },
});
```

```bash
# .env - read by `nuxi dev`, NOT by the built server
NUXT_ASTRONODE_API_KEY=aie_live_...
```

- Keys at the top level of `runtimeConfig` exist only on the server. Keys under `public` are sent to the browser with every page.
- `NUXT_ASTRONODE_API_KEY` overrides `astronodeApiKey` only because that key is declared, even as an empty string. An environment variable with no matching key in `nuxt.config.ts` is ignored, and the handler sends `Bearer ` with 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:

```ts
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),
  }));
});
```

- `$fetch` JSON-encodes the object body and throws on any non-2xx status; the error carries AstroNode's envelope in `err.data`, so the `catch` can pass its `code` on through `createError`.
- 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 `label` field and still got a 200; passing the body through unchanged would have been a `400`.

## What the route returned

*2 November 1984, 23:10, Pune (`tz_offset_minutes: 330`), run on 5 December 2026*

| 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`](https://astronode.dev/capabilities/vedic/dasha-lookup) answers that directly and needs the Hobby plan. How the balance is computed: [Vimshottari dasha calculation](https://astronode.dev/guides/vimshottari-dasha-calculation).

## The page: useFetch on submit

`pages/dasha.vue`:

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

## Endpoints

- [`POST /v1/dasha/vimshottari`](https://astronode.dev/capabilities/vedic/dasha-vimshottari) - Vimshottari Dasha API
