Setup
This guide will walk you through how to install and setup the Immutable Commerce Widget in your application.
Install Checkout
The Immutable Commerce Widget can be included in your application by first installing the SDK in one of two different ways:
- Install the npm module and import the Checkout SDK
- or...
- Load the SDK via a CDN by pasting a script tag into your application.
The Immutable Commerce Widget will be loaded dynamically as a separate bundle into your application when you create the Checkout SDK instance and call the widgets()
factory function.
- npm module
- JavaScript via CDN
Prerequisites
Node Version 20 or later
To install nvm
follow these instructions. Once installed, run:
nvm install --lts
- (Optional) To enable Code Splitting (importing only the SDK modules you need) there are additional prerequisites.
Install the Immutable SDK
Run the following command in your project root directory.
- npm
- yarn
npm install -D @imtbl/sdk
# if necessary, install dependencies
npm install -D typescript ts-node
yarn add --dev @imtbl/sdk
# if necessary, install dependencies
yarn add --dev typescript ts-node
The Immutable SDK is still in early development. If experiencing complications, use the following commands to ensure the most recent release of the SDK is correctly installed:
- npm
- yarn
rm -Rf node_modules
npm cache clean --force
npm i
rm -Rf node_modules
yarn cache clean
yarn install
To add the SDK to an application using the CDN the following script can be placed inside the head tag.
<script src="https://cdn.jsdelivr.net/npm/@imtbl/sdk/dist/browser/checkout/sdk.js"></script>
Quickstart
- React
- JavaScript
The Commerce Widget is designed to be used on the client-side only. The window
and document
objects are required.
The following code snippet shows the recommened way to intiaise and create the widget:
- You must create an instance of the Checkout SDK first
- We recommend creating and initialising the Widget at the beginning of your application
This ensures the Widget is correctly synced with the provider once the user connects their wallet.
import { useEffect } from 'react';
import { checkout, config } from '@imtbl/sdk';
// Create an instance of the Checkout SDK and configure the environment SANDBOX / PRODUCTION
const checkoutSDK = new checkout.Checkout({
baseConfig: { environment: config.Environment.SANDBOX },
bridge: { enable: true },
swap: { enable: true },
onRamp: { enable: true }
});
export const App = () => {
useEffect(() => {
(async () => {
// Get an instance of the factory to be able to create the Widget
const factory = await checkoutSDK.widgets({
config: { theme: checkout.WidgetTheme.DARK },
});
// RECOMMENDED: Create the Commerce Widget once at the start of your application.
const widget = factory.create(checkout.WidgetType.IMMUTABLE_COMMERCE, {
// provider: web3Provider // pass in a Web3Provider if already connected to a wallet
config: {
// pass in the configuration for each flow
WALLET: {
showNetworkMenu: true,
showDisconnectButton: true,
}
}
});
// Mount the Commerce Widget at the element id provided
// You can mount and unmount this widget in specific parts of your application as needed.
widget.mount('mount-point', {
// the flow to be used
flow: checkout.CommerceFlowType.CONNECT,
// ... other params, will depend on the selected flow
});
})();
}, []);
return (<div id="mount-point" />);
};
The following codeblock demonstrates how to use the Commerce Widget in the HTML file of an application by accessing the SDK via the CDN.
- You must create an instance of the Checkout SDK first
- We recommend creating and initialising the Widget at the beginning of your application
This ensures the Widget is correctly synced with the provider once the user connects their wallet.
<html>
<head>
<!-- Load the SDK from jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/@imtbl/sdk/dist/browser/checkout/sdk.js"></script>
</head>
<body>
<!-- Create a mount point for the widget -->
<div id="mount-point"></div>
<script>
// Initialize Checkout SDK
const checkout = new ImmutableCheckout.Checkout();
// Initialize the widgets factory
(async function () {
const factory = await checkout.widgets({
config: { theme: ImmutableCheckout.WidgetTheme.DARK },
});
// RECOMMENDED: Create the Commerce Widget once at the start of your application.
const widget = widgets.create(
ImmutableCheckout.WidgetType.IMMUTABLE_COMMERCE
);
// Mount the Commerce Widget at the element id provided
// You can mount and unmount this widget in specific parts of your application as needed.
widget.mount('mount-point', {
// the flow to be used
flow: 'CONNECT',
// ... other params, will depend on the selected flow
});
})();
</script>
</body>
</html>
Configuration & versioning
The checkout.widgets()
factory function can be provided config
and a version
, which can be used to configure the theme and the specific SDK version to use.
import { checkout } from "@imtbl/sdk";
const checkoutSDK = new checkout.Checkout();
// @ts-ignore
const factory = await checkoutSDK.widgets({
config: {
language: 'en',
theme: checkout.WidgetTheme.DARK,
},
version: {
major: 1,
minor: 2,
patch: 3,
},
});
Property | Description |
---|---|
config | The config object can be used to configure the Immutable Commerce Widget. This is currently used to set the theme, language, and enable certain features. |
version | The SDK automatically downloads the latest compatible version of the library. However, optionally you can pass the desired version of widgets to load. |
Bundle loading
The Immutable Commerce Widget is built and released with every new Immutable SDK release following the Immutable SDK versioning.
Most are automatically applied to ensure continued and seamless improvements to the product for our partners, as our SDK will load the widgets bundle from https://cdn.jsdelivr.net when checkout.widgets()
is called. If a specific version is required then the version can be set when the factory function checkout.widgets()
is called.
When a breaking change is required in the widget bundle, updates will pause until the package is manually upgraded.
Only non breaking changes are seamlessly applied to prevent disruptions in your application.
Widget factory
The checkout.widgets()
function returns a widget factory object that can be used to create any widget, update config and provider.
Method | Parameters | Description |
---|---|---|
create(type, props) | type : the widget type to instantiate props : the flow configurations and provider to create the widget | To customise widget behaviour. |
updateProvider(provider) | provider : the Web3Provider object to update all the widgets | To update the wallet provider. This will be reflected on all flows |
- React
- JavaScript
import { Web3Provider } from '@ethersproject/providers';
import { checkout } from '@imtbl/sdk';
const checkoutSDK = new checkout.Checkout();
// Get an instance of the factory to be able to create the Widget
// @ts-ignore
const factory = await checkoutSDK.widgets({
config: {
language: 'en',
theme: checkout.WidgetTheme.DARK,
},
});
// Create a Commerce Widget using the factory
// @ts-ignore
const widget = factory.create(checkout.WidgetType.IMMUTABLE_COMMERCE, {
// @ts-ignore
// if application is already connected to a wallet
provider: web3Provider,
// optional configuration by flow
config: {
WALLET: {
// showDisconnectButton: true,
// showNetworkMenu: true,
},
SALE: {
// hideExcludedPaymentTypes: true,
// waitFulfillmentSettlements: true,
},
// SWAP: {},
// ONRAMP: {},
// CONNECT: {},
// BRIDGE: {},
// ADD_FUNDS: {},
},
// @ts-ignore
});
// Use this at anytime to update the provider used by the widget
// after the widget was created.
// @ts-ignore
factory.updateProvider(new Web3Provider(provider));
// Get an instance of the factory to be able to create the Widget
const factory = await checkout.widgets({
config: {
theme: 'light',
language: 'en',
},
});
// Create a Commerce Widget using the factory
const widget = factory.create(ImmutableCheckout.WidgetType.IMMUTABLE_COMMERCE, {
// if application is already connected to a wallet
provider: web3Provider,
// optional configuration by flow
config: {
WALLET: {
// showDisconnectButton: true,
// showNetworkMenu: true,
},
SALE: {
// hideExcludedPaymentTypes: true,
// waitFulfillmentSettlements: true,
},
// SWAP: {},
// ONRAMP: {},
// CONNECT: {},
// BRIDGE: {},
// ADD_FUNDS: {},
},
});
// Use this at anytime to update the provider used by the widget
// after the widget was created.
factory.updateProvider(new Web3Provider(provider));
Widget interface
Method | Parameters | Description |
---|---|---|
mount(id, params) | id : ID of the DOM element where the widget will be mounted params : the specific widget parameters | Mount a widget to a DOM reference element |
unmount() | Unmount a widget and reset parameters | |
update(props) | props : the widget specific properties including configuration and provider to be updated | Update the widget configuration |
addListener(type, callback) | type : the widget specific event name callback:(data) : function to execute when the event is received | Add a listener for a widget event |
removeListener(type) | type : the widget specific event name | Removes an event listener for a widget event |
Widget Events
The Immutable Commerce Widget emits events when critical actions are taken by the user or key states are reached. Subscribing to these events allows your application to respond appropriately to changes in the widget's state.
Event Type | Description | Event Payload |
---|---|---|
CommerceEventType.INITIALISED | The widget has been initialised. | |
CommerceEventType.PROVIDER_UPDATED | The provider has been updated. | CommerceProviderUpdatedEvent |
CommerceEventType.CLOSE | The user clicked the close button on the widget. This should usually be wired up to call the widget's unmount() function. | |
CommerceEventType.SUCCESS | The user has completed the flow successfully. | CommerceSuccessEvent |
CommerceEventType.FAILURE | There has been an error in the flow. | CommerceFailureEvent |
CommerceEventType.DISCONNECTED | The user has disconnected their wallet. | |
CommerceEventType.USER_ACTION | The user has taken an action in the flow. | CommerceUserActionEvent |
For detailed information on the events specific to each flow, please refer to the respective flow's documentation page.