For Websites/Dapps
This guide is for anyone who wishes to integrate the Enefits SDK into a website or dapp. Before continuing, we highly recommend reviewing the General Usage guide to familiarize yourself with general SDK workflow concepts before continuing.
You will need to generate an API key with your Enefits account before continuing with integration.
To obtain an API Key, simply register at http://enefits.co, go to Developer in the account menu in the top right section. From there, generate an API Key and save this for all API requests as defined below. We only show this once so if you lose this API Key, you’ll have to re-generate from the Developer section.
Run the demo
To run a live demo, visit https://x.enefits.co/demo/ and provide your own API key where requested.
Download the SDK
The Enefits JS SDK can be loaded before the closing </head> tag in your HTML code.
<script src="https://x.enefits.co/sdk/enefits.js"></script>
Initialize the SDK
/**
* Enefits JS SDK will fire the event `enefits_init_success` on
* successful initialisation.
*/
document.addEventListener('enefits_init_success', function (event) {
// Enefits SDK is now ready for use.
});
Enefits.init("YOUR_API_KEY_HERE");Connect Wallet
Display a button or call-to-action (for example: Connect Wallet) which when pressed or clicked will prompt the user to connect a wallet. After the user selects a wallet and initiates a session, the returned address will be available to the Enefits SDK to check NFTs and any offers the user is eligible for.
/**
* Event `enefits_connected` is fired on successful connection
* with a blockchain account.
*
* The chain data and account are availabe as `event.detail.chain` and
* `event.detail.account` respectively.
*/
document.addEventListener('enefits_connected', function (event) {
console.log('Enefits connected to blockchain:', event.detail.chain);
console.log('Enefits connected with account:', event.detail.account);
});
/**
* Event `enefits_connect_error` is fired when SDK fails to connect.
*/
document.addEventListener('enefits_connect_error', function (event) {
alert("Enefits SDK failed to connect to a blockchain account");
});
/**
* This function will open a modal box with a list of supported
* providers (defaults to MetaMask).
*
* Enefits SDK will fire the event `enefits_connected` after successfully
* connecting with a blockchain account. If connection fails, the event
* `enefits_connect_error` will be fired.
*/
Enefits.connectAccount();Get all Enefits Offers
This method will return all Offers that the user is eligible for based on the address they provided when they connected their wallet.
/**
* Returns a list of Offers applicable to the NFTs owned by the connected account.
* Each Offer object has the following attributes:
* - `name`
* - `promo_description`
* - `offer_url`
* - `expires_at`
*/
Enefits.getOffers().then(function (offers) {
console.log('Available offers:', offers);
})Based on the response from this method and the existence of a specific offer_id value that the website/dapp is looking for, your app logic will handle accordingly.
Get all NFTs held by wallet
/**
* Event `enefits_collections_success` is fired once the SDK fetches the
* list of NFT/Token collections held by the connected account.
*
* Event `enefits_collections_error` is fired if SDK fails to fetch the
* list of NFT/Token collections for the connected account.
*/
document.addEventListener('enefits_collections_success', function (event) {
// `event.detail.collections` is an array of objects
// Each object has a `contractAddress` for the NFT and `total` denotes
// the number of tokens owned by the connected address
console.log("List of NFTs:", event.details.collections)
});
document.addEventListener('enefits_collections_error', function (event) {
alert("Enefits SDK failed to fetch NFTs for the connected account")
})
/**
* Get a list of NFTs/tokens held by the connected account.
*/
Enefits.getNFTs()Helper Functions
Use the functions provided below to create a tighter integration with the Enefits SDK and manage user connectivity states.
/**
* Check if Enefits SDK has successfully initialised.
*
* @return boolean
*/
Enefits.isInitComplete()
/**
* Check if Enefits SDK was able to connect to a blockchain account.
*
* @return boolean
*/
Enefits.isAccountConnected()
/**
* Get the address of the connected account.
*
* @return string
*/
Enefits.getConnectedAccount()
/**
* Get the blockchain info for the connected account.
*
* @return object
*/
Enefits.getChainData()
/**
* Disconnect from the blockchain account. On successful disconnect,
* Enefits fires event `enefits_disconnected`
*/
Enefits.disconnect()