JAVASCRIPT TUTORIAL
Astrology API JavaScript Guide
Complete JavaScript tutorial for astrology API. Build horoscope apps with React, Node.js, Next.js with TypeScript support.
January 7, 2026
-
18 min read
Installation
# NPM
npm install @anthropic/vedika
# Yarn
yarn add @anthropic/vedika
# PNPM
pnpm add @anthropic/vedika
Quick Start
import { Vedika } from '@anthropic/vedika';
const vedika = new Vedika({ apiKey: 'YOUR_API_KEY' });
// Generate birth chart
const chart = await vedika.birthChart({
datetime: '1990-05-15T14:30:00+05:30',
latitude: 28.6139,
longitude: 77.2090
});
console.log(`Ascendant: ${chart.ascendant.sign}`);
console.log(`Moon Sign: ${chart.moon.sign}`);
console.log(`Sun Sign: ${chart.sun.sign}`);
// List all planets
chart.planets.forEach(planet => {
console.log(`${planet.name}: ${planet.sign} at ${planet.degree}°`);
});
TypeScript Support
import { Vedika, BirthChart, Planet, Dasha } from '@anthropic/vedika';
interface BirthDetails {
datetime: string;
latitude: number;
longitude: number;
ayanamsa?: 'lahiri' | 'raman' | 'krishnamurti';
}
const vedika = new Vedika({ apiKey: process.env.VEDIKA_API_KEY! });
async function analyzeBirthChart(details: BirthDetails): Promise<{
sunSign: string;
moonSign: string;
ascendant: string;
planets: Planet[];
}> {
const chart: BirthChart = await vedika.birthChart(details);
return {
sunSign: chart.sun.sign,
moonSign: chart.moon.sign,
ascendant: chart.ascendant.sign,
planets: chart.planets
};
}
// Usage with full type safety
const analysis = await analyzeBirthChart({
datetime: '1990-05-15T14:30:00+05:30',
latitude: 28.6139,
longitude: 77.2090,
ayanamsa: 'lahiri'
});
React Hooks
import { useVedika, useBirthChart, useHoroscope } from '@anthropic/vedika/react';
function HoroscopeApp() {
const { isReady } = useVedika();
const { chart, loading, error } = useBirthChart({
datetime: '1990-05-15T14:30:00+05:30',
latitude: 28.6139,
longitude: 77.2090
});
if (loading) return Loading chart...;
if (error) return Error: {error.message};
return (
Your Birth Chart
Ascendant: {chart.ascendant.sign}
Moon Sign: {chart.moon.sign}
Sun Sign: {chart.sun.sign}
Planets
{chart.planets.map(planet => (
-
{planet.name}: {planet.sign} ({planet.degree.toFixed(2)}°)
{planet.retrograde && ' (R)'}
))}
);
}
// Daily horoscope hook
function DailyHoroscope({ sign }) {
const { horoscope, loading } = useHoroscope({
sign,
period: 'daily'
});
if (loading) return Loading...;
return (
{sign} Daily Horoscope
{horoscope.prediction}
Lucky Number: {horoscope.luckyNumber}
Lucky Color: {horoscope.luckyColor}
);
}
Next.js Integration
// app/api/horoscope/route.ts
import { Vedika } from '@anthropic/vedika';
import { NextRequest, NextResponse } from 'next/server';
const vedika = new Vedika({ apiKey: process.env.VEDIKA_API_KEY! });
export async function POST(request: NextRequest) {
const { datetime, latitude, longitude } = await request.json();
try {
const chart = await vedika.birthChart({
datetime,
latitude,
longitude
});
return NextResponse.json({
ascendant: chart.ascendant,
planets: chart.planets,
houses: chart.houses,
yogas: chart.yogas
});
} catch (error) {
return NextResponse.json(
{ error: 'Failed to generate chart' },
{ status: 500 }
);
}
}
// app/horoscope/page.tsx
'use client';
import { useState } from 'react';
export default function HoroscopePage() {
const [chart, setChart] = useState(null);
const [loading, setLoading] = useState(false);
async function generateChart(e: React.FormEvent) {
e.preventDefault();
setLoading(true);
const formData = new FormData(e.target as HTMLFormElement);
const response = await fetch('/api/horoscope', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
datetime: formData.get('datetime'),
latitude: parseFloat(formData.get('latitude') as string),
longitude: parseFloat(formData.get('longitude') as string)
})
});
setChart(await response.json());
setLoading(false);
}
return (
);
}
Node.js Backend
import express from 'express';
import { Vedika } from '@anthropic/vedika';
const app = express();
const vedika = new Vedika({ apiKey: process.env.VEDIKA_API_KEY });
app.use(express.json());
// Birth chart endpoint
app.post('/api/birth-chart', async (req, res) => {
const { datetime, latitude, longitude, ayanamsa } = req.body;
try {
const chart = await vedika.birthChart({
datetime,
latitude,
longitude,
ayanamsa: ayanamsa || 'lahiri'
});
res.json(chart);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Kundli matching endpoint
app.post('/api/match', async (req, res) => {
const { male, female } = req.body;
try {
const match = await vedika.match({ male, female });
res.json({
score: match.totalScore,
percentage: match.percentage,
recommendation: match.recommendation,
kootas: match.kootas
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// AI chat endpoint with streaming
app.post('/api/chat', async (req, res) => {
const { question, birthDetails } = req.body;
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
const stream = await vedika.chatStream({
question,
birthDetails
});
for await (const chunk of stream) {
res.write(`data: ${JSON.stringify({ text: chunk })}\n\n`);
}
res.write('data: [DONE]\n\n');
res.end();
});
app.listen(3000, () => console.log('Server running on port 3000'));
AI Chat with Streaming
import { Vedika } from '@anthropic/vedika';
const vedika = new Vedika({ apiKey: 'YOUR_API_KEY' });
// Non-streaming
const response = await vedika.chat({
question: 'What career paths suit me based on my chart?',
birthDetails: {
datetime: '1990-05-15T14:30:00+05:30',
latitude: 28.6139,
longitude: 77.2090
}
});
console.log(response.answer);
// Streaming (for real-time UI updates)
const stream = await vedika.chatStream({
question: 'Explain my upcoming Saturn dasha in detail',
birthDetails: {
datetime: '1990-05-15T14:30:00+05:30',
latitude: 28.6139,
longitude: 77.2090
}
});
for await (const chunk of stream) {
process.stdout.write(chunk); // Real-time output
}
// React component with streaming
function AstrologyChat() {
const [messages, setMessages] = useState([]);
const [streaming, setStreaming] = useState(false);
async function askQuestion(question: string) {
setStreaming(true);
setMessages(prev => [...prev, { role: 'assistant', content: '' }]);
const stream = await vedika.chatStream({
question,
birthDetails: userBirthDetails
});
let fullResponse = '';
for await (const chunk of stream) {
fullResponse += chunk;
setMessages(prev => {
const updated = [...prev];
updated[updated.length - 1].content = fullResponse;
return updated;
});
}
setStreaming(false);
}
return (
{messages.map((msg, i) => (
{msg.content}
))}
e.key === 'Enter' && askQuestion(e.target.value)}
disabled={streaming}
/>
);
}
Start Building with JavaScript
The Vedika JavaScript SDK works in browsers, Node.js, and edge runtimes. Full TypeScript support with React hooks included.