Minting a Cardano NFT using Java

Gino Busok Jr
4 min readJan 19, 2022

This story will show how to mint an NFT in Cardano Blockchain using Java. If you are a Java developer and wanted to mint an NFT for whatever reason, this story can definitely help you.

To start, here are some assumptions and requirements

  1. You know NFTs and what IPFS is. Google is your friend :-)
  2. You know Java and Maven.
  3. You have a Cardano Wallet in Testnet. I will use Nami here since it is easy to use and it can show the NFT that you will mint.
  4. You already funded your Testnet wallet. You can get some tAda here https://testnets.cardano.org/en/testnets/cardano/tools/faucet/ or you can contact me with your address if you want some test Ada.
  5. We are going to use Blockfrost’s APIs so create an account there. We will need 2 projects. A free account will allow you to create only1 project. So you can either create 2 free accounts or purchase one of their paid plans. Note that I am not affiliated with Blockfrost but to support this wonderful project, please consider purchasing a paid plan.

To create an NFT using Java, I will use 2 awesome libraries.

  1. https://github.com/bloxbean/cardano-client-lib
  2. https://github.com/blockfrost/blockfrost-java

Bloxbean’s cardano-client-lib will handle the creation of our NFT. While blockfrost-java will handle the uploading of our NFT’s image to IPFS. You really do not need blockfrost-java since you can just call their IPFS API but for simplicity’s sake, we are going to use that library.

For those that just want to get the final code and the pom.xml, here you go

If you are interested on some explanations about the code, then read on.

The first thing you need to do is upload your asset to IPFS. The first 3 lines of codes will handle that.

IPFSService ipfsService = new IPFSServiceImpl(Constants.BLOCKFROST_IPFS_URL, BLOCKFROST_IPFS_PROJECT_ID);File file = new File("/path/to/your/image.jpeg");IPFSObject ipfsObject = ipfsService.add(file);// You will use the IPFS hash as the image link of our NFT
System.out.println(ipfsObject.getIpfsHash());

The code for createNewToken method is based on the sample code from the bloxbean library, which you can find here https://github.com/bloxbean/cardano-client-lib#token-minting-transaction.

In Cardano, creating a native token and an NFT are basically the same. You will need 2 things: Policy ID and the asset name. The Policy ID is generated in the first few lines of codes while we are going to set the Asset name. In our sample, we set this to “BabyPetite”. Now if we want to create an NFT, we will need to add a metadata that follows this standard https://cips.cardano.org/cips/cip25/. If you don’t want to read that link then all you need to know is that your metadata, at the minimum should follow this format. And yes, you really need to use the key 721. You cannot use other numbers.

These codes handle the creation of that metadata

/* Create the metadata. Follow the standard defined here https://cips.cardano.org/cips/cip25/ */
CBORMetadataMap nftInfo = new CBORMetadataMap()
.put("image", "ipfs://" + ipfshHash)
.put("name", "Baby Petite")
.put("etc", "Any Metadata"); // 1
CBORMetadataMap nameMetadata = new CBORMetadataMap()
.put("BabyPetite", nftInfo); // 2
CBORMetadataMap policyMetadata = new CBORMetadataMap()
.put(policyId, nameMetadata); // 3
Metadata metadata = new CBORMetadata()
.put(new BigInteger("721"), policyMetadata); //4
  1. In here we are creating the metadata inside <asset_name>. The only data required here are the image and name. Our IPFS hash is going to be passed in the image and should follow the format ipfs://ipfs-hash-here. Then the name here should be the human readable name. The etc is just there to show you that you can put any other details in your metadata. For example, if you are creating an RPG character you can put the base power here, starting life, guild, etc.
  2. Here is where we create the metadata with the Asset Name as the key and the body should be the one we created in code 1.
  3. In here we are just using Policy ID as the key as discussed in the standards.
  4. And then finally, we need to use the number 721, again as discussed in the standard as the main key of our metadata

Once we created the metadata, we then create our Transaction. Next step is to compute the Fee that we need to create the NFT. And lastly, we are then going to send the transaction to the blockchain.

If the transaction is successful, you should see the transaction ID like this

Transaction ID: d4e8233e56dc8b393a963c01234567898aaa6bb3b17f0fe195ad1d123456789a

You can check the status of the transaction in Cardano Testnet explorer https://explorer.cardano-testnet.iohkdev.io/en. Just paste the transaction ID and wait for the result. Note that sometimes it will take more than a minute for it to show in the explorer. But once you we are able to search for the ID, then you can now check your Nami wallet to verify if you already received the minted NFT. You should be able to see something like this. Of course the picture should be the one that you uploaded. In my case, I used the photo of my cute dog Petite :-)

Sample Nami Wallet Asset Display

And done! For next steps, I suggest to try to modify my code since it really does not follow any good coding standards :P, explore bloxbean cardano lib, try to send the minted NFT to other addresses and check other APIs offered by Blockfrost. Start creating your NFTs now :-)

--

--