Why Use the Wrapper?
Section titled “Why Use the 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
Importing the Wrapper
Section titled “Importing the Wrapper”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.
CSRF Tokens
Section titled “CSRF Tokens”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.
Making Requests
Section titled “Making Requests”The wrapper follows this pattern:
FOSSBilling.api.{role}.{method}(endpoint, params, onSuccess, onError, showSpinner)| Parameter | Description |
|---|---|
role | admin, client, or guest |
method | Use get or post for core routes; guest authentication requires post. Prefer post for mutations. |
endpoint | The API endpoint (e.g., "currency/get_pairs") |
params | Request parameters (object) |
onSuccess | Callback that receives the API response's result value. |
onError | Callback that receives { message, code } when the request fails. |
showSpinner | Boolean to show/hide the loading spinner (optional, default: true) |
Examples
Section titled “Examples”Get Currency Pairs (Guest)
Section titled “Get Currency Pairs (Guest)”FOSSBilling.api.guest.get( "currency/get_pairs", {}, function(response) { console.log("Currency pairs:", response); }, function(error) { console.error("Error:", error); });Get Client List (Admin)
Section titled “Get Client List (Admin)”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); });Get the Signed-In Client's Profile
Section titled “Get the Signed-In Client's Profile”FOSSBilling.api.client.get( "profile/get", {}, (profile) => console.log("First name:", profile.first_name), (error) => console.error("Failed to load profile:", error.message));Loading Spinner
Section titled “Loading Spinner”The wrapper creates and removes the spinner element automatically. Your theme provides the .spinner-border styling.

Pass false as the fifth argument to disable the spinner for a request:
FOSSBilling.api.admin.post('client/get_list', {}, onSuccess, onError, false);API Forms and Links
Section titled “API Forms and Links”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:
| Option | Applies to | Description |
|---|---|---|
message | Forms and links | Show a success toast after the request. |
redirect | Forms and links | Redirect to the given URL after success. |
reload | Forms and links | Pass true to reload the current page after success. |
callback | Forms and links | Name of a global window function to call with the API result. |
params | Links | Extra request parameters merged into the link request. |
modal | Links | Show 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.
Manual Requests
Section titled “Manual Requests”If you need to call the API without the wrapper, see the fetch() example in the API reference.