Monitor Invoices and Credit Balance with the Melbicom API

GUIDE

A twenty-line script and a cron entry that watch your account: it checks for unpaid invoices and a low credit balance every morning and alerts your team before billing becomes an outage.

An unpaid invoice can sit unnoticed for days, and a credit balance can run out under an active service. Both states are visible in the client area, which means both are visible to a script. This guide builds a small watcher on the Melbicom API’s two billing endpoints: it runs every morning and reports only when something needs your attention.

Prerequisites

  • A Melbicom API token, from Create Your Melbicom API Token.
  • A machine that runs on a schedule: any server or workstation with cron, curl and jq.
  • A working way to send alerts from that machine: the example uses the mail command; any chat webhook does the same job.

Why Watch Billing by API

  • Unpaid invoices are reported at the next morning run: independent of email reminders and shared inboxes.
  • Low credit is detected early: you top up on your own schedule instead of after an interruption.
  • No routine checking: the script is silent while everything is fine.

Step 1: The Two Endpoints

Everything the watcher needs comes from two calls. GET /billing/invoices lists your invoices with their number, dates, totals and status, and accepts a status filter (Paid, Unpaid, Cancelled, Refunded) so the watcher asks only for what matters; GET /billing/creditbalance returns the account credit as a decimal string with its currency:

curl -s -H "Authorization: Bearer YOUR_TOKEN" https://api01.melbicom.net/api/billing/creditbalance

The response is minimal: {"balance":"100.00","currency":"EUR"}.

Step 2: The Watcher Script

Save the script below, make it executable, and set the balance floor and alert address at the top. Size the floor to cover your usual spend across the time it takes you to notice the alert and top up. The token itself never appears in the file: the script expects MELBICOM_API_TOKEN to already be in its environment, set where the script is scheduled, and stops with a clear error when it is missing. This is the secure pattern for a scheduled credential, since the file itself can sit in a repository or be shared without exposing anything:

#!/usr/bin/env bash
# Alerts when an invoice is unpaid or the credit balance drops below the floor.
API="https://api01.melbicom.net/api"
TOKEN="${MELBICOM_API_TOKEN:?set MELBICOM_API_TOKEN in the environment}"
MIN_BALANCE="25.00"
ALERT_TO="you@example.com"

unpaid=$(curl -sf -H "Authorization: Bearer $TOKEN" "$API/billing/invoices?status=Unpaid" \
  | jq -r 'length')
balance=$(curl -sf -H "Authorization: Bearer $TOKEN" "$API/billing/creditbalance" \
  | jq -r '.balance')

msg=""
[ "$unpaid" -gt 0 ] && msg="$unpaid unpaid invoice(s). "
awk "BEGIN { exit !($balance < $MIN_BALANCE) }" && msg="${msg}Credit balance $balance is below $MIN_BALANCE."
[ -n "$msg" ] && echo "$msg" | mail -s "Melbicom billing alert" "$ALERT_TO"

Make the script executable:

chmod +x /usr/local/bin/melbicom-billing-watch.sh

The alert line is the part to make your own: swap mail for a chat webhook, a pager, or any channel your team monitors.

Step 3: Schedule It

Edit your schedule with crontab -e and add both lines below: the environment line supplies the token, and the entry runs the watcher every morning at 09:00. The crontab is readable only by its own user, so the credential stays out of the script and out of version control:

MELBICOM_API_TOKEN=YOUR_TOKEN
0 9 * * * /usr/local/bin/melbicom-billing-watch.sh

Finally, restrict the token to this machine’s IP so the credential in the crontab cannot be used from any other address: on the API Tokens page, click Edit next to the IP restriction status, add the machine’s address, and save. The full token management surface is in Secure and Manage Your Melbicom API Token.

What You’ve Built

A billing watch that costs two API calls a day and removes a class of billing surprises: unpaid invoices and a falling balance are now reported to you instead of waiting to be noticed. The pattern extends naturally, from posting the daily balance to a dashboard to reconciling invoice totals; every field the script reads is in Melbicom API: Authentication and Endpoints. For CDN packages on allowances, Prepaid Pockets auto-refill automates the response to a low balance the same way this watcher automates the alert.

Can’t find what you need? Our engineers are available around the clock, from quick fixes to full infrastructure design.