Creating custom NFT collections with Candy Machine

Creating custom collections becomes very easy with the help of candy machine. Below is an example to create a candy machine in order to mint a collection of 50 identitcal NFTs and also different ways of minting them.

Creating candy machine

A candy machine is an object which stores the configuration for all the NFTs which will be minted for a collection. To create a candy machine we can use https://docs.shyft.to/start-hacking/candy-machine#create-candy-machine

We will be creating a candy machine which will be able to mint 50 identical NFTs and which can be minted with either SOL payment or SPL Token payment. The only prerequisite for creating a candy machine you will need a metaplex collection NFT which is owned by the candy machine creator. In this case we already have one hence we will be using that. The code sample below can be used to create this Candy machine configuration.

var myHeaders = new Headers();
myHeaders.append("x-api-key", "<api-key>");

var body = {
    "network": "devnet",
    "wallet": "7iviPE2HZ15QCvHHCm8QzYdXKt9qReeT5P4aqSRAp7oY",
    "symbol": "IDT",
    "max_supply": 0,
    "royalty": 0,
    "collection": "7KnYuwbcG3EDLBnpUTovGN1WjpB1WvvyNuMgjRezG33s",
    "items_available": 50,
    "bulk_item_settings": {
        "name": "Identical #$ID+1$",
        "uri": "https: //some.uri/abc/path"
    },
    "groups": [
        {
            "label": "token",
            "guards": {
                "tokenPayment": {
                    "amount": 1000000,
                    "destination": "AUE3Z8m8HDg8fLaho1XcQzxmmM9bNwKkdLqqkWAdR5Et",
                    "mint": "4831mWZ5kJxer8VbLVfARG4jz7zgXKdwkDrGp5Lf4xbY"
                }
            }
        },
        {
            "label": "sol",
            "guards": {
                "solPayment": {
                    "amount": 0.5,
                    "destination": "7iviPE2HZ15QCvHHCm8QzYdXKt9qReeT5P4aqSRAp7oY"
                },
            }
        }
    ]
};

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: JSON.stringify(body),
  redirect: 'follow'
};

fetch("{{url}}/candy_machine/create", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Here we can we see that we have created a candy machine with total 50 items. In order to not fill all the items as all the NFTs are identical in our case we have added the bulk_item_settings config.

Here the URI would be same for all NFTs which will point to the metadata json. In the name field we can see the special variable $ID+1$ . This is a special variable which will allow us to add dynamic number to our NFT mints. With this config the first NFT would be named Identical #1 the second would be Identical #2 and so on.

The groups config which is seen will help us for minting. In the groups config we have specified 2 groups. In the first groups named token whenever the user will mint a NFT from that group he will be charged 1 token (considering that the above token has 6 decimals) which will be sent to the destination. Here it is important to note that the destination address is should be the associated token account of the treasury account for that specific token. mint is the address of the required SPL token.

The second group is sol. Using this group while minting will transfer 0.5 SOL to the destination account address.

Running this api would return an encoded transaction which should be signed by the wallet address specified while calling the API.

More details on create API can be found here: https://docs.shyft.to/start-hacking/candy-machine#create-candy-machine

Minting token from candy machine

For minting token from the above candy machine we would need to know the candy machine address, the collection NFT authority address and the address in which the NFT will be minted to.

var myHeaders = new Headers();
myHeaders.append("x-api-key", "<api-key>");

var body = {
    "network": "devnet",
    "wallet": "7iviPE2HZ15QCvHHCm8QzYdXKt9qReeT5P4aqSRAp7oY",
    "authority": "7iviPE2HZ15QCvHHCm8QzYdXKt9qReeT5P4aqSRAp7oY",
    "candy_machine": "EPqDyRiGSiNVbeK3NQtSeXfpenfXMXniTBD7F3jfFkNj",
    "mint_group": "token"
}

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: JSON.stringify(body),
  redirect: 'follow'
};

fetch("{{url}}/candy_machine/mint", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

While creating candy machine we specified the groups which will be used for different forms of payment. In the above snippets we can see, while using the mint API, if we specify the mint_group as token then the NFT would be minted as per the guards specified in token group while creating the candy machine. The using the above snippet would mint an NFT and transfer 1 token to destination address (AUE3Z8m8HDg8fLaho1XcQzxmmM9bNwKkdLqqkWAdR5Et)specified while creating Candy machine.

For SOL payment the mint_group needs to set as sol. This will mint a NFT and transfer 0.5 SOL to destination address of 7iviPE2HZ15QCvHHCm8QzYdXKt9qReeT5P4aqSRAp7oY

The above examples would return an encoded transaction which has to be signed by the wallet address specified in the mint API body.

More details on minting API can be found here: https://docs.shyft.to/start-hacking/candy-machine#mint-nfts-from-candy-machine

Conclusion

With this our NFT collection setup is complete. For every new mint the user has to call the API with new wallet address.

Last updated