The following sample will show you how to create a NFT on Hedera Hashgraph. We will first generate a request for the end user. Once validated, you will get notified for the NFT's creation success.
Prerequisites:
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:
When initializing a NFT's creation, a request will be sent to the end user
/* Update the fields with your informations */
const name = 'NFT Test';
const description = 'Description of my NFT';
const category = CategoryNFT.ART;
const creator = 'Johny.B';
const cid = ''; /* cid linked to the NFT - IPFS storage */
const supply = 1; /* Nb of NFT available */
const fromAccountId= ''; /* Account ID of the user */
const customRoyaltyFee = {
numerator: 1,
denominator: 10,
fallbackFee: 100, /* in Hbar */
};/* Optional*/
await client.createNFT({fromAccountId, name, description, category, creator, cid, supply, customRoyaltyFee});
Step 4: Listen for the NFT's creation
Finally get notified when your NFT has been successfully created !
/* Subscribe to new Create NFT Validation */
client.createNFTValidation().subscribe(nft => {
console.log('NFT Created', nft);
});
Code Check ✅
Your index.js file should look like this:
const { Client, CategoryNFT } = require("@xact-wallet-sdk/client");
require("dotenv").config();
async function createNFT() {
//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 name = 'NFT Test';
const description = 'Description of my NFT';
const category = CategoryNFT.ART;
const creator = 'Johny.B';
const media = ''; /* base64 format */
const supply = 1; /* Nb of NFT available */
const fromAccountId= ''; /* Account ID of the user */
/* Request for NFT's creation */
await client.createNFT({fromAccountId, name, description, category, creator, media, supply});
/* Listen for Creation's success */
client.createNFTValidation().subscribe(nft => {
console.log('NFT Created', nft);
});
}
createNFT();