The following sample will show you how to prompt a user for a token association. We will first generate a request for the end user. Once validated, you will get notified about token association status.
Prerequisites:
Step 1: Import modules
Let's continue building on the index.js from the previous example (Environment Set-up) and add the following modules:
When initializing a token association, a request will be sent to the end user
/* Update the fields with your informations */constfromAccountId='';consttokenToAssociate='';awaitclient.associate({fromAccountId, tokenId: tokenToAssociate});
Step 3: Listen for the Token Association
Finally get notified when a new token association has been done successfully !
/* Subscribe to new Token Associations */client.paymentValidation().subscribe(token => {console.log('Associated Token', token);});
Code Check ✅
Your index.js file should look like this:
index.js
const { Client } =require("@xact-wallet-sdk/client");require("dotenv").config();asyncfunctionassociate() {//Grab your api key from your .env fileconstapiKey=process.env.API_KEY;// If we weren't able to grab it, we should throw a new errorif (apiKey ==null) {thrownewError("Environment variables API_KEY must be present"); }/* Create a new instance of Client */constclient=newClient({apiKey});/* Init the connexion */awaitclient.initConnexion();/* Update the fields with your informations */consttokenId='';constfromAccountId='';/* Request for association */awaitclient.associate({fromAccountId, tokenId});/* Listen for Association's success */client.associateValidation().subscribe(token => {console.log('New associated token', token); });}associate();