DEV Community

Waylon Jepsen
Waylon Jepsen

Posted on • Edited on

How to Mint an NFT with JavaScript

This article will teach you how to create and mint NFTs and query their data using JavaScript on the hashgraph network.

What the heck is an NFT?

Before we get into it, let's go over what an NFT is. NFT stands for Non-Fungible Token. The word fungible means "able to be replaced" or "replace an identical item". In short, a Non-Fungible token is irreplaceable. For a digital asset to be irreplaceable, it needs to be unique. So the tokens we will be creating are unique and irreplaceable! Some use cases for these unique tokens ensure the authenticity of information. As of right now, the main usages of NFTs have been for digital art and collectibles but they can be used for so much more. For example, one could use NFTs to protect information like a deed to a house, certificates of authenticity, educational degrees, identity cards, or other crucial unique details.

How do you make one?

For developers getting into web3 and blockchain technologies, it can be challenging to learn all the caveats of the new technologies such as learning a new programming language like Solidity. Minting an NFT on Hedera is simple and can be done with programming languages you are already familiar with like JavaScript. Making an NFT on the Hedera Network uses one of the network's core services, the Hedera Token Service(HTS). There are three officially supported SDKs for this service: Golang, Java, and JavaScript, as well as a variety of community-developed SDKs.

Environment Set up

You are free to use any of your preferred environment tools. I recommend following this short (3min) guide to set up with node.

Code Check

After configuring your account Id, private keys, and the client. your code should look something like this

Code Check

Creating the NFT

To create the NFT, you will need to import TokenCreateTransaction, PrivateKey, TokenType, and TokenSupplyType from the Hedera SDK, so include these in the require statement at the top of your file. Then you can use the TokenCreateTransaction passing in the appropriate configuration for your token. For reference, take a look at the example below.

Token Create

Here we create a token with a supply of 10. The initial collection must be zero so that you can set unique metadata for each NFT. It is also crucial that the decimals are zero so that your NFTs wont be fractional. The supply key is the key of the account permitted to mint the corresponding NFTs. We will go over minting in the next section.

Minting

Minting an NFT is straightforward. We use the TokenMintTransaction from the Hedera SDK, so be sure to include it in your imports at the top of your file. The syntax is provided below.

NFT Mint

There are multiple options for metadata storage solutions. I wrote more in-depth about the possibilities and use cases here. However, it is very straightforward and typical for developers to use the InterPlanetary File System (IPFS).

Querying NFT Information

Now that your NFT has been minted, you might want to be able to access its metadata from the network. To do this, you can use the TokenNftInfoQuery, and NftId from the Hedera SDK.

NFT Query

This query returns the metadata of the minted NFT.

If you liked this guide or have questions I will be going through some of these examples live on twitch so be sure to follow HederaTV on twitch.

Top comments (17)

Collapse
 
spaceportdevelopment profile image
Spaceport Development

First of all, thank you very much for the interesting post, but since I see this here a lot, I asked myself how you do that with the code excerpt so that it looks like it is in a computer window ... Is there a website or something ?

Collapse
 
0xjepsen profile image
Waylon Jepsen

Yeah absolutely, it's an application called carbon. Handy for making pretty code.
carbon.now.sh/

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Just a point, code shown using Carbon it isn't very accessible for those with screen readers. It's preferred to use the inbuilt syntax highlighting to be inclusive.

Thread Thread
 
0xjepsen profile image
Waylon Jepsen

Interesting, good point. I know a lot of people use it to prevent copy-pasters and encourage people to really get into the concepts and explore the syntax themselves.

Collapse
 
spaceportdevelopment profile image
Spaceport Development

Thank You

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Hi Waylon. Seems like there's a ton of networks out there to choose from - why did you choose Hedera/Hashgraph? What's the sweet spot of it? Has it a huge community? How does it compare to e.g. Polkadot?

Collapse
 
0xjepsen profile image
Waylon Jepsen • Edited

Great question! It can be overwhelming with all the networks out there. Hedera is currently the most utilized DLT, processing more transactions than Ethereum and BTC in under two years. Additionally, its predictably low fees and high throughput allow for scalability that few networks can match. A recent report by the London School of Economics showed that Hedera uses less energy than any of the other DLTs. There are so many amazing things about Hedera, but I encourage you to do some of your own research and go down the rabbit hole :). Full disclaimer: I am employed by Hedera so I have an inherent bias, I chose to work for them because of some of the details listed above. If you do have any questions about using the SDKs, please feel free to reach out.
Take a look at the capabilities Hedera

Regarding the community, there is a very active developer community, if you are curious i recommend checking out the developer discord

Collapse
 
0xjepsen profile image
Waylon Jepsen

Also I found a community member who made some info graphics on some comparison metrics

Thread Thread
 
thorstenhirsch profile image
Thorsten Hirsch

These numbers are really impressive.

Thread Thread
 
0xjepsen profile image
Waylon Jepsen

I thought so too :)

Collapse
 
stefanpt profile image
Stefanpt

So glad you did this article
Im getting the following though?
.setTokenType(TokenType.NonFungibleUnique)
^

TypeError: Cannot read property 'NonFungibleUnique' of undefined

Collapse
 
0xjepsen profile image
Waylon Jepsen

Hmm it could be a few different things. Be sure to import TokenType from hedera sdk at the top. What version of the SDK are you using?

Collapse
 
thaddaeustedcode profile image
thaddaeustedcode

Currently, programming is not required to create nft. There are ways to create such nft without coding. Everything is described very well in this post: gamerseo.com/blog/how-to-create-nf...

Collapse
 
harinda05 profile image
Harinda Samarasekara

Hi,

Is there a way to allow a list of allowed accountIds to mint on a token? Rather than limited to a single accout? Just like in ethereum anyone can mint the token?

Collapse
 
0xjepsen profile image
Waylon Jepsen

Hey good question, currently there is support for a threshold key, with allows to be configured for a list of keys to all be valid signatures. I have been talking internally with support to leave the supply key empty (allowing anyone to mint as long as supply is below max supply).

Collapse
 
jasonarends profile image
Jason Arends

Hi, your IPFS link should go to IPFS.io, not .com.

Thanks for the article, I plan on trying this.

Collapse
 
0xjepsen profile image
Waylon Jepsen

Thanks for the tip! Let me know how it goes!