With Shyft's API you can create applications to mint NFTs.
In this tutorial we will build a simple dapp that allows you to create an NFT on Solana blockchain, using Shyft APIs. At the end of this tutorial your newly minted NFT will be available in your own wallet.
Pre-requisites
To get started first thing first, let's install a browser extension of Phantom Wallet from below links:
Once installed, open Phantom in your browser, and follow the on-screen steps to create a new wallet. Refer to this guide, if you need help. Now, you have a public-private key pair that serves as an identity on blockchain.
Install nodeJs and npxx (node package executor)
Steps for mac:
//type the following commands on your terminal:
$ brew update
$ brew install node
//once completed, check if installed properly:
$ node -v
v18.0.0 // if installed properly, should output the node installed version
// install npx:
$ npm i -g npx
Setup
Alright, now we are all set to create our first NFT minting dapp. We will use ReactJs to build a simple front end to allow you to interactively create a NFT. We will use VS Code to walk through the tutorial.
Authentication: Get 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.
Setup react project
$ npx create-react-app my-first-nft-dapp
This creates the boilerplate code for your dapp, and file structure looks like below:
Let's dive into code
Let's create a new form for accepting all the details(parameters) we will require to create a new NFT. We have created a new component for the form, but you can also put this code directly under App.js
This is a simple form with takes in the following parameters:
Choose an image file that you want to mint as an NFT
Select the network (testnet, devnet, mainnet-beta)
Your phantom wallet's public key
NFT Name
NFT symbol
NFT description
External_Url, this can be link to any website that you want to put in. This will be visible in the phantom wallet account for navigation.
Number of editions of one particular NFT that can be minted. can be set to zero, if you want to create an one-of-a-kind NFT.
Royalty percentage of the NFT creator. Can be any value between 0 - 100.
Attributes associated to the NFT. It's a json array string, don't forget to Stringify as done in below code snippet.
let attrib = [{"trait_type":"speed","value":100},{"trait_type":"aggression","value":"crazy"},{"trait_type":"energy","value":"very high"}];let paramsToPass =JSON.stringify(attrib);//Here we have created 3 attributes for our NFT, namely://1. speed = 100//2. aggression = "crazy"//3. energy = "very high"
Making the API call
Once we have the data, we are ready to make the API call. For this tutorial, we have used the axios package to make the API call, but you can use any other methods including JavaScript's very own fetch.
"dependencies": { ..."axios":"^0.27.2" .... },
axios will allow your react application to make http request to Shyft's server and create your NFT.
Now, let's create a function for making the API call using the data we have collected using the form we created in this tutorial.
constmintNow= (e) => {e.preventDefault();setStatus("Loading");let formData =newFormData();formData.append("network", network);formData.append("wallet", publicKey);formData.append("name", name);formData.append("symbol", symbol);formData.append("description", desc);formData.append("attributes",JSON.stringify(attr));formData.append("external_url", extUrl);formData.append("max_supply", maxSup);formData.append("royalty", roy);formData.append("file", file);axios({// Endpoint to send files url:"https://api.shyft.to/sol/v1/nft/create_detach", method:"POST", headers: {"Content-Type":"multipart/form-data","x-api-key":"Your-api-key", Accept:"*/*","Access-Control-Allow-Origin":"*", },// Attaching the form data data: formData,})// Handle the response from backend here.then(async (res) => {console.log(res);if(res.data.success ===true) {setStatus("success: Transaction Created. Signing Transactions. Please Wait.");consttransaction=res.data.result.encoded_transaction; //encoded transactionsetSaveMinted(res.data.result.mint);constret_result=awaitsignAndConfirmTransactionFe(network,transaction,callback); //signing the encoded transactionconsole.log(ret_result);setDispResp(res.data); } })// Catch errors if any.catch((err) => {console.warn(err);setStatus("success: false"); });}
Now you are all set to mint your first NFT. Please note that this API does not require the private key, instead it uses the public keyto sign the transaction required for the create operation.
Head over to the terminal and start the react app, by running:
npm run start
Your basic app will look like this:
All you have to do is enter the information in the fields and hit submit button.
Once we receive the encoded_transaction in the response, the last and final step is to sign the transaction. We can sign encoded_transactionin two ways, either using the wallet from the frontend, or by using the wallet's private key from the backend.
You can read more about signing transactions on Solana here, or you can use our online dev tool for signing this encoded transaction available in the below link.
Paste the mint value (the on-chain address of your token) returned in the response in the search bar, you should get the details of the created NFT.
Details of the transaction can be checked by pasting txnId from the response in the previous search bar on Solana explorer.
By now, NFT has already been received in your wallet!!
Go to Your Collectibles tab of your phantom wallet, and you should see the newly created NFT.
Ending Notes
I hope you enjoyed this tutorial and are excited to dive deep into web3 awesomeness. Stay tuned for more such tutorials.
If you want the complete code for this project with good looking UI, please checkout/fork and play with the code at this Repository.:
As soon as you hit submit the request is sent to the Shyft server, and you have successfully created a new NFT transaction. Now, all you need to do is sign this transaction using your wallet and voila! Your newly created NFT will be added to your wallet.
Once successfully signed, your NFT will be created and added to your wallet.
Thank you for your time. We hope you have a great time building dApps with SHYFT APIs. Happy hacking.