# JavaScript SDK

The Banxa JavaScript SDK provides a lightweight alternative to manually constructing referral URLs. Include a single script tag and use the `Banxa` class to trigger redirect or iFrame checkout from any button or event in your page.

## Include the SDK

Add the following loader script to your page. It asynchronously loads the SDK and calls your `yourOnLoadFunction` callback when ready.


```javascript
!function(callback){
  var b = document.createElement('script');
  b.type = "text/javascript";
  b.async = true;
  b.src = "https://sdk.banxa.com/js/banxa-sdk-latest.js";
  var x = document.getElementsByTagName('script')[0];
  x.parentNode.insertBefore(b, x);
  if (callback) { b.addEventListener("load", function() { callback() }) }
}(yourOnLoadFunction);
```

## Initialise the Banxa class

Create an instance of `Banxa` with your partner name. Use the second argument to select sandbox or production.


```javascript
// Production
const banxa = new Banxa('your-partner-name');

// Sandbox
const banxa = new Banxa('your-partner-name', 'sandbox');
```

Replace `your-partner-name` with the partner subdomain provided by Banxa (e.g., `binance`, `metamask`).

## Redirect to a new tab

Open the Banxa checkout in a new browser tab when a button is clicked.


```javascript
banxa.redirect('#redirect', {
  fiatType: 'AUD',
  coinType: 'BTC',
  fiatAmount: 500,
  walletAddress: '3Hiy7HuFcqwkgERyfRSwEHqrwSwTirm8zb',
  theme: 'dark'
});
```


```html
<button id="redirect">Buy crypto</button>
```

The first argument (`#redirect`) is a CSS selector for the element that triggers the checkout on click.

## Embed as an iFrame

Inject an iFrame into a container element in your page.


```javascript
banxa.iframe(
  '#iframeButton',   // trigger element selector
  '#iframeTarget',   // container element selector
  {
    fiatType: 'AUD',
    coinType: 'BTC',
    fiatAmount: 200,
    walletAddress: '3Hiy7HuFcqwkgERyfRSwEHqrwSwTirm8zb'
  },
  '800px',           // optional width (pass false to omit)
  '400px'            // optional height (pass false to omit)
);
```


```html
<button id="iframeButton">Open checkout</button>

<div id="iframeTarget">
  <!-- iFrame is injected here -->
</div>
```

## Supported parameters

All [referral URL parameters](/products/hosted-checkout/docs/referral-integration/supported-parameters) are supported as keys in the options object passed to `redirect()` and `iframe()`.

## Notes

- The SDK generates a referral URL from the options you provide and opens it via redirect or iFrame — it does not make server-to-server API calls.
- For server-to-server order creation (with webhooks, order IDs, and KYC sharing), use the [API integration](/products/hosted-checkout/docs/api-integration/api-integration-overview) instead.