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/imagesGenerates an image and returns the bytes as base64. Dhanu stores nothing, no task, no document, no version history. Save the bytes yourself.
Request
| Field | Type | Required | Notes |
|---|---|---|---|
prompt | string | Yes | What to draw. Up to 2000 characters. |
aspectRatio | string | No | Defaults to 1:1. One of 1:1, 3:4, 9:16, 4:3, 16:9. |
projectId | string | No | Which 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
| Code | Status | Meaning |
|---|---|---|
invalid_api_key | 401 | The bearer key is missing or unknown. |
api_key_revoked | 401 | The key was revoked. |
api_key_expired | 401 | The key passed its expiry date. |
api_key_ip_not_allowed | 403 | The caller's IP is outside the key's allowlist. |
developer_access_revoked | 403 | The key's creator lost developer access in the workspace. |
api_key_project_scope | 403 | The key covers no active projects, or projectId is outside its scope. |
invalid_request_body | 422 | The body did not match the schema; error.issues names each field. |
project_id_required | 422 | A multi-project key called without projectId. |
insufficient_credits | 402 | The workspace credit balance is zero or below. |
key_spend_limit_reached | 402 | The key reached its monthly spend cap. |
invalid_generation_prompt | 422 | The prompt was empty or longer than 2000 characters. |
invalid_image_aspect_ratio | 422 | The aspect ratio is outside the five supported values. |
The full list is on Errors.