Read your DA totals and track micro-DA balance via the credits API.
OpenDunes accounts are prepaid. You deposit Algerian Dinars (DA) with a CIB or Edahabia card, and the balance is deducted per request based on token usage.
Balances are stored in micro-DA: 1 DA = 1,000,000 micro-DA (bigint — no floats). The credits API returns DA values derived from those integers.
const res = await fetch("https://opendunes.com/v1/credits", {
headers: { Authorization: `Bearer ${process.env.OPENDUNES_API_KEY}` },
});
const { data } = await res.json();
console.log(`Into wallet: ${data.total_credits} DA`);
console.log(`Spent: ${data.total_usage} DA`);
console.log(`Remaining: ${data.total_credits - data.total_usage} DA`);GET /v1/credits returns:
| Field | Type | Description |
|---|---|---|
data.total_credits | number | Total DA ever put into the wallet — total_credits − total_usage is your balance |
data.total_usage | number | Total DA ever spent on requests |
Every successful inference response also carries:
X-Balance-Available: 4820000That is the remaining balance in micro-DA after the current request was deducted. Reading this header is the fastest way to keep a live balance display without a separate API call.
// .withResponse() exposes the raw HTTP response alongside the parsed body
const { data, response } = await client.chat.completions
.create({ ... })
.withResponse();
const remaining = Number(response.headers.get("x-balance-available") ?? 0);
const da = remaining / 1_000_000;When your balance is at zero or below, the API returns HTTP 402 before any model is called:
{
"error": {
"code": 402,
"message": "Insufficient balance for this request.",
"metadata": {
"limit_source": "wallet",
"error_type": "payment_required"
}
}
}limit_source is key when the key's spending cap stopped it instead, and guardrail for a guardrail's budget — see Errors.
Catch this and redirect your user to deposit at /dashboard/billing.
Each request costs (input_tokens × input_rate) + (output_tokens × output_rate), rounded up to the nearest micro-DA (minimum 1). Rates are shown on each model's page in DA per million tokens