Xact
  • Welcome
  • SDK
    • About our SDK
    • JavaScript
      • Getting started
      • Login
      • Refresh an Account
      • Send Hbar
      • Associate a Token
      • Transfer a Token
      • Create a NFT
      • Sell a NFT
      • Withdraw a NFT from Sale
      • Buy a NFT
      • Buy a NFT from QR Code
    • PHP
      • Getting started
      • Login
      • Refresh an Account
      • Send Hbar
      • Associate a Token
      • Transfer a Token
      • Create NFT
      • Sell a NFT
      • Withdraw a NFT from Sale
      • Buy a NFT
      • Buy a NFT from QR Code
  • Resources
    • Demo applications
  • Support and Community
  • Discord Server
  • Telegram Group
Powered by GitBook
On this page
  1. SDK
  2. JavaScript

Buy a NFT

PreviousWithdraw a NFT from SaleNextBuy a NFT from QR Code

Last updated 3 years ago

CtrlK
  • Prerequisites:
  • Step 1: Import the following modules
  • Step 2: Initializing Buy NFT
  • Step 3: Listen for the NFT's buy
  • Code Check ✅

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:

Getting started

Step 1: Import the following modules

Let's continue building on the index.js from the previous example (Environment Set-up) and add the following modules:

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

Step 2: Initializing Buy NFT

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

/* 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

Step 3: Listen for the NFT's buy

Finally get notified when your NFT has been successfully bought !

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

Code Check ✅

Your index.js file should look like this:

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();

tokenId
=
''
/* tokenId of the NFT */
await client.buyNFT(tokenId, {fromAccountId,sellerAccountId,nftIds});