Cadence Cookbook


Mint NFT

Introduction to minting an NFT on Flow

14 Oct 2022

Contributed by Flow Blockchain


This is for minting NFTS. It includes the minting resource that you can use in your smart contract, as well as the transaction to mint from the deployed account. It can be included in your smart contract along with the NFT Standard.

Smart Contract Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 pub resource NFTMinter { // mintNFT mints a new NFT with a new ID // and deposit it in the recipients collection using their collection reference pub fun mintNFT( recipient: &{NonFungibleToken.CollectionPublic}, name: String, description: String, thumbnail: String, power: String, will: String, determination: String ) { // create a new NFT var newNFT <- create NFT( id: NewExampleNFT.totalSupply, name: name, description: description, thumbnail: thumbnail, power: power, will: will, determination: determination ) // deposit it in the recipient's account using their reference recipient.deposit(token: <-newNFT) NewExampleNFT.totalSupply = NewExampleNFT.totalSupply + (1 as UInt64) } }

This resource should be implemented in your contract if you would like to mint NFTS.

It also requires other items to be implemented into the contract such as a 'NFT' resource that has metadata defined and conforms to the NFT Standard, as well as a recepient account which has the capability to deposit NFTS into their collection.

To see how to link a Collection Interface to an account, check out the linking collections example.

Transaction Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import NonFungibleToken from 0x03 import NewExampleNFT from 0x02 // This script uses the NFTMinter resource to mint a new NFT // It must be run with the account that has the minter resource // stored in /storage/NFTMinter transaction{ // local variable for storing the minter reference let minter: &NewExampleNFT.NFTMinter prepare(signer: AuthAccount) { // borrow a reference to the NFTMinter resource in storage self.minter = signer.borrow<&NewExampleNFT.NFTMinter>(from: NewExampleNFT.MinterStoragePath) ?? panic("Could not borrow a reference to the NFT minter") } execute { // Borrow the recipient's public NFT collection reference let receiver = getAccount(0x02) .getCapability(NewExampleNFT.CollectionPublicPath) .borrow<&{NonFungibleToken.CollectionPublic}>() ?? panic("Could not get receiver reference to the NFT Collection") // Mint the NFT and deposit it to the recipient's collection self.minter.mintNFT( recipient: receiver, name: "Second NFT", description: "The Best NFT", thumbnail: "NFT: Thumbnail", power: "The best", will: "The strongest", determination: "unbeatable" ) log("Minted an NFT") } }

This transaction is used to mint an NFT from the account that has the minting resource saved to it. You are able to give other users minting rights, check out the Admin Minting example for that.

In this case you are defining who is the person that will be receiving the NFT and checking if they actually have the capability to store the NFT in their collection before moving forward. As well as borrowing the Minting resource to mint new NFTS that should be in the account that has the minter saved.

At the end you are executing the transaction and minting the NFT.

ProgressNFT Fundamentals

Up Next: Collection for Holding NFTs


Related Recipes

14 Oct 2022
Collection for Holding NFTs
14 Oct 2022
NFT with Metadata
01 Apr 2022
Metadata Views