DEV Community

CryptoLoom
CryptoLoom

Posted on • Originally published at cryptoloom.xyz on

Pin It to Win It: Exploring IPFS Pinning Services with JavaScript Code Showcases

The InterPlanetary File System (IPFS) is an innovative peer-to-peer protocol designed for the distributed storage and retrieval of data. This revolutionary system creates content-addressable, censorship-resistant, and highly resilient storage on the internet.

However, persistence is not guaranteed in the IPFS network. Due to its decentralized nature, content is stored on individual nodes, and each node decides independently to store or delete it at any time.

This is where IPFS pinning services come in, providing content-addressed and always-online storage for your data. By “pinning” your data, you ensure its persistence in the IPFS network, making it immune to deletions and availability issues.

In this article, we’ll cover the basics of IPFS pinning services, dive into their benefits, and demonstrate how to use them, with code examples in Javascript. We will also explore three popular pinning services – Pinata, Infura, and Temporal – which can be easily integrated into your projects.

What is IPFS Pinning?

In IPFS, data is identified through a cryptographically-secure hash (CID), generated from the content itself. When new content is added to an IPFS node, it computes a unique CID and shares it across the network.

To keep content alive and increase its resilience, users can “pin” data to the IPFS network. Pinning instructs the IPFS node to preserve and re-share the content, making it accessible over the long run.

Pinning can be performed locally, on the user’s own IPFS node, or remotely, using dedicated external “pinning services” that provide always-online storage for IPFS content. These services, such as Pinata, Infura, and Temporal, enable users to offload storage and increase content availability without dealing with the complexities of hosting and managing their IPFS nodes.

Why Do We Need IPFS Pinning Services?

Pinning services are essential for ensuring content availability and persistence in IPFS. Here are some reasons why you should consider using a pinning service:

  1. Increased Content Resilience : Pinning content on multiple nodes increases its resilience and availability in the face of potential failures or outages.
  2. Offload Storage Responsibilities : Pinning services help you offload the responsibility of hosting and managing your IPFS node, allowing you to focus on building your application or service.
  3. Censorship-Resistance : Using a pinning service helps maintain your data’s availability, even if local IPFS nodes are shut down or censored.
  4. Content Redundancy : Pinning services provide redundant storage for your data, spreading it across several nodes and networks, protecting against data loss.

IPFS Pinning Services: Code Examples in Javascript

We’ll now look at three popular IPFS pinning services and demonstrate how to use each one with code examples in Javascript.

1. Pinata

Pinata is a dedicated IPFS pinning service that provides a simple API for storing and managing IPFS content. To get started, sign up for a free account at Pinata and obtain your API key.

Next, let’s install the “ipfs-http-client” and “axios” libraries by running the following command:

npm install ipfs-http-client axios

Enter fullscreen mode Exit fullscreen mode

Create a new Javascript file and add the following code to add a file to IPFS using Pinata:

const IPFS = require('ipfs-http-client');
const ipfs = IPFS.create();
const axios = require('axios');

async function main() {
  // Add file to local IPFS node
  const content = 'Hello, World!';
  const { cid } = await ipfs.add(content);

  // Pin file to Pinata
  const pinataApiKey = 'your-pinata-api-key';
  const pinataSecretApiKey = 'your-pinata-secret-api-key';
  const url = 'https://api.pinata.cloud/pinning/pinByHash';

  await axios.post(
    url,
    { hashToPin: cid.toString() },
    {
      headers: {
        pinata_api_key: pinataApiKey,
        pinata_secret_api_key: pinataSecretApiKey,
      },
    }
  );

  console.log(`Content added to IPFS and pinned with Pinata: ${cid}`);
}

main();

Enter fullscreen mode Exit fullscreen mode

Replace your-pinata-api-key and your-pinata-secret-api-key with your Pinata API credentials. When you execute the script, your content should be added to your local IPFS node and pinned to Pinata.

2. Infura

Infura is a popular service providing both Ethereum and IPFS infrastructure as a service. To get started, sign up for a free account at Infura, and create a new IPFS project.

Add the “ipfs-http-client” library by running the following command:

npm install ipfs-http-client

Enter fullscreen mode Exit fullscreen mode

Create a new Javascript file and add the following code to utilize Infura as your IPFS pinning service:

const IPFS = require('ipfs-http-client');

async function main() {
  // Set up Infura IPFS API
  const infuraProjectId = 'your-infura-project-id';
  const infuraIpfs = IPFS.create({
    host: 'ipfs.infura.io',
    port: 5001,
    protocol: 'https',
    headers: {
      authorization: `Bearer ${infuraProjectId}`,
    },
  });

  // Add file to Infura IPFS
  const content = 'Hello, World!';
  const { cid } = await infuraIpfs.add(content);

  console.log(`Content added and pinned to IPFS using Infura: ${cid}`);
}

main();

Enter fullscreen mode Exit fullscreen mode

Replace your-infura-project-id with your Infura project ID. When you execute the script, your content should be added and pinned to IPFS using Infura.

3. Temporal

Temporal is another powerful IPFS pinning service with a wide range of features. To get started, sign up for a free account at Temporal, and obtain your API key.

Next, install the “ipfs-http-client” and “axios” libraries by running the following command:

npm install ipfs-http-client axios

Enter fullscreen mode Exit fullscreen mode

Create a new Javascript file and add the following code to use Temporal as your IPFS pinning service:

const IPFS = require('ipfs-http-client');
const ipfs = IPFS.create();
const axios = require('axios');
const FormData = require('form-data');

async function main() {
  // Add file to local IPFS node
  const content = 'Hello, World!';
  const { cid } = await ipfs.add(content);

  // Pin file to Temporal
  const apiKey = 'your-temporal-api-key';
  const url = 'https://api.temporal.cloud/ipfs/public/pin/add/' + cid.toString();

  const formData = new FormData();
  formData.append('file', content);

  await axios.post(url, formData, {
    headers: {
      ...formData.getHeaders(),
      'Authorization': `Bearer ${apiKey}`,
    },
  });

  console.log(`Content added to IPFS and pinned with Temporal: ${cid}`);
}

main();

Enter fullscreen mode Exit fullscreen mode

Replace your-temporal-api-key with your Temporal API key. When you execute the script, your content should be added to your local IPFS node and pinned to Temporal.

Conclusion

IPFS pinning services are essential tools for guaranteeing the availability and persistence of your data on the IPFS network. Whether you choose Pinata, Infura, or Temporal, these services will help you offload storage and focus on building your applications.

Remember to choose a pinning service that fits your needs, taking into consideration factors like cost, reliability, and support.

References

The post Pin It to Win It: Exploring IPFS Pinning Services with JavaScript Code Showcases appeared first on CryptoLoom.

Top comments (0)