SDK Guide
For Websites/Dapps
8 min
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 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/ 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()