ChainSafe Storage — A step by step tutorial on how to store and retrieve NFT data

ChainSafe Storage — A step by step tutorial on how to store and retrieve NFT data

Authored by Sadiqur Rahman

ChainSafe Storage provides a ready-made, distributed storage solution for any use case. Our product uses IPFS for hot storage and Filecoin for archival storage and extra redundancy. This makes storage more robust and decentralized than traditional cloud alternatives and ensures that the user's data will always be available and resistant to censorship.

The storage UI presents an intuitive pinning service that allows users to easily pin and retrieve files on IPFS and Filecoin. Storage also delivers a set of exposed APIs that allow developers to integrate our software as the storage layer for their product with just a few clicks. In addition to the Storage APIs, the product provides S3 compatible API keys which means the creators of most traditional websites and applications can replace their storage layer with robust, distributed storage by changing a single field! Try Storage today!


Non-Fungible Tokens (NFTs) are unique, decentralized assets. One of the most compelling use cases for ChainSafe Storage is the storage of off-chain data associated with NFTs. With Storage, users can rest easy knowing that the associated off-chain data for their NFTs will always be just as available and decentralized as the asset itself!

In this article, we will go through how to store and retrieve the off-chain data for any NFT.

Prerequisites

  1. Sign-up on Storage

  2. Create an API key

  3. Create a bucket

Sign-up on Storage

Go to https://app.storage.chainsafe.io if you haven't created an account already.

Create an API key

  1. Go to settings and click on "Add API key"

  2. This will generate a key and a secret. Store your secret somewhere safe like you would back up a private key for a crypto wallet. It won't be displayed again in the API Key List of the settings page.

  3. All of the following steps we will use this secret as <API_SECRET>

Create a bucket

To store an object in Storage, you need to create a bucket. A bucket is a container for data.

When creating a bucket, provide the following two params as a request body:

  1. name - The name of the bucket. Must be unique in case you have multiple buckets

  2. type - For buckets in Storage, this value is always fps

Now, let's create a bucket with the following http request:

POST /api/v1/buckets HTTP/1.1
Host: https://api.chainsafe.io
Authorization: Bearer <API_SECRET>
Content-Type: application/json
{
 "name": "Test Bucket",
 "type": "fps", 
}

You will get a json response with ID. We will refer to this ID as BUCKET_ID in the following steps.

Storing NFT data

To store NFT data you must provide the following fields:

  1. BUCKET_ID as path param

  2. path : Path for where to upload file data. You can provide a non-existing directory path. In that case directories according to the path will be created as well.

  3. file : The file that you are uploading

Now, let's upload a file (example_nft) on the path /my_data

POST /api/v1/bucket/<BUCKET_ID>/upload HTTP/1.1
Host: https://api.chainsafe.io
Content-Type: multipart/form-data; boundary= - - WebKitFormBoundary7MA4YWxkTrZu0gW
Authorization: Bearer <API_SECRET>
 - - WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example_nft"
Content-Type: application/json
(data)
 - - WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="path"
/my_data
 - - WebKitFormBoundary7MA4YWxkTrZu0gW

Retrieving NFT data

NFT data stored on ChainSafe Storage can be accessed from the storage download API or from any public IPFS network from any peer that has the content.

Retrieving via download endpoint

Simply provide this path in the json request body

POST /api/v1/bucket/<BUCKET_ID>/download HTTP/1.1
Host: https://api.chainsafe.io
Authorization: Bearer <API_SECRET>
Content-Type: application/json
{
 "path": "/my_data/example_nft"
}

Retrieving via ipfs gateway

First, get the CID of the uploaded file. Then, perform the following request to get file details in json format:

POST /api/v1/bucket/<BUCKET_ID>/file HTTP/1.1
Host: https://api.chainsafe.io
Authorization: Bearer <API_SECRET>
Content-Type: application/json
{
 "path": "/my_data/example_nft"
}

Response:

{
"content":
{
"name": "file1.pdf",
"cid": "QmfPaBnVAR48UbcjF8crcX7TtJKiV8g3DJkTUsBB6pXb7e",
"size": 10121,
...
},
...
}

Copy the CID. Using the CID, data can be fetched directly from any public IPFS gateway. You can also use ChainSafe's IPFS gateway (https://ipfs.chainsafe.io)

The URL should be in this format:

https://{gateway URL}/ipfs/{content ID}/{optional path to resource}

If we want to get our uploaded file through Chainsafe's IPFS gateway, the URL will be:

https://ipfs.chainsafe.io/ipfs/QmfPaBnVAR48UbcjF8crcX7TtJKiV8g3DJkTUsBB6pXb7e

And there you have it. You now have everything you need to upload, store and download your NFT's off-chain data using ChainSafe Storage.

Note: There are some additional storage APIs that are not covered here. View the remaining Storage Docs (https://docs.storage.chainsafe.io) for more details of each storage API.

About ChainSafe

ChainSafe is a leader in blockchain development and infrastructure solutions for Web3. We're working toward a more decentralized future by building client implementations for Ethereum, Filecoin, Polkadot, and Mina. We're also maintaining the Web3.js library, working on a Gaming SDK that connects NFTs to Unity, building a distributed cloud storage application, and bootstrapping a multi-chain bridge. To learn more, click here.

Want to help build the foundations of Web3? Join us!

Have a question, comment, or suggestion? Drop into our Discord and join the conversation! We're also always looking for talented people. Check out our open positions and get in touch ➡️➡️ careers@chainsafe.io

Website |Youtube |Twitter |Linkedin |GitHub