Skip to main content

mod_nibblebill — Real-Time Prepaid Billing

mod_nibblebill is an applications module that performs real-time, per-second billing against an ODBC-connected database while a call is in progress. It reads a billing rate and account identifier from channel variables, then periodically debits the running balance and takes a configured action — such as playing a warning tone or hanging up — when the balance falls below defined thresholds.

Reach for this module when you need prepaid calling card or wholesale VoIP billing where you must cap expenditure per call, warn callers at a low-balance threshold, and guarantee that a zero-balance account cannot run indefinitely.

Loading the Module

The module name is mod_nibblebill. In the vanilla FreeSWITCH installation it is commented out in conf/vanilla/autoload_configs/modules.conf.xml and is not loaded by default. To enable it, uncomment the entry:

<load module="mod_nibblebill"/>

No special compile-time flag is required; the module is included in the standard FreeSWITCH source tree under src/mod/applications/mod_nibblebill.

Configuration: nibblebill.conf.xml

Place the configuration file at conf/autoload_configs/nibblebill.conf.xml. The module requires an ODBC DSN; without one no billing transactions are written.

ParameterPurposeAccepted ValuesDefault
odbc-dsnODBC data source name (or database:user:password form) for the billing databaseString(none — must be set)
db_dsnDeprecated alias for odbc-dsn, kept for backwards compatibility. Setting it logs a deprecation warning. Use odbc-dsn instead.String(none)
db_tableTable that holds account balancesString(none)
db_column_cashColumn storing the monetary balanceString(none)
db_column_accountColumn storing the account identifierString(none)
custom_sql_lookupCustom SELECT that returns nibble_balance; channel variables are expanded. Overrides column-name params.SQL string(none — uses column params)
custom_sql_saveCustom UPDATE for deducting a charge; channel variable nibble_bill holds the debit amount. Overrides column-name params.SQL string(none — uses column params)
global_heartbeatHow often (seconds) to bill mid-call via session heartbeat. 0 disables mid-call billing (bill only at hangup).Integer ≥ 00 (vanilla conf ships 60)
lowbal_amtBalance at or below which the low-balance action fires (once per call). Can be negative.Float0 (vanilla conf ships 5)
lowbal_actionDialplan application string executed when balance hits lowbal_amtApplication stringplay ding
nobal_amtBalance at or below which the call is terminated/transferred. Can be negative.Float0
nobal_actionDialplan application string or transfer destination executed when balance hits nobal_amtApplication stringhangup
percall_max_amtMaximum total charge allowed per call (fraud guard). Parsed and stored but not enforced in the current source — the enforcement logic is not implemented.Float0 (vanilla conf ships 100)
percall_actionAction taken when per-call maximum is reached. Parsed and stored but not enforced in the current source.Application stringhangup
var_name_rateChannel variable name used to read the billing rate (overrides default nibble_rate)Stringnibble_rate
var_name_accountChannel variable name used to read the account ID (overrides default nibble_account)Stringnibble_account

Trimmed example from the shipped conf:

<configuration name="nibblebill.conf" description="Nibble Billing">
<settings>
<param name="odbc-dsn" value="bandwidth.com"/>
<param name="db_table" value="accounts"/>
<param name="db_column_cash" value="cash"/>
<param name="db_column_account" value="id"/>
<param name="global_heartbeat" value="60"/>
<param name="lowbal_amt" value="5"/>
<param name="lowbal_action" value="play ding"/>
<param name="nobal_amt" value="0"/>
<param name="nobal_action" value="hangup"/>
<param name="percall_max_amt" value="100"/>
<param name="percall_action" value="hangup"/>
</settings>
</configuration>

Dialplan Applications

ApplicationPurposeArguments
nibblebillControl billing state or schedule for the current channelpause | resume | reset | adjust <amount> | heartbeat <seconds> | check | flush

Argument details:

  • pause — Suspends billing; time elapsed while paused is not charged.
  • resume — Resumes billing; the paused interval is subtracted from the next billing cycle.
  • reset — Moves the billing start timestamp to right now, discarding elapsed unbilled time.
  • adjust <amount> — Immediately credits or debits <amount> from the account in the database (positive value credits the account, negative value debits it).
  • heartbeat <seconds> — Enables session heartbeat billing at the given interval for this channel.
  • check — Logs the total amount billed so far on this channel (INFO level).
  • flush — Triggers an immediate billing cycle for elapsed time.

The channel must have nibble_rate (or the configured var_name_rate equivalent) and nibble_account (or var_name_account) set before billing will occur.

<!-- Set billing parameters and start billing -->
<action application="set" data="nibble_account=${accountcode}"/>
<action application="set" data="nibble_rate=0.05"/>
<action application="bridge" data="sofia/gateway/carrier/$1"/>

<!-- Mid-call: pause billing while playing a menu -->
<action application="nibblebill" data="pause"/>

<!-- Resume billing after the menu -->
<action application="nibblebill" data="resume"/>

<!-- Enable per-channel heartbeat billing every 30 seconds -->
<action application="nibblebill" data="heartbeat 30"/>

API Commands

CommandPurposeSyntax
nibblebillControl billing state for any active channel by UUIDnibblebill <uuid> [pause | resume | reset | adjust <amount> | heartbeat <seconds> | check | flush]

fs_cli examples:

nibblebill 1234-abcd-5678-efgh pause
nibblebill 1234-abcd-5678-efgh adjust -2.50
nibblebill 1234-abcd-5678-efgh check
nibblebill 1234-abcd-5678-efgh heartbeat 60

Channel Variables

VariableSet / ReadPurpose
nibble_rateReadBilling rate in dollars per minute. Default variable name; overridden by var_name_rate in config.
nibble_accountReadAccount identifier used to look up and debit the balance. Default variable name; overridden by var_name_account in config.
nibble_incrementReadBilling increment in seconds. When set, charges are rounded up to the next full increment instead of billed by exact elapsed time.
nibble_roundingReadRounding precision exponent. The total billed amount is rounded up to nibble_rounding decimal places at hangup (internally uses 10^nibble_rounding as the rounding factor).
nibble_minimumReadMinimum charge in dollars applied at hangup if total billed falls below this value.
nobal_amtReadPer-channel override of the global nobal_amt threshold.
lowbal_amtReadPer-channel override of the global lowbal_amt threshold.
nibble_billSetSet to the current debit amount immediately before executing custom_sql_save, allowing the SQL to reference ${nibble_bill}.
nibble_total_billedSetUpdated after every successful billing cycle with the cumulative amount charged on this call.
nibble_current_balanceSetSet to the account's remaining balance at call hangup.

Source: src/mod/applications/mod_nibblebill, conf/vanilla/autoload_configs/nibblebill.conf.xml