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.

POSThttps://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
promptstringYesWhat to draw. Up to 2000 characters.
aspectRatiostringNoDefaults to 1:1. One of 1:1, 3:4, 9:16, 4:3, 16:9.
projectIdstringNoWhich of the key's projects the call runs against. Required when the key can access 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"
  }'
import { Dhanu } from "dhanu";

const dhanu = new Dhanu(process.env.DHANU_API_KEY!);

const image = await dhanu.images.generate({
  prompt: "a red bicycle on a white background",
  aspectRatio: "1:1",
});
import os
from dhanu import Dhanu

dhanu = Dhanu(os.environ["DHANU_API_KEY"])

image = dhanu.images.generate(
    prompt="a red bicycle on a white background",
    aspect_ratio="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"));
import base64
import json
import os
import urllib.request

request = urllib.request.Request(
    "https://api.dhanu.ai/api/pub/v1/images",
    data=json.dumps({"prompt": "a red bicycle", "aspectRatio": "1:1"}).encode(),
    headers={
        "Authorization": f"Bearer {os.environ['DHANU_API_KEY']}",
        "Content-Type": "application/json",
    },
)

with urllib.request.urlopen(request) as response:
    payload = json.load(response)

with open("out.png", "wb") as handle:
    handle.write(base64.b64decode(payload["image"]["b64"]))

Errors

CodeStatusMeaning
invalid_api_key401The bearer key is missing or unknown.
api_key_revoked401The key was revoked.
api_key_expired401The key passed its expiry date.
api_key_ip_not_allowed403The caller's IP is outside the key's allowlist.
developer_access_revoked403The key's creator lost developer access in the workspace.
api_key_project_scope403The key covers no active projects, or projectId is outside its scope.
invalid_request_body422The body did not match the schema; error.issues names each field.
project_id_required422A multi-project key called without projectId.
insufficient_credits402The workspace credit balance is zero or below.
key_spend_limit_reached402The key reached its monthly spend cap.
invalid_generation_prompt422The prompt was empty or longer than 2000 characters.
invalid_image_aspect_ratio422The aspect ratio is outside the five supported values.

The full list is on Errors.