Skip to content

JavaScript API Wrapper

  • Automatic CSRF handling — No need to manage tokens manually
  • Built-in spinner — Shows a loading indicator during requests
  • Less coupling — If the API changes, the wrapper absorbs more of that churn
  • Cleaner code — Less boilerplate for you

The bundled admin and Huraga themes already load the wrapper. For a custom theme, add this to your base layout before your theme JavaScript:

{{ 'js/fossbilling.js'|public_asset_url|script_tag }}
{{ 'js/api.js'|public_asset_url|script_tag }}
<script src="{{ 'build/js/mytheme.js'|asset_url }}"></script>

The script_tag filter adds a cache-busting hash and prevents the same script from being rendered more than once.

The wrapper automatically binds API forms and links when the page loads.

Load a FOSSBilling page first to establish the session and token. The wrapper reads the fossbilling_csrf cookie and sends the token on same-origin requests, including protected guest calls. If you include a CSRFToken form field explicitly, use the current page’s token. See the API reference for which requests require a token and where to send it.

The wrapper follows this pattern:

FOSSBilling.api.{role}.{method}(endpoint, params, onSuccess, onError, showSpinner)
ParameterDescription
roleadmin, client, or guest
methodUse get or post for core routes; guest authentication requires post. Prefer post for mutations.
endpointThe API endpoint (e.g., "currency/get_pairs")
paramsRequest parameters (object)
onSuccessCallback that receives the API response's result value.
onErrorCallback that receives { message, code } when the request fails.
showSpinnerBoolean to show/hide the loading spinner (optional, default: true)
FOSSBilling.api.guest.get(
"currency/get_pairs",
{},
function(response) {
console.log("Currency pairs:", response);
},
function(error) {
console.error("Error:", error);
}
);
FOSSBilling.api.admin.post(
"client/get_list",
{ per_page: 10, page: 1 },
function(response) {
console.log("Clients:", response.list);
},
function(error) {
console.error("Failed to load clients:", error);
}
);
FOSSBilling.api.client.get(
"profile/get",
{},
(profile) => console.log("First name:", profile.first_name),
(error) => console.error("Failed to load profile:", error.message)
);

The wrapper creates and removes the spinner element automatically. Your theme provides the .spinner-border styling.

Spinner example

Pass false as the fifth argument to disable the spinner for a request:

FOSSBilling.api.admin.post('client/get_list', {}, onSuccess, onError, false);

The Twig helpers fb_api_form() and fb_api_link() render a data-fb-api attribute that the wrapper uses for automatic form and link handling.

Use api_url for the target endpoint:

<form action="{{ 'profile/update'|api_url }}" {{ fb_api_form({ message: 'Profile updated'|trans }) }}>
<input type="text" name="first_name" value="{{ profile.first_name }}">
<button type="submit">{{ 'Save'|trans }}</button>
</form>

fb_api_form() adds method="post" and data-fb-api. The wrapper serializes the form, adds the CSRF token automatically for session-authenticated calls, disables enabled buttons while the request is running, and shows errors with FOSSBilling.message().

Use API links for read-only requests:

<a href="{{ 'currency/get_pairs'|api_url(role: 'guest') }}"
{{ fb_api_link({ callback: 'showCurrencies' }) }}>
{{ 'Load Currency Pairs'|trans }}
</a>

Define the callback in your theme JavaScript. The wrapper sends a GET request and passes the result to the callback.

window.showCurrencies = (pairs) => console.table(pairs);

Supported data-fb-api options:

OptionApplies toDescription
messageForms and linksShow a success toast after the request.
redirectForms and linksRedirect to the given URL after success.
reloadForms and linksPass true to reload the current page after success.
callbackForms and linksName of a global window function to call with the API result.
paramsLinksExtra request parameters merged into the link request.
modalLinksShow a confirmation, danger, or prompt modal before the request.

For a prompt modal, set modal.key to the request parameter that should receive the entered value.

If you need to call the API without the wrapper, see the fetch() example in the API reference.