API reference
Generate text
POST /api/pub/v1/text, generate text from a prompt, with an optional system instruction and token cap.
POST
https://api.dhanu.ai/api/pub/v1/textGenerates 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
| Field | Type | Required | Notes |
|---|---|---|---|
prompt | string | Yes | Up to 2000 characters. |
system | string | No | A system instruction. |
maxTokens | integer | No | At most 9007199254740991. |
projectId | string | No | Which of the key's projects the call runs against. Required when the key can access more than one project. |
stream | boolean | No | Stream 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
| 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. |
The full list is on Errors.