> For the complete documentation index, see [llms.txt](https://xact.gitbook.io/xact/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xact.gitbook.io/xact/sdk/javascript/associate-a-token.md).

# Associate a Token

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: <a href="#pre-requisites" id="pre-requisites"></a>

{% content-ref url="/pages/-Mg56\_a4eJ5VygETYHMC" %}
[Getting started](/xact/sdk/javascript/getting-started.md)
{% endcontent-ref %}

## Step 1: Import modules

Let's continue building on the *index.js* from the previous example ([Environment Set-up](/xact/sdk/javascript/getting-started.md#step-1-set-up-your-node-js-environment)) and add the following modules:

```typescript
const { Client } = require("@xact-wallet-sdk/client");
require("dotenv").config();
```

## Step 2: Initializing Token Association

When initializing a token association, a request will be sent to the end user

```typescript
/* Update the fields with your informations */
const fromAccountId = '';
const tokenToAssociate = '';

await client.associate({fromAccountId, tokenId: tokenToAssociate});
```

## Step 3: Listen for the Token Association

Finally get notified when a new token association has been done successfully !

```typescript
/* Subscribe to new Token Associations */
client.paymentValidation().subscribe(token => {
    console.log('Associated Token', token);
});
```

## Code Check ✅

Your *index.js* file should look like this:

{% code title="index.js" %}

```typescript
const { Client } = require("@xact-wallet-sdk/client");
require("dotenv").config();

async function associate() {

    //Grab your api key from your .env file
    const apiKey = process.env.API_KEY;


    // If we weren't able to grab it, we should throw a new error
    if (apiKey == null) {
        throw new Error("Environment variables API_KEY must be present");
    }
    /* Create a new instance of Client */
    const client = new Client({apiKey});
    
    /* Init the connexion */
    await client.initConnexion();
    
    /* Update the fields with your informations */
    const tokenId = '';
    const fromAccountId = '';
    
    /* Request for association */
    await client.associate({fromAccountId, tokenId});
    
    /* Listen for Association's success */
    client.associateValidation().subscribe(token => {
        console.log('New associated token', token);
    });
}

associate();
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://xact.gitbook.io/xact/sdk/javascript/associate-a-token.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
