Besides the Buy credits tab, you can buy AI credits through the API, paid with the account credit balance in MT. The price is the same as in Meu Interweb. It is useful, for example, for a program of yours to top up credits when the balance runs low.
Before you start
| 1 |
A prod_ key with billing:purchase and ai:write. billing:purchase is a dangerous permission, off by default: only give it to the key that buys. See creating a key.
|
|
| 2 |
Credit on the account, in MT. This purchase can only be paid with that balance.
|
|
| 3 |
A complete account profile. Otherwise the API answers 409 with profile_incomplete.
|
|
1. See the prices
curl "https://api.interweb.co.mz/v1/ai/credits/prices" -H "Authorization: Bearer $INTERWEB_API_KEY"
| Field |
What it is |
| currency |
The account currency. |
| minimum and maximum |
How many credits you can buy at once. The minimum is 1,000. |
| tiers |
The volume tiers: from how many credits (from) each price per credit (unit_price) applies. |
| tax_added |
Whether tax is added on the invoice. |
2. Buy
Send quantity to POST /v1/ai/credits/purchase, with the Idempotency-Key header, which is required. Replace the example value with your own: a UUID works well. On Linux and macOS:
curl -X POST "https://api.interweb.co.mz/v1/ai/credits/purchase" \
-H "Authorization: Bearer $INTERWEB_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 6f1d2c3a-8b4e-4f7a-9c21-1d2e3f4a5b6c" \
-d '{"quantity": 1000}'
The response is 202 and includes, among others, status, credits, unit_price, invoice_id, amount (the total with tax), subtotal and tax. The credits go into the balance with the payment. If the invoice does not match the price, the purchase is undone and nothing is charged.
Errors for this purchase
| Response |
When |
What to do |
| 402 insufficient_credit |
The account balance in MT is not enough. details has credit_missing and add_funds_url. |
Add funds and repeat with the same Idempotency-Key. |
| 403 spending_limit_exceeded |
The purchase goes over the key spending limit. |
Use a key with a higher limit or wait for resets_at. |
| 403 insufficient_scope |
The key lacks billing:purchase or ai:write. |
Create a key with both. |
| 409 profile_incomplete |
The account profile is incomplete. |
Complete your profile in Meu Interweb. |
| 409 idempotency_conflict |
The same Idempotency-Key with a different body, or the first purchase is still running. |
Wait a little, or use a new value if it is a different purchase. |
| 429 rate_limited |
More than 5 purchases per minute. |
Wait the seconds in Retry-After. |
|
Test first with a sandbox_ key. In the Sandbox nothing is created or charged: amount comes without tax and tax is null. Then switch to the prod_ key.
|
|
Use a new Idempotency-Key for each purchase, and the same one when you repeat the same purchase after a network failure: that way you never buy twice. Credits do not take a promo_code.
|
RECOMMENDED PRODUCT Web hosting with cPanel Domain and SSL included, daily backups and the panel you already know. from 321,75 MT/mo (3-year plan, with coupon) See plans |