Clarity makes creating NFTs incredibly easy. With built-in functions for creating and working with the token, you can have an NFT created in less than 10 minutes of work.
Let's see how.
:::tip For a more in-depth discussion of NFTs in Clarity and how to create them, check out the NFTs chapter in the Clarity book. :::
Trait
The first thing we need when we create an NFT is a trait. A trait is an interface that allows us to create an NFT with a defined set of functions. Its primary purpose is to ensure that NFTs are composable and different tools know how to interact with them.
By implementing a trait that the community agrees on, all protocols and products know how they can interact with an NFT.
(define-trait nft-trait (;; Last token ID, limited to uint range (get-last-token-id () (responseuintuint));; URI for metadata associated with the token (get-token-uri (uint) (response (optional (string-ascii256)) uint));; Owner of a given token identifier (get-owner (uint) (response (optionalprincipal) uint));; Transfer from the sender to a new principal (transfer (uintprincipalprincipal) (responsebooluint)) ))
All we are doing here is defining the function signatures for functions we'll need to implement in our Clarity contract, which we can see a simple version of below.
Clarity Code
This is the Clarity code we need in order to create an NFT, with one additional function, mint that allows us to actually create a new NFT. This mint function is not needed to adhere to the trait.