# Get warned when credits run low, and top up automatically

_Yes: GET /v1/balance returns low_balance, a WhatsApp alert goes to the account contact when the balance drops to the threshold (at most once per 12 h), and automatic card top-up is configured in the console, not through the API._

Keywords: low credits, low balance, balance alert, automatic top-up, auto-recharge, top up, out of credits, depleted balance, 402, buy credits, saved card, use_saved_card, alert threshold, low_balance, X-Credits-Remaining, credit packs

## Scenario

Your application sends reminders all week. One day the balance hits zero and template sends are refused with `402`. **Yes, you can be warned beforehand, and you can have it topped up automatically.** But you need to know exactly what is sent, and what is not.

## Know where the balance stands

```bash
# curl
curl -X GET "https://wa-api.geskap.com/v1/balance" \
  -H "Authorization: Bearer $GESKAP_API_KEY"
```

```js
// Node.js
const res = await fetch("https://wa-api.geskap.com/v1/balance", {
  method: "GET",
  headers: {
    Authorization: `Bearer ${process.env.GESKAP_API_KEY}`,
  },
});
console.log(res.status, await res.json());
```

```php
// PHP
$ch = curl_init("https://wa-api.geskap.com/v1/balance");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("GESKAP_API_KEY"),
  ],
]);
$res = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), " ", $res;
```

```python
# Python
import os, requests

res = requests.request(
    "GET",
    "https://wa-api.geskap.com/v1/balance",
    headers={"Authorization": f"Bearer {os.environ['GESKAP_API_KEY']}"},
    timeout=15,
)
print(res.status_code, res.json())
```

The response gives `credit_balance`, `low_balance_threshold` and `low_balance` (true when the balance is lower than or equal to the threshold). Every `POST /v1/messages` also returns the `X-Credits-Remaining` header. Watch it in your code: it is the fastest signal.

## The automatic alert

When a template send leaves a balance lower than or equal to the threshold, a WhatsApp alert message is sent, from your own number, to the **account's contact number**. It goes out at most **once every 12 hours**.

- The alert threshold (`low_balance_threshold`) is an account setting, changed by the administrator: you do not change it yourself.
- The alert is evaluated **after a template send** (`POST /v1/messages`), not after a free-form reply.
- It only goes out if the account has a contact number and a connected WhatsApp number. Otherwise, nothing.
- The timestamp is set before sending: if WhatsApp refuses the alert, the next one is not retried before 12 h.
- It is **not a webhook event**: your server receives nothing. Your webhook events are `message.status`, `message.inbound` and `message.echo`.
- The code also contains low-balance and depleted-balance e-mails, but whether they are sent depends on a platform setting: do not build your monitoring on them.

## Top up by hand, through the API

```bash
# curl
curl -X GET "https://wa-api.geskap.com/v1/credits/packs" \
  -H "Authorization: Bearer $GESKAP_API_KEY"
```

```js
// Node.js
const res = await fetch("https://wa-api.geskap.com/v1/credits/packs", {
  method: "GET",
  headers: {
    Authorization: `Bearer ${process.env.GESKAP_API_KEY}`,
  },
});
console.log(res.status, await res.json());
```

```php
// PHP
$ch = curl_init("https://wa-api.geskap.com/v1/credits/packs");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("GESKAP_API_KEY"),
  ],
]);
$res = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), " ", $res;
```

```python
# Python
import os, requests

res = requests.request(
    "GET",
    "https://wa-api.geskap.com/v1/credits/packs",
    headers={"Authorization": f"Bearer {os.environ['GESKAP_API_KEY']}"},
    timeout=15,
)
print(res.status_code, res.json())
```

Pick a pack (`pack_code`) then pay by card. With `use_saved_card` and a card already on file, the card is charged without a redirect and the credits arrive immediately. Otherwise the response contains `checkout_url`: open it to pay. The first payment saves the card.

```bash
# curl
curl -X POST "https://wa-api.geskap.com/v1/credits/checkout" \
  -H "Authorization: Bearer $GESKAP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "pack_code": "standard", "use_saved_card": true }'
```

```js
// Node.js
const res = await fetch("https://wa-api.geskap.com/v1/credits/checkout", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.GESKAP_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ "pack_code": "standard", "use_saved_card": true }),
});
console.log(res.status, await res.json());
```

```php
// PHP
$ch = curl_init("https://wa-api.geskap.com/v1/credits/checkout");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("GESKAP_API_KEY"),
    "Content-Type: application/json",
  ],
  CURLOPT_POSTFIELDS => json_encode(["pack_code" => "standard", "use_saved_card" => true]),
]);
$res = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), " ", $res;
```

```python
# Python
import os, requests

res = requests.request(
    "POST",
    "https://wa-api.geskap.com/v1/credits/checkout",
    headers={"Authorization": f"Bearer {os.environ['GESKAP_API_KEY']}"},
    json={"pack_code": "standard", "use_saved_card": True},
    timeout=15,
)
print(res.status_code, res.json())
```

## Automatic top-up

It is configured in the console (`PUT /v1/console/autorecharge`, with your console session): enable, threshold, and the pack to buy. There is no route to set it with an API key.

| Rule | Value |
|---|---|
| Condition | a card is on file, and the balance is **strictly lower** than the threshold |
| Default threshold | 50 credits (adjustable by you) |
| Minimum delay between two top-ups | 1 hour |
| Cap | 3 top-ups per calendar month |
| When it triggers | after a `POST /v1/messages` send, and in a daily sweep at 04:00 UTC |
| If the card is declined or asks for authentication | automatic top-up is **disabled** |

When it disables itself, a "Recharge automatique désactivée" line appears in `GET /v1/logs?category=rejects` with the reason. Top up manually once, then re-enable it in the console.

> **Pitfall:** two different thresholds exist. `low_balance_threshold` triggers the alert and the `low_balance` flag (balance lower than **or equal to**). The auto top-up threshold is yours, and the top-up only goes out if the balance is **strictly lower**. Set the top-up threshold above what your traffic consumes between two sweeps.

> **Pitfall:** at a zero balance, a template answers `402` "Solde de crédits insuffisant" and nothing is sent. A free-form reply within 24 h still goes out (its charge never blocks). See [Cost of a message](https://console.geskap.com/docs/cout-d-un-message?lang=en).

> **Pitfall:** the top-up is attempted right after the send that dropped the balance under the threshold. If it fails, the send stays successful and nothing is flagged in the response; the 04:00 UTC sweep will try again. Keep a margin of credits rather than counting on it at the last moment.

_Proof — verified in the API source: public_api.py, api_alerts.py, api_send.py, autorecharge.py, broadcast_scheduler.py, console_api.py._

---

# Être prévenu quand les crédits sont bas, et recharger automatiquement

_Oui : GET /v1/balance donne low_balance, une alerte WhatsApp part au contact du compte quand le solde passe sous le seuil (au plus une fois par 12 h), et la recharge automatique par carte se configure dans la console, pas par l'API._

Mots-clés: crédits bas, solde bas, alerte solde, recharge automatique, auto-recharge, recharger, plus de crédits, solde épuisé, 402, acheter des crédits, carte enregistrée, use_saved_card, seuil d'alerte, low_balance, X-Credits-Remaining, packs de crédits

## Scénario

Votre application envoie des rappels toute la semaine. Un jour le solde tombe à zéro et les envois de templates sont refusés en `402`. **Oui, vous pouvez être prévenu avant, et vous pouvez faire recharger automatiquement.** Mais il faut savoir exactement ce qui part, et ce qui ne part pas.

## Savoir où en est le solde

```bash
# curl
curl -X GET "https://wa-api.geskap.com/v1/balance" \
  -H "Authorization: Bearer $GESKAP_API_KEY"
```

```js
// Node.js
const res = await fetch("https://wa-api.geskap.com/v1/balance", {
  method: "GET",
  headers: {
    Authorization: `Bearer ${process.env.GESKAP_API_KEY}`,
  },
});
console.log(res.status, await res.json());
```

```php
// PHP
$ch = curl_init("https://wa-api.geskap.com/v1/balance");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("GESKAP_API_KEY"),
  ],
]);
$res = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), " ", $res;
```

```python
# Python
import os, requests

res = requests.request(
    "GET",
    "https://wa-api.geskap.com/v1/balance",
    headers={"Authorization": f"Bearer {os.environ['GESKAP_API_KEY']}"},
    timeout=15,
)
print(res.status_code, res.json())
```

La réponse donne `credit_balance`, `low_balance_threshold` et `low_balance` (vrai quand le solde est inférieur ou égal au seuil). Chaque `POST /v1/messages` renvoie aussi l'en-tête `X-Credits-Remaining`. Surveillez-le dans votre code : c'est le signal le plus rapide.

## L'alerte automatique

Quand un envoi de template laisse un solde inférieur ou égal au seuil, un message WhatsApp d'alerte est envoyé, depuis votre propre numéro, au **numéro de contact du compte**. Il part au plus **une fois toutes les 12 heures**.

- Le seuil d'alerte (`low_balance_threshold`) est un réglage du compte, modifié par l'administrateur : vous ne le changez pas vous-même.
- L'alerte est évaluée **après un envoi de template** (`POST /v1/messages`), pas après une réponse libre.
- Elle ne part que si le compte a un numéro de contact et un numéro WhatsApp connecté. Sinon, rien.
- L'horodatage est posé avant l'envoi : si WhatsApp refuse l'alerte, la suivante n'est pas retentée avant 12 h.
- Ce n'est **pas un événement de webhook** : votre serveur ne reçoit rien. Vos événements webhook sont `message.status`, `message.inbound` et `message.echo`.
- Le code contient aussi des e-mails de rappel de solde bas ou épuisé, mais leur envoi dépend d'un réglage de la plateforme : ne bâtissez pas votre surveillance dessus.

## Recharger à la main, par l'API

```bash
# curl
curl -X GET "https://wa-api.geskap.com/v1/credits/packs" \
  -H "Authorization: Bearer $GESKAP_API_KEY"
```

```js
// Node.js
const res = await fetch("https://wa-api.geskap.com/v1/credits/packs", {
  method: "GET",
  headers: {
    Authorization: `Bearer ${process.env.GESKAP_API_KEY}`,
  },
});
console.log(res.status, await res.json());
```

```php
// PHP
$ch = curl_init("https://wa-api.geskap.com/v1/credits/packs");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("GESKAP_API_KEY"),
  ],
]);
$res = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), " ", $res;
```

```python
# Python
import os, requests

res = requests.request(
    "GET",
    "https://wa-api.geskap.com/v1/credits/packs",
    headers={"Authorization": f"Bearer {os.environ['GESKAP_API_KEY']}"},
    timeout=15,
)
print(res.status_code, res.json())
```

Choisissez un pack (`pack_code`) puis payez par carte. Avec `use_saved_card` et une carte déjà enregistrée, la carte est débitée sans redirection et les crédits arrivent tout de suite. Sinon, la réponse contient `checkout_url` : ouvrez-la pour payer. Le premier paiement enregistre la carte.

```bash
# curl
curl -X POST "https://wa-api.geskap.com/v1/credits/checkout" \
  -H "Authorization: Bearer $GESKAP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "pack_code": "standard", "use_saved_card": true }'
```

```js
// Node.js
const res = await fetch("https://wa-api.geskap.com/v1/credits/checkout", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.GESKAP_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ "pack_code": "standard", "use_saved_card": true }),
});
console.log(res.status, await res.json());
```

```php
// PHP
$ch = curl_init("https://wa-api.geskap.com/v1/credits/checkout");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("GESKAP_API_KEY"),
    "Content-Type: application/json",
  ],
  CURLOPT_POSTFIELDS => json_encode(["pack_code" => "standard", "use_saved_card" => true]),
]);
$res = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), " ", $res;
```

```python
# Python
import os, requests

res = requests.request(
    "POST",
    "https://wa-api.geskap.com/v1/credits/checkout",
    headers={"Authorization": f"Bearer {os.environ['GESKAP_API_KEY']}"},
    json={"pack_code": "standard", "use_saved_card": True},
    timeout=15,
)
print(res.status_code, res.json())
```

## La recharge automatique

Elle se configure dans la console (`PUT /v1/console/autorecharge`, avec votre session de console) : activer, seuil, et pack à acheter. Il n'existe pas de route pour la régler avec une clé API.

| Règle | Valeur |
|---|---|
| Condition | une carte est enregistrée, et le solde est **strictement inférieur** au seuil |
| Seuil par défaut | 50 crédits (réglable par vous) |
| Délai minimal entre deux recharges | 1 heure |
| Plafond | 3 recharges par mois calendaire |
| Quand elle se déclenche | après un envoi `POST /v1/messages`, et lors d'un balayage quotidien à 04:00 UTC |
| Si la carte est refusée ou demande une authentification | la recharge automatique est **désactivée** |

Quand elle se désactive, une ligne « Recharge automatique désactivée » apparaît dans `GET /v1/logs?category=rejects` avec la raison. Rechargez une fois manuellement, puis réactivez-la dans la console.

> **Piège :** deux seuils différents existent. `low_balance_threshold` déclenche l'alerte et le drapeau `low_balance` (solde inférieur **ou égal**). Le seuil de la recharge automatique est le vôtre, et la recharge ne part que si le solde est **strictement inférieur**. Réglez le seuil de recharge au-dessus de ce que votre trafic consomme entre deux balayages.

> **Piège :** à solde nul, un template répond `402` « Solde de crédits insuffisant » et rien ne part. Une réponse libre dans les 24 h, elle, part quand même (son débit ne bloque jamais). Voir [Coût d'un message](https://console.geskap.com/docs/cout-d-un-message).

> **Piège :** la recharge est tentée juste après l'envoi qui a fait passer le solde sous le seuil. Si elle échoue, l'envoi reste réussi et rien ne vous est signalé dans la réponse ; le balayage de 04:00 UTC retentera. Gardez une marge de crédits plutôt que de compter sur elle au dernier moment.

_Preuve — vérifié dans le code de l'API : public_api.py, api_alerts.py, api_send.py, autorecharge.py, broadcast_scheduler.py, console_api.py._

Support: WhatsApp +237 6 56 09 88 31 · All recipes: https://wa-api.geskap.com/llms.txt
