How to create NFTs in TypeScript with Metaplex Metadata program and Irys permanent storage service.
metaplex-foundation/js
npm module, also known as the
Metaplex JavaScript SDK.mpl-token-metadata
npm module.
null
(to ensure that the
supply never changes).json
file.
metaplex-foundation/js
npm module (also known as the Metaplex JavaScript SDK)
to prepare assets, create NFTs, update NFTs, and associate an NFT with a broader
collection. For more information on metaplex-foundation/js
see the
Metaplex JavaScript SDK README and
the
Metaplex JS SDK Examples.
Metaplex
instance serves as the entry point for accessing the Metaplex SDK.
This instance accepts a connection used to communicate with the cluster.
Additionally, developers can customize the SDK’s interactions by specifying an
“Identity Driver” and a “Storage Driver”.
The Identity Driver is a keypair that can be used to sign transactions, which is
a requirement when creating an NFT. The Storage Driver is used to specify the
storage service you want to use for uploading assets. The irysStorage
driver
is the default option, and it uploads assets to Irys, a permanent and
decentralized storage service.
Below is an example of how you can set up the Metaplex
instance for devnet.
toMetaplexFile
function, and
finally uploading it to the designated Storage Driver.
The Metaplex SDK supports the creation of a new Metaplex file from either files
present on your local computer or those uploaded by a user through a browser.
You can do the former using readFileSync()
to read the image file, then
convert it into a Metaplex file using toMetaplexFile()
. Finally, use your
Metaplex
instance to call storage().upload(file)
to upload the file. The
function’s return value will be the URI where the image was stored.
nfts().uploadMetadata()
function. This will return a URI where the JSON
metadata is stored.
Remember, the offchain portion of the metadata includes things like the image
URI as well as additional information like the name and description of the NFT.
While you can technically include anything you’d like in this JSON object, in
most cases, you should follow the
NFT standard
to ensure compatibility with wallets, programs, and applications.
To create the metadata, use the uploadMetadata
method provided by the SDK.
This method accepts a metadata object and returns a URI that points to the
uploaded metadata.
create()
method allows you to create a new NFT
with minimal configuration. This method will create the mint account, token
account, metadata account, and master edition account for you. The data provided
to this method will represent the onchain portion of the NFT metadata. You can
explore the SDK to see all the other input optionally supplied to this method.
isMutable
property to true, allowing for
updates to be made to the NFT’s metadata. However, you can choose to set
isMutable
to false, making the NFT’s metadata immutable.
isMutable
as true, you may update your NFT’s metadata. The
SDK’s update
method allows you to update both the onchain and offchain
portions of the NFT’s metadata. To update the offchain metadata, you’ll need to
repeat the steps of uploading a new image and metadata URI (as outlined in the
previous steps), then provide the new metadata URI to this method. This will
change the URI that the onchain metadata points to, effectively updating the
offchain metadata as well.
update
will stay the
same, by design.
collection
field with a key
that points to the
Certified Collection
NFT.
Simply put, NFTs that are part of a collection are associated with another NFT
that represents the collection itself.
Certified collections are important because they mean the collection owner has
verified that each NFT actually belongs to the collection!
To add an NFT to a collection, first, the Collection NFT has to be created. The
process is the same as before, except you’ll include one additional field on our
NFT Metadata: isCollection
. This field tells the token program that this NFT
is a Collection NFT.
collection
field in our new Nft.
collection
field like so:
verified
field above to true, but it’s incredibly important. This is what lets
consuming programs and apps know that your NFT is in fact part of the
collection. You can do this using the verifyCollection
function:
create-metaplex-collection.ts
, and add our imports:
create-metaplex-nft.ts
. The setup for this will look
the same as the previous file, with slightly different imports:
verified
field in the onchain metadata is set to true
, so consuming
programs and apps can know for sure that the NFT in fact belongs to the
collection:
npx esrun create-metaplex-nft.ts
. If all goes well, you will see the
following:
update-metaplex-nft.ts
. The imports will be simila
to our previous files:
npx esrun update-metaplex-nft.ts
. You should see something like: