TypeScript and JavaScript quickstart

You need Node.js 18 or later. The client has no dependencies and comes with type declarations for every operation.

1. Install

The client is not on npm yet, so install this release from our site:

npm install https://developers.astrologics.in/downloads/sdks/astrologics-typescript-0.1.0.tgz

2. Set your key

Create a test key. Test keys run the real engine and are never charged.

export ASTROLOGICS_API_KEY=al_test_YOUR_KEY

Use a secret key only on a server, never in a web page or a mobile app.

3. Make your first call

Save as first-call.mjs and run node first-call.mjs.

import { AstroLogics } from "astrologics";

const client = new AstroLogics({ apiKey: process.env.ASTROLOGICS_API_KEY });

const { data, meta } = await client.getKundli({
  birth: { date: "1990-08-13", time: "06:30", place: { latitude: 28.6139, longitude: 77.209 } },
});
console.log(data.lagna.sign, meta.mode, meta.credits_charged);

It prints the lagna's sign, then test 0: the call ran in test mode and cost no credits. A place can also be a place_id or free text; see the birth object.

4. Save a chart

Save as save-chart.mjs. An SVG operation returns a string, and a PNG or PDF operation returns a Uint8Array.

import { writeFile } from "node:fs/promises";
import { AstroLogics } from "astrologics";

const client = new AstroLogics({ apiKey: process.env.ASTROLOGICS_API_KEY });

const svg = await client.getChartSvg({
  varga: "D1",
  birth: { date: "1990-08-13", time: "06:30", place: { latitude: 28.6139, longitude: 77.209 } },
});
await writeFile("chart.svg", svg);

5. Handle errors

Save as handle-errors.mjs. Any failed call throws an ApiError. Branch on code, not on the message, and quote requestId when you write to us. See errors.

import { AstroLogics, ApiError } from "astrologics";

const client = new AstroLogics({ apiKey: process.env.ASTROLOGICS_API_KEY });

try {
  await client.getKundli({ birth: { date: "1990-13-45" } });
} catch (e) {
  if (!(e instanceof ApiError)) throw e;
  console.log(e.status, e.code, e.requestId);
  for (const issue of e.issues) console.log(issue.param, issue.message);
}

ApiError also carries candidates for an ambiguous place and suggestions for an unknown path.

Retries and timeouts

429 and 5xx answers are retried twice by default, honouring Retry-After. quota_exhausted is not retried. To change this, pass maxRetries and timeoutMs (default 60000) to the constructor.

Find a method

Each method is named after the operation's id in the API reference, and its arguments are the operation's parameters and body, side by side. There is one method per public operation: 133 today.

Go live

Swap in a live key (al_live_) when you are ready. Live keys spend your plan's credits, and only successful calls are charged.

Next: SDKs · Next.js tutorial · Python · PHP