Sell a NFT

The following sample will show you how to sell a NFT using the Hedera Hashgraph and Filecoin to store the media. We will first generate a request for the end user. Once validated, you will get notified for the NFT's selling success.

In order to guarantee the process, the NFT will be transferred on an escrow account. User could withdraw anytime and get NFT back.

Prerequisites:

pageGetting 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

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

/* Update the fields with your informations */
const fromAccountId = '' /* Account Id of the Seller */
const hbarAmount = 10 /* Unit Price per NFT in Hbar */
const nftIds = ['1@0.0.123456'] /* Array of nftIds to sell */
/* You can specify a quantity if you prefer to let us choose randomly for you */
/* But nftIds field cannot be set with quantity */
const tokenId = ''; /* token Id of the NFT */
const isCollection = ''; /* Sell the nftIds under one QR Code */
/* Optional NOT YET AVAILABLE - The NFT could only be sell to accountId present on that list */
const accountIds = ['', ''];
/* Optional - Add a middle men in order to collect fees */
const middleMen = {
    middleManAccountId,
    middleManTypeOfFees: MiddleManFeesType.HBAR,
    middleManFees: 10
};

/* If you're selling by nftIds replace quantity by ndtIds */
await client.sellNFT({fromAccountId, nftIds, hbarAmount, tokenId, accountIds, isCollection, middleMen});

Step 3: Listen for the NFT's sell

Finally get notified when your NFT has been successfully set in sale !

/* Subscribe to new sale NFT Validation */
client.sellNFTValidation().subscribe(nft => {
    console.log('NFT successfully set in sale', nft);
});

Code Check ✅

Your index.js file should look like this:

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

async function sellNFT() {

    //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 Seller */
    const hbarAmount = 10 /* Unit Price per NFT in Hbar */
    const nftIds = ['1@0.0.123456'] /* Array of nftIds to sell */
    const tokenId = ''; /* token Id of the NFT */
    
    /* If you're selling by nftIds replace quantity by ndtIds */
    await client.sellNFT({fromAccountId, hbarAmount, tokenId, nftIds});
    
    /* Subscribe to new sale NFT Validation */
    client.sellNFTValidation().subscribe(nft => {
        console.log('NFT successfully set in sale', nft);
    });
}

sellNFT();

Last updated