Management systems and websites: paying invoices from credit and sending SMS through the API

API & DocumentationWhat You Can Automate·· 3 views· 7 min read

The short answer: your billing system, your management software or your website can talk to our API: see the invoices you have with us, pay them from your account credit and send SMS to your own customers, such as payment confirmations and reminders. This guide brings together the two areas that do this work, Invoices and SMS, and shows how to build the integration safely.

The integration runs on your server, never in the browser

The API only accepts the key in the Authorization header, over HTTPS, and browsers can only call the API from the documentation: your site's pages cannot use the key directly. The path is always the same: the page calls your server, and your server calls the API.

1 Keep the key in an environment variable or a secrets vault, never in the code or in a repository.
2 Fill in the key's IP list with your server's IP: a request from any other address answers 403 ip_not_allowed.
3 Use one key per program, with only the permissions it needs. If one leaks, the others keep working.

The permissions for each part

To Permission Note
See invoices, the PDF, your credit and transactions billing:read
Pay invoices from credit billing:pay Sensitive, off by default. Each invoice total counts towards the key's spending limit.
Send, schedule and cancel SMS, and record who does not want them sms:write Needs an active SMS reseller account. Without one, the API answers 403 sms_reseller_required.
See message status, your SMS balance and the history sms:read

SMS use credits from your SMS balance, not your account credit, and the key's spending limit does not apply to them. The SMS reseller account is switched on in Meu Interweb, and the product is on the SMS page.

Paying your invoices from credit, step by step

1 See your credit. GET /v1/billing/credit returns balance and currency.
2 List unpaid invoices with GET /v1/invoices?status=unpaid. The list comes in pages, and balance_due is what is left to pay on each one.
3 Find out what each invoice is for. GET /v1/invoices/{id} returns the lines, with type, description and related_id, the service or domain the line refers to. That is how your system knows which of your customers that renewal belongs to.
4 Pay with POST /v1/invoices/{id}/pay, always with an Idempotency-Key built from the invoice number: repeating with the same value does not charge again.
5 Check the answer: status, amount_charged and credit_balance, the credit left. If the invoice had already been paid in the meantime, amount_charged is 0.
6 Keep the document. GET /v1/invoices/{id}/pdf returns the PDF in content, in base64. If pdf_available is false, url takes you to the invoice in Meu Interweb.

curl https://api.interweb.ao/v1/invoices/1042/pay -X POST \
-H "Authorization: Bearer $INVOICE_KEY" \
-H "Idempotency-Key: pay-invoice-1042"

Answer What it means What your system does
200 with status paid The invoice is paid. Records amount_charged and moves to the next one.
402 insufficient_credit There is not enough credit. Nothing was charged. Alerts whoever tops up the credit and tries later.
403 spending_limit_exceeded The payment would go over the key's spending limit. Nothing was charged. Reviews the limit: create a key with a higher one, if that makes sense.
409 conflict In details.reason: invoice_not_unpaid (no longer unpaid), nothing_to_pay, credit_not_allowed (this invoice cannot be paid from credit) or profile_incomplete (the account profile is not complete). Does not repeat. Handles the case in Meu Interweb.
429 rate_limited Paying invoices and buying domains and extras share 5 requests a minute, per account. Waits for Retry-After and repeats with the same Idempotency-Key.
The API only charges your credit. To pay any other way, use Meu Interweb or the url of each invoice, which shows the payment methods for your account. How credit is topped up and used is explained in credit balance in your account.

Sending SMS to your customers

1 Send with POST /v1/sms/messages, with number and text. Write the number with or without the country code, because the system can tell the country from the first digit: 923456789 goes to Angola and 841234567 to Mozambique. So +244 923 456 789 and 244923456789 are the same number as 923456789.
2 The sender is optional. Without sender, the message goes out with the free sender, NETMSG. For another name, use an approved sender from GET /v1/sms/senders.
3 Always send an Idempotency-Key built from the event (for example the receipt number): a retry never sends twice.
4 Read the status in the answer: sent, failed or queued. Later, GET /v1/sms/messages/{id} shows it again.

curl https://api.interweb.ao/v1/sms/messages -X POST \
-H "Authorization: Bearer $SMS_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: sms-receipt-1042" \
-d '{"number": "923456789", "text": "We have received your payment for invoice 1042. Thank you!"}'

The first send, step by step, is in sending SMS through the API, and what to write in a confirmation in payment confirmation SMS.

Reminders that cancel themselves when the customer pays

When your system issues an invoice to a customer, it can schedule the reminder straight away: add send_at to the request, with the date and time in ISO 8601 and the time zone, in the future and no more than 365 days ahead. The message becomes scheduled and credits are only taken when it goes out. If the customer pays first, your system calls POST /v1/sms/messages/{id}/cancel and nobody gets a reminder for a bill already paid. Only messages still scheduled can be cancelled; others answer 409 with not_cancellable. See cancelling a scheduled SMS through the API and, for the wording, payment reminder SMS.

A complete integration, in one table

In your system API request Permission
A customer paid you for their domain renewal GET /v1/invoices?status=unpaid and POST /v1/invoices/{id}/pay billing:read, billing:pay
Confirm the payment to the customer POST /v1/sms/messages sms:write
You issued an invoice to the customer POST /v1/sms/messages with send_at sms:write
The customer paid before the reminder POST /v1/sms/messages/{id}/cancel sms:write
The customer asked not to receive SMS POST /v1/sms/opt-outs. From then on, sending to that number answers 422 number_opted_out. sms:write
The customer stopped paying you for a domain PUT /v1/domains/{domain}/auto-renew with auto_renew set to false: renewal invoices stop being issued. domains:write
The monthly report GET /v1/billing/transactions and GET /v1/sms/stats billing:read, sms:read
Test everything in Sandbox first. With a sandbox_ key, the payment is simulated and the answer tells you, in credit_sufficient and within_spending_limit, whether it would go through; SMS are simulated and use no credits. If your system is WHMCS, there is already a guide with a ready-made hook: SMS notices from your own WHMCS.

Want to connect your system to your account? See the API page and start with a Sandbox key, which charges and sends nothing.

See the API page

SEE ALSO

Invoices through the API: listing, PDF, paying with credit, renewal cycle and cancellations

Sending SMS through the API: key, permissions, Sandbox and your first request

Cancelling a scheduled SMS through the API, for example when the customer has already paid

SMS notices from your own WHMCS: a hook that calls our SMS API on invoices and payments

Credit balance in your account: where it comes from and how it is used on invoices

Sensitive API permissions and the spending limit: paying, buying, reinstalling, cPanel access

Was this answer helpful? Your answer helps us improve this article.