Skip to main content
All Integrations
TypeScript / Node.js Guide

TypeScript & JavaScript
Integration Guide

Learn how to query the BaZi API calculation engine from Node.js, Next.js, or browser environments. Use either our typed SDK (Beta) or native fetch().

Production Ready via REST: While the official @baziapi/sdk npm package is in active developer preview (Beta), you can integrate immediately using standard fetch or axios in production.

1. Recommended: Native Fetch (Zero Dependencies)

Works out of the box in Node 18+, Next.js App Router, Cloudflare Workers, and Deno with zero external npm dependencies:

// Native Node.js / Next.js fetch example
async function getBaziChart() {
  const response = await fetch("https://api.baziapi.pro/api/v2/bazi/calculate", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-api-key": process.env.BAZI_API_KEY!,
    },
    body: JSON.stringify({
      birthDate: "1994-08-23",
      birthTime: "09:30",
      gender: "male",
      timezone: "Asia/Hong_Kong",
      language: "en",
    }),
  });

  if (!response.ok) {
    throw new Error(`BaZi API Error: ${response.statusText}`);
  }

  const { data } = await response.json();
  console.log("Day Master:", data.dayMaster);
  console.log("Four Pillars:", data.pillars);
}

2. Official TypeScript SDK

Developer Beta

Provides end-to-end type safety, automatic retry management, and error code enumerations:

$ npm install @baziapi/sdk # Preview release
import { BaziClient } from "@baziapi/sdk"; // Beta package

const client = new BaziClient({
  apiKey: process.env.BAZI_API_KEY,
});

const chart = await client.calculate({
  birthDate: "1994-08-23",
  birthTime: "09:30",
  gender: "male",
  timezone: "Asia/Hong_Kong",
});

console.log(chart.fourPillars.yearPillar); // Typed autocomplete

Need your API Key?

Sign up for free and generate an API key in under 60 seconds.