Skip to content
dhanu docs
Browse pages

API reference

Generate an image

POST /api/pub/v1/images — generate an image from a prompt and get the bytes back as base64.

POST https://api.dhanu.ai/api/pub/v1/images

Generates an image and returns the bytes as base64. Dhanu stores nothing — no task, no document, no version history. Save the bytes yourself.

Request

FieldTypeRequiredNotes
promptstringYesUp to 2000 characters
aspectRatiostringNo1:1, 3:4, 9:16, 4:3, or 16:9
projectIdstringNoRequired when the key is scoped to more than one project
curl -X POST https://api.dhanu.ai/api/pub/v1/images \
  -H "Authorization: Bearer $DHANU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "a red bicycle on a white background",
    "aspectRatio": "1:1"
  }'

Response

201 Created

{
  "image": { "b64": "…", "mimeType": "image/png" },
  "model": "…",
  "usage": { "inputTokens": 9, "outputTokens": 0 },
  "cost": { "amount": 800, "currency": "NPR" }
}

cost.amount is in the currency’s minor unit, so 800 is NPR 8.00 — roughly what a typical image costs.

Saving the bytes

import { writeFileSync } from "node:fs";

const response = await fetch("https://api.dhanu.ai/api/pub/v1/images", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.DHANU_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ prompt: "a red bicycle", aspectRatio: "1:1" }),
});

const { image } = await response.json();
writeFileSync("out.png", Buffer.from(image.b64, "base64"));

Errors

CodeStatusMeaning
invalid_generation_prompt422Prompt is empty or over 2000 characters
invalid_image_aspect_ratio422Aspect ratio is not one of the five supported
project_id_required422Multi-project key called without projectId
insufficient_credits402Workspace credit balance is zero or below

The full list is on Errors.