Create NFTs from the backend on Solana
Creating NFTs from the backend on Solana using SHYFT APIs
We have already seen how we can create NFTs from the front end using SHYFT APIs efficiently. Sometimes, we may have a different use-case scenario where we have all the data available in the backend and we have to create the NFT directly from the backend. This tutorial shows one such use-case scenario where we create NFTs directly from the backend using SHYFT APIs.
Pre-requisites
Authentication: Getting your Shyft API key
x-api-key
is an authentication parameter, which gives you access to SHYFT APIs. You can get your own API Key from the SHYFT website. Just signup with your email id here and you can get it for free.
NPM Packages Required
path — This package is used to read the image from a specified path.
axios — This package is used to make the API call, you can also use any other method or package as per your requirement.
The API call
For creating NFTs from the backend. we will use the following API :
This API accepts the following details of the NFT that is to be created in multipart/form-data
format:
network
: Specifies the Solana network instance, which can be devnet, testnet or mainnet-beta.name
: Name of the NFT to be created.symbol
: Symbol of the NFT to be created.description
: A short description about the NFT that we are creating.attributes
: Accepts the NFT attributes according to their trait-type and value. Check below sample for the format.external_url
: any external URL associated with the NFT.max_supply
: Number of editions of the currently created NFT that can be minted. If set to0
, creates one-of-a-kind NFTs.royalty
: Specifies the creator royalty in percentage, a value between 0 - 100.nft_receiver
: Specifies the wallet to which the NFT will be minted, if left blank, will be minted to the wallet whoseprivate_key
has been provided.service_charge
: Specifies an amount in SOL, which can be charged while creating the NFTsprivate_key
: Private Key of the user who is attempting to create the NFT.
For more details about our APIs, check out SHYFT’s API Documentation or you can also check out our postman documentation here.
Assuming we have all the data available in the backend, let’s create a new FormData object
and append all the data required to create the NFT. (we have used static data for demo purposes)
Sending Image Data
Note that, we have used the createReadStream
function, with the image path to open and read the data in the file/stream. the function path.resolve()
has been used to resolve the relative path provided for the image. Alternatively, for using absolute paths such as 'D:/index.png’, we will have to use the path.join()
function.
We can also get the image data in a readable stream format from an external URI, but for that, we would require a few extra steps. First, we will have to fetch the image data in a readable stream format, somewhat like this
Once this request has been executed successfully, we go on and append the imgData.data
to the FormData object, so that it can be passed on as a parameter for making the create NFT API call.
Once done, now we are ready to make the API call. We have used the axios
package to make the API call, but you can use any other method as per your requirement. Also, note that we have to set the Content_type
to multipart/form-data
in the header, and we have to pass the x-api-key
obtained from the SHYFT website.
Here is the API call:
Once successfully executed, it should return a response somewhat similar to this. The mint
returned in the response is the NFT address of the NFT just created.
That’s pretty much everything about this tutorial. If you follow this tutorial step by step, you will be able to create a new NFT successfully from the backend.
If you enjoyed this tutorial, please feel free to check out our API tutorials on Building your first NFT dApp or Building your own NFT marketplace.
Resources
Join our Discord
Hope you have a great time building dApps with SHYFT APIs. Happy Hacking!! 😇
Last updated