Skip to content
dhanu docs
Browse pages

API reference

Generate text

POST /api/pub/v1/text, generate text from a prompt, with an optional system instruction and token cap.

POSThttps://api.dhanu.ai/api/pub/v1/text

Generates text from a prompt. Nothing is stored, no task, no document, no version history. You get the text back and a metering record is written against the acting project.

Request

FieldTypeRequiredNotes
promptstringYesUp to 2000 characters.
systemstringNoA system instruction.
maxTokensintegerNoAt most 9007199254740991.
projectIdstringNoWhich of the key's projects the call runs against. Required when the key can access more than one project.
streambooleanNoStream the reply as server-sent events (delta events, then one done event) instead of one JSON body.
curl -X POST https://api.dhanu.ai/api/pub/v1/text \
  -H "Authorization: Bearer $DHANU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Write a haiku about Kathmandu",
    "system": "You are a concise poet.",
    "maxTokens": 200
  }'
import { Dhanu } from "dhanu";

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

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

console.log(reply.text);
import os
from dhanu import Dhanu

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

reply = dhanu.text.generate(
    prompt="Write a haiku about Kathmandu",
    system="You are a concise poet.",
    max_tokens=200,
)

print(reply.text)

Response

201 Created

{
  "text": "…",
  "model": "…",
  "usage": { "inputTokens": 12, "outputTokens": 41 },
  "cost": { "amount": 28, "currency": "NPR" }
}

cost.amount is in the currency’s minor unit, so 28 is NPR 0.28. A reply of roughly 500 tokens in and 500 out costs on the order of 28 paisa.

Streaming

Send "stream": true (or Accept: text/event-stream) to receive the reply as server-sent events: delta events carrying { "text": "…" } as tokens arrive, then one done event with the same body a non-streaming call returns. The SDKs expose it as an iterator:

for await (const event of dhanu.text.stream({ prompt: "Write a haiku about Kathmandu" })) {
  if (event.type === "delta") process.stdout.write(event.text);
  else console.log("\ncost", event.result.cost);
}
for event in dhanu.text.stream(prompt="Write a haiku about Kathmandu"):
    print(getattr(event, "text", ""), end="")

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.

The full list is on Errors.