Node.js Integration
Express, Fastify, and serverless
Express.js Setup
// server.js
import express from 'express';
import { VedikaClient } from '@vedika-io/sdk';
const app = express();
app.use(express.json());
const client = new VedikaClient({ apiKey: process.env.VEDIKA_API_KEY });
app.post('/api/chart', async (req, res) => {
try {
// req.body: { datetime, latitude, longitude, timezone }
const chart = await client.post('/v2/astrology/birth-chart', req.body);
res.json({
sunSign: chart.sunSign,
moonSign: chart.moonSign,
ascendant: chart.ascendant.sign,
planets: chart.planets
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/api/match', async (req, res) => {
const { bride, groom } = req.body;
// Guna Milan takes the male chart first, the female chart second
const match = await client.getGunaMilan(groom, bride);
res.json({
score: match.total_points,
verdict: match.match_result
});
});
app.listen(3000, () => console.log('Server running on port 3000'));
Fastify Setup
// server.js
import Fastify from 'fastify';
import { VedikaClient } from '@vedika-io/sdk';
const fastify = Fastify({ logger: true });
const client = new VedikaClient({ apiKey: process.env.VEDIKA_API_KEY });
fastify.post('/api/chart', {
schema: {
body: {
type: 'object',
required: ['datetime', 'latitude', 'longitude', 'timezone'],
properties: {
datetime: { type: 'string' },
latitude: { type: 'number' },
longitude: { type: 'number' },
timezone: { type: 'string' }
}
}
}
}, async (request, reply) => {
const chart = await client.post('/v2/astrology/birth-chart', request.body);
return { sunSign: chart.sunSign, moonSign: chart.moonSign };
});
fastify.listen({ port: 3000 });
Streaming Response
// Express SSE streaming
app.post('/api/chat/stream', async (req, res) => {
const { question, birthDetails } = req.body;
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
try {
// askQuestionStream yields the payload of each SSE "data:" line (a JSON string)
for await (const payload of client.askQuestionStream({ question, birthDetails })) {
res.write(`data: ${payload}\n\n`);
}
res.end();
} catch (error) {
res.write(`data: ${JSON.stringify({ type: 'error', error: error.message })}\n\n`);
res.end();
}
});
AWS Lambda
// handler.js
import { VedikaClient } from '@vedika-io/sdk';
const client = new VedikaClient({ apiKey: process.env.VEDIKA_API_KEY });
export const handler = async (event) => {
const body = JSON.parse(event.body);
try {
const chart = await client.post('/v2/astrology/birth-chart', body);
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sunSign: chart.sunSign,
moonSign: chart.moonSign,
ascendant: chart.ascendant.sign
})
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message })
};
}
};
Vercel Edge Functions
// api/chart.js
import { VedikaClient } from '@vedika-io/sdk';
const client = new VedikaClient({ apiKey: process.env.VEDIKA_API_KEY });
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const chart = await client.post('/v2/astrology/birth-chart', req.body);
res.json(chart);
}
export const config = {
runtime: 'edge',
};
Rate Limiting Middleware
import rateLimit from 'express-rate-limit';
const apiLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 20, // 20 requests per minute
message: { error: 'Too many requests, please try again later' }
});
app.use('/api/', apiLimiter);
Redis Caching
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);
async function getCachedOrFetch(key, ttl, fetchFn) {
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const data = await fetchFn();
await redis.setex(key, ttl, JSON.stringify(data));
return data;
}
app.get('/api/panchang', async (req, res) => {
const { date, lat, lng } = req.query;
const key = `panchang:${date}:${lat}:${lng}`;
const panchang = await getCachedOrFetch(key, 3600, () =>
client.get('/v2/astrology/panchang', {
date,
latitude: parseFloat(lat),
longitude: parseFloat(lng),
timezone: '+05:30'
})
);
res.json(panchang);
});