A Custom API which returns random Turtle images along with some metadata through an IPFS Hash
The API is built using NodeJS
and ExpressJS
.
The API endpoint takes in 2 query parameters namely characterId
(ID of the Turtle in the smart contract) and score
(new high score that the user has achieved in the game).
The speed
of the new Turtle NFT generated will depend on the new high score achieved and is scaled accordingly inside the game. The speed is also randomized to a certain extent.
The name
of the new Turtle is being generated using the characterId.
The most interesting part of the API is the Random Turtle image generation part. The turtle is divided into 4 components such as Head, Legs, Shell and Tail. Everytime the API is called, the image-blend-randomize.js
file returns a PNG which is a randomized combination of image collection in each component. The components are stored in the folder components
. See the image below to understand it better.
The NFT metadata is a JSON
file which comprises of a name, description, image URL and some traits(speed of the Turtle in this case). This metadata is stored in Filecoin
and IPFS
using nft.storage
. It is a free decentralized off-chain storage of NFT data. After uploading the JSON to nft.storage
, an IPFS hash is returned which is returned by the API.
Chainlink Oracles
are used to fetch the data using API calls. The smart contracts are deployed to Polygon Mumbai
testnet and the suitable Oracle which is used to GET bytes32
data on-chain is Linkriver. Since bytes32
variable can only hold 32 characters of data, the first endpoint returns the first 31 characters of the IPFS hash. We had to optimize the API to create a second endpoint which contains the next 28 characters of the IPFS hash.
By this way, we can solve the problem of getting the IPFS hash from off-chain into on-chain using Chainlink by making 2 API calls which can further be used to set the Token URI for the NFT in the smart contract.
Finally, the API is hosted using Heroku which is a cloud platform. The base URL for the hosted API is https://images-blend.herokuapp.com/
- nft.storge - Free Storage for NFTs
- NodeJS - Node.js is an open-source, cross-platform, back-end JavaScript runtime environment
- ExpressJS - Express.js is a back end web application framework for Node.js
- Heroku - Heroku is a cloud platform which is used to host the API
These instructions will get you a copy of the project up and running on your local machine for testing purposes
Please make sure you've already installed NPM package.
A step by step guide to locally run the API
- First, get a free API key from nft.storage
- Clone this repository:
git clone https://github.com/DineshBS44/images-blend
- Install all the required packages using the following command
npm install
- Create a
.env
file and store the following content in it and include API key ofnft.storage
:
NFT_STORAGE_API_KEY="<nft.storage API Key>"
- To test the API endpoint locally, use the following command:
node server.js
-
Now open http://localhost:4000/?score=400&characterId=2 in your browser to view the first 32 characters of the IPFS Hash of a Randomly generated Turtle NFT's metadata. The
characterId
andscore
are passed as query parameters to the API. To view the next 28 characters of the IPFS Hash, call http://localhost:4000/second which is the 2nd endpoint. -
To use the same API which is hosted using Heroku, open the link https://images-blend.herokuapp.com/ which is the base URL for the API.
- nft.storage - To store NFT's metadata to
Filecoin
andIPFS
- images - Used to blend different image components to return a new PNG file
- express - backend web framework for Node JS
- dotenv - To manage data in
.env
files - node-fetch - A light weight module that brings Fetch API to Node JS
- Dinesh B S (@DineshBS44)
- Suryashankar Das (@iamsdas)
Licensed under MIT License : https://opensource.org/licenses/MIT