Storing Data on IPFS with Node-Red

This guide will provide detailed instructions on how to install and use Node-Red to write data to IPFS( InterPlanetary File System).
IPFS is a distributed system for storing and accessing data files. The data is addressed based on a cryptographic hash of its content and not by its location. It is possible to run an IPFS node on the pi however for this tutorial I will use an IPFS storage service called Pinata. Pinata offers a free tier with 1GB of storage and up to 2 levels of data replication.

This guide is written for installation on Raspberry Pi 4 running Ubuntu 21.10 however setup would be nearly identical on a VPS. This guide assumes that you have some familiarity in using a Raspberry Pi and accessing it via SSH. The Pi will be run in headless operation(no monitor, keyboard, mouse).

Table of Contents

    1. Install Ubuntu Server 21.10

    See this guide for a secure basic installation of Ubuntu 21.10.

    2. Install Node-Red (Tested with Version 2.1.3)

    See this guide for installation of Node-Red

    3. Install Node Dependencies

    Pinata has a NodeJS SDK for managing files with their service. There are no native Node-Red “nodes” available for this SDK however it is now quite easy to use external libraries in a Node-Red function node.
    When sending files to Pinata we also need to use a NodeJS file stream. We will use the fs NodeJS library.

    1. Change to Node-Red directory in user account(not root)
      cd ~/.node-red
    2. Install fs NodeJS module
      npm install fs
    3. Install Pinata SDK NodeJS module
      npm install @pinata/sdk
    4. Restart Node-Red
      node-red-restart

    4. Setup Pinata IPFS Account

    1. Create Free Account on https://www.pinata.cloud/pricing
    2. Create new API Key.
      1. Go to Account->API Keys->+ New Key
      2. Enter a Key Name and enable Admin Keys
      3. Create Key and the popup window will show API Key and API Secret.
      4. Record these 2 values in safe place as the API Secret cannot be accessed again.

    5. Import Example Flow into Node-Red

    1. Download Node-Red Example Flow
      https://gateway.pinata.cloud/ipfs/QmUrfNpH1j9EqiBiHbNEFeAqqBH59SkW3Mx8yDJ7X1zVXA/PinataTestFlow.json
    2. Open Node-Red Editor
    3. Import the Example Flow. The flow should look like this:
    Node-Red IPFS example Flow

    6. Configure Example Flow

    1. In the pinata.testAuthentication() function node replace the API Key and API Secret placeholders with your values.
      const pinata = pinataSDK('API Key','API Secret')
    2. In the pinata.pinFileToIPFS() function node replace the API Key and API Secret placeholders with your values.
      const pinata = pinataSDK('API Key','API Secret')
    3. Edit the “Push to Create Test File” Inject Node and change the text(This is a test file) to whatever you want your content to be. If you keep the text the same as the example then the IPFS network should detect that the content is a duplicate when you publish.
    4. Deploy flow to save changes.
    Push to Create Test File

    7. Run Example

    1. Open the debug panel on the right side of Node-Red
    2. Press the “Push to Test” inject button. This will test the authentication to your Pinata account. You should get the following success message if your API keys are all correct.
    3. Press the “Push to Create Test File” inject button. This will create a local file in home folder of raspberry pi. You should get the following message if it succeeds.
    4. Press the “Push to Write IPFS” inject button. This will read the file previously created and write to IPFS. If it succeeds you should get a similar message.
    5. Use the following gateway URL format to access your files directory
      https://ipfs.io/ipfs/your file's hash
      or
      https://gateway.pinata.cloud/ipfs/your file's hash
    6. Use the following gateway URL format to access your file directly
      https://ipfs.io/ipfs/your file's hash/testfile.txt
      or
      https://gateway.pinata.cloud/ipfs/your file's hash/testfile.txt