# A current-hora service in Deno, with only the permissions it needs

> Deno runs nothing with network or environment access unless you grant it, which suits code that holds an API key. This service answers one question - which planetary hour (hora) is in effect now at a given place - and is started with permission to reach exactly one host.

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

**How this page was verified:** `handler` from `main.ts` was executed on Node 24 against the in-memory API, with a stand-in for `Deno.env.get` over the process environment; `Deno.serve` was not reached because it is guarded by `import.meta.main`. Deno is not installed on the build machine, so the permission flags, `--env-file`, `Deno.serve` and Deno Deploy were not run.

## The service

`main.ts`:

```ts
const BASE = Deno.env.get("ASTRONODE_BASE_URL") ?? "https://api.astronode.dev";
const KEY = Deno.env.get("ASTRONODE_API_KEY");
if (!KEY) throw new Error("ASTRONODE_API_KEY is not set");

type Hora = { name: string; start: string; end: string; lord: string };

async function horas(date: string, tz: number, lat: number, lng: number): Promise<Hora[]> {
  const res = await fetch(`${BASE}/v1/muhurta/hora`, {
    method: "POST",
    headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json" },
    body: JSON.stringify({ date, tz_offset_minutes: tz, lat, lng }),
  });
  const json = await res.json();
  if (!res.ok) throw new Error(`${json.error.code}: ${json.error.message}`);
  return json.data.windows;
}

export async function handler(req: Request): Promise<Response> {
  const q = new URL(req.url).searchParams;
  const tz = Number(q.get("tz") ?? 0); // minutes east of UTC
  const lat = Number(q.get("lat"));
  const lng = Number(q.get("lng"));
  const at = q.has("at") ? new Date(q.get("at")!) : new Date();
  const localDate = (d: Date) => new Date(d.getTime() + tz * 60_000).toISOString().slice(0, 10);

  try {
    // A hora day runs sunrise to sunrise: before local sunrise, "now" is in yesterday's list.
    for (const date of [localDate(at), localDate(new Date(at.getTime() - 86_400_000))]) {
      const current = (await horas(date, tz, lat, lng)).find(
        (h) => at >= new Date(h.start) && at < new Date(h.end),
      );
      if (current) return Response.json({ at: at.toISOString(), hora_day: date, ...current });
    }
    return Response.json({ error: "no hora covers that instant" }, { status: 404 });
  } catch (err) {
    return Response.json({ error: (err as Error).message }, { status: 502 });
  }
}

if (import.meta.main) Deno.serve(handler);
```

Two Deno habits are doing work here. The key is checked at start-up, so a misconfigured deployment fails immediately rather than on the first request. And `handler` is exported with `Deno.serve` behind `import.meta.main`, so `deno test` can import the module and call `handler(new Request(...))` without opening a port.

## Grant exactly what it uses

`deno.json`:

```json
{
  "tasks": {
    "dev": "deno run --watch --env-file=.env --allow-net=api.astronode.dev,0.0.0.0:8000 --allow-env=ASTRONODE_API_KEY,ASTRONODE_BASE_URL main.ts"
  }
}
```

- **`--allow-net` covers listening as well as fetching.** `Deno.serve` binds `0.0.0.0:8000` by default, so that address has to be in the list next to `api.astronode.dev`; with the host alone, the fetch is allowed and the server refuses to start.
- **`--allow-env` with names** grants those variables only. Reading any other variable throws a permission error rather than returning `undefined` - which is why `ASTRONODE_BASE_URL` is listed even though it is optional.
- **`--env-file` loads; it does not grant.** The file's values enter the environment, but the code still needs `--allow-env` to read them. Keep `.env` out of git.

## What it returned

*New York (`tz=-300`, `lat=40.7128`, `lng=-74.006`), weekend of 5-6 December 2026*

| `at` (UTC) | Local | `hora_day` | Hora returned |
| --- | --- | --- | --- |
| 2026-12-05 15:00 | Sat 10:00 | 2026-12-05 | Hora 4 (Sun), 14:25:38-15:12:37Z |
| 2026-12-06 10:00 | Sun 05:00 | 2026-12-05 | Hora 23 (Jupiter), 09:39:26-10:52:32Z |
| 2026-12-06 12:30 | Sun 07:30 | 2026-12-06 | Hora 1 (Sun), from sunrise 12:05:37Z |

The second row is the reason for the loop. At 05:00 on Sunday the calendar date is the 6th, but the Sunday hora sequence only starts at sunrise, so the hour belongs to Saturday's list - the 23rd of its 24 horas. Code that looks only in today's list finds nothing before sunrise (and costs a second call when it does look back). Day horas that Saturday were about 47 minutes and night horas about 73: twelve equal parts of each half of the day, not clock hours. `/v1/muhurta/hora` needs the Hobby plan or above; times come back as UTC instants. Far enough north there is no sunrise at all, and the API says so: for Longyearbyen (78.22° N) it returns `400` "No sunrise at latitude 78.22° on 2026-12-05 (midnight sun or polar night)", which the handler passes on as a `502` with that message.

## On Deno Deploy

The same `main.ts` deploys unchanged: Deno Deploy runs the module, `import.meta.main` is true for the entry point, and `Deno.serve` receives requests on whatever port the platform assigns. Set `ASTRONODE_API_KEY` in the project's environment variables in the Deno Deploy dashboard - there is no `.env` file and no permission flags there, so the narrowing in `deno.json` is a local and self-hosted safeguard, not a production one.

## The mistake that bites first: widening permissions to silence a prompt

The first time a flag is missing, Deno stops with a permission error (or prompts, in an interactive terminal), and the quick fix is `-A`. That grants all network, environment, file and subprocess access - including every environment variable on the machine to every dependency you import. Read the error instead: it names the host or variable it wanted, and adding that one item keeps the guarantee that this process can talk to `api.astronode.dev` and nowhere else.

## Questions

**Which AstroNode endpoints does the Deno example call?**

1 endpoint: Hora (Planetary Hours) (`POST /v1/muhurta/hora`).

**Was the Deno code on this page actually run?**

`handler` from `main.ts` was executed on Node 24 against the in-memory API, with a stand-in for `Deno.env.get` over the process environment; `Deno.serve` was not reached because it is guarded by `import.meta.main`. Deno is not installed on the build machine, so the permission flags, `--env-file`, `Deno.serve` and Deno Deploy were not run.

## Endpoints

- [`POST /v1/muhurta/hora`](https://astronode.dev/capabilities/vedic/muhurta-hora) - Hora (Planetary Hours) API
