# A streaming AI astrologer chat, ready to run

> A chat page and a zero-dependency Node proxy for the AstroNode chat service: the user enters their birth details once, asks questions, and the astrologer's answer streams in as it is written - with follow-up suggestions, a stop button and clear errors when a quota is reached.

Source: https://astronode.dev/templates/ai-astrologer-chat · Updated: 2026-12-05

## What it does

- `POST /api/chat` validates the message and birth details, turns the IANA zone into the UTC offset at the birth moment, and opens the stream with your key.
- Relays each event to the browser as soon as it is parsed, so the reply renders progressively; the final `done` event is trimmed to the reply and its ids.
- Sends a heartbeat every 15 seconds, aborts the upstream call when the user presses Stop or leaves, and reports a stream that breaks off as an error rather than a short answer.
- Passes quota and validation errors through with their code and `Retry-After`, so the page can say when the user can ask again.
- Keeps the birth details in the browser's localStorage, and renders every value with `textContent`.

## Endpoints and plan

*API calls the template makes*

| Endpoint | Used for | Plan |
| --- | --- | --- |
| `POST /v1/chat/respond/stream` | The answer as Server-Sent Events: tool, delta, replace, then done or error | Free |

Everything it calls is on the Free plan, so a Free test key runs the whole template.

## Where things live

| File | What it holds |
| --- | --- |
| `server.js` | Static files and `POST /api/chat` |
| `src/chat.js` | Validation, the upstream request, relaying events |
| `public/sse.js` | The SSE parser, shared by the server and the browser |
| `public/app.js` | The chat page |
| `test/fake-chat-service.js` | A local fake of the stream for development (`npm run fake-chat`) |

## Keys and deployment

- The API key is read from the server's environment and sent as `Authorization: Bearer <key>`; it never reaches a browser or an app bundle.
- `npm run check` runs 18 tests offline, including the stream parser fed byte by byte (through multi-byte Devanagari characters) and the whole proxy against a fake chat service in `test/` - no key and no network.
- Any Node host that does not buffer responses: behind nginx, turn `proxy_buffering` off for `/api/chat`, or the stream arrives all at once.
- The chat service is at `https://chatbot-api.astronode.dev` (`ASTRONODE_CHAT_URL`). Each message is one call against your plan's monthly quota; the Free plan is also capped at five chat messages a day.
- Put sign-in in front of a public deployment: every message spends your quota.

## Questions

**Does the AI astrologer chat work on the Free plan?**

Yes, up to five messages a day on Free; paid plans have no daily chat cap, only the monthly quota shared with the other endpoints.

**Why proxy the stream instead of calling the chat API from the browser?**

The API key must stay on a server. The proxy adds the key and the birth details and relays the events unchanged, so the browser still sees tokens as they arrive.

**Can I use it without a build step or framework?**

Yes. The page is plain HTML and JavaScript and the server has no dependencies; the parser in `public/sse.js` drops into any front end.

## Related pages

- [Use case: AI astrology chat](https://astronode.dev/use-cases/ai-chat)
- [Recipe: streaming chat answers](https://astronode.dev/recipes/streaming-chat-answers)
- [AI Astrologer](https://astronode.dev/ai-astrologer)
- [All templates](https://astronode.dev/templates)
