Skip to content
dhanu docs
Browse pages

SDKs

Node SDK

The official dhanu npm package — install it, generate images and text, and handle errors with DhanuError.

The dhanu package wraps the public API for Node. It has no dependencies and uses the global fetch, so it needs Node 18 or newer.

Install

npm i dhanu

Set up the client

import { Dhanu } from "dhanu";

// Server-side only — never ship your secret key to a browser.
const dhanu = new Dhanu(process.env.DHANU_API_KEY!);

The key is always passed explicitly. There is no baseUrl option: the base URL comes from DHANU_BASE_URL if set, and otherwise defaults to https://api.dhanu.ai. Point DHANU_BASE_URL at http://localhost:3000 for local development.

Generate an image

import { writeFileSync } from "node:fs";

const image = await dhanu.images.generate({
  prompt: "a red bicycle on a white background",
  aspectRatio: "1:1", // "1:1" | "3:4" | "9:16" | "4:3" | "16:9"
});

writeFileSync("out.png", Buffer.from(image.b64, "base64"));

Returns { b64, mimeType, model, usage, cost }. Nothing is stored server-side — see Generate an image.

Generate text

const reply = await dhanu.text.generate({
  prompt: "Write a haiku about Kathmandu",
  system: "You are a concise poet.",
  maxTokens: 200,
});

console.log(reply.text);

Returns { text, model, usage, cost }.

Verify a key

const me = await dhanu.me();

Handle errors

Every failure throws DhanuError with a machine-readable code, the HTTP status, a message, and the requestId to quote in support reports.

import { DhanuError } from "dhanu";

try {
  await dhanu.images.generate({ prompt: "…" });
} catch (err) {
  if (err instanceof DhanuError) {
    if (err.code === "rate_limited") {
      // back off and retry — see /developer/reference/rate-limits
    }
    console.error(err.code, err.status, err.message, err.requestId);
  }
}

The complete list of codes is on Errors.