> 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/buy-a-nft.md).

# Buy a NFT

The following sample will show you how to buy a NFT. We will first generate a request for the end user. Once validated, you will get notified.

## 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 the following modules <a href="#step-1-import-de-following-modules" id="step-1-import-de-following-modules"></a>

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:

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

## Step 2: Initializing Buy NFT <a href="#step-2-initializing-token-transfer" id="step-2-initializing-token-transfer"></a>

When initializing a NFT's buy, a request will be sent to the end user

```javascript
/* Update the fields with your informations */
const fromAccountId = ''; /* account Id of the buyer */
/* Account ID of the Seller */
const sellerAccountId = '';
const nftIds = ['1@0.0.123456'] /* ID of the NFTs you want to buy */
const tokenId = '' /* tokenId of the NFT */

await client.buyNFT(tokenId, {fromAccountId,sellerAccountId,nftIds});
```

## Step 3: Listen for the NFT's buy <a href="#step-3-listen-for-the-token-transfer" id="step-3-listen-for-the-token-transfer"></a>

Finally get notified when your NFT has been successfully bought !

```javascript
/* Subscribe to new NFT bought */
client.buyNFTValidation().subscribe(nft => {
    console.log('NFT successfully bought', nft);
});
```

## Code Check ✅ <a href="#code-check" id="code-check"></a>

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

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

async function buyNFT() {

    //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 fromAccountId = ''; /* account Id of the buyer */
    /* Account ID of the Seller */
    const sellerAccountId = '';
    const tokenId = '' /* tokenId of the NFT */
    const nftIds = ['1@0.0.123456'] /* ID of the NFTs you want to buy */

    await client.buyNFT(tokenId, {fromAccountId,sellerAccountId,nftIds});
    /* Listen for Creation's success */
    client.buyNFTValidation().subscribe(nft => {
        console.log('NFT successfully bought', nft);
    });
}

buyNFT();
```
