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 :

POST https://api.shyft.to/sol/v1/nft/create

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 to 0, 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 whose private_key has been provided.

  • service_charge: Specifies an amount in SOL, which can be charged while creating the NFTs

  • private_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)

const data = new FormData();
  
data.append('network', 'devnet');
data.append('name', 'GirlDrags');
data.append('symbol', 'GDG');
data.append('description', 'hair on fire problem solving');
data.append('attributes', '[{"trait_type": "speed", "value": 100},\\n{"trait_type": "aggression", "value": "crazy"},\\n{"trait_type": "energy", "value": "very high"}]');
data.append('external_url', 'https://shyft.to/');
data.append('max_supply', '1');
data.append('royalty', '10');
data.append('file', fs.createReadStream(path.resolve(__dirname, './images/index.png')));
data.append('nft_receiver', '5KW2twHzRsAaiLeEx4zYNV35CV2hRrZGw7NYbwMfL4a2');
data.append('service_charge', '{ "receiver": "2fmz8SuNVyxEP6QwKQs6LNaT2ATszySPEJdhUDesxktc", "amount": 0.1 }');
data.append('private_key', YOUR-PRIVATE-KEY);

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.

data.append('file', fs.createReadStream(path.join('D:/index.png')));

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

const imgData = await axios.get('https://www.arweave.net/GZtct3fpYUlB9ZT92SMkVEp7TUvLlFV-kke4f5I5D9E?ext=jpg', { responseType: 'stream' });

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.

data.append('file', imgData.data, 'test.jpg');

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:

const response = await axios.post('https://api.shyft.to/sol/v1/nft/create', data, { headers: {
    'Content-Type': 'multipart/form-data',
    'x-api-key': YOUR_API_KEY,
  } });
console.log('data', response.data); // displaying the response

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.

data {
  success: true,
  message: 'NFT created successfully',
  result: {
    txId: '4gs1ffsGHtvxLKV4D6vmTUfjNvisPS82iJjB8DNyeKmL9cUBX8VKvkFUX3nXtUUUNcA5TQ8vZzu5GaoT7CeEwWpp',
    mint: '7LjUNQ9gDydqy5kbVppFomNSRnRnE8cgf8zvFRHp39nz',
    metadata: '4W333HyXhLsX16rZYaHgUbfapv6dTB9KtFqpMSdcMsx9',
    edition: '9WbVRGgHRCXNTA8WfU31AussJxRSptW8Vyic2RaFafSh'
  }
}

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

SHYFT API Documentation

Shyft Website

Get API Key

GitHub

Join our Discord

Hope you have a great time building dApps with SHYFT APIs. Happy Hacking!! 😇

Last updated