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).
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.
- Change to Node-Red directory in user account(not root)
cd ~/.node-red
- Install fs NodeJS module
npm install fs
- Install Pinata SDK NodeJS module
npm install @pinata/sdk
- Restart Node-Red
node-red-restart
4. Setup Pinata IPFS Account
- Create Free Account on https://www.pinata.cloud/pricing
- Create new API Key.
- Go to Account->API Keys->+ New Key
- Enter a Key Name and enable Admin Keys
- Create Key and the popup window will show API Key and API Secret.
- Record these 2 values in safe place as the API Secret cannot be accessed again.
5. Import Example Flow into Node-Red
- Download Node-Red Example Flow
https://gateway.pinata.cloud/ipfs/QmUrfNpH1j9EqiBiHbNEFeAqqBH59SkW3Mx8yDJ7X1zVXA/PinataTestFlow.json - Open Node-Red Editor
- Import the Example Flow. The flow should look like this:
6. Configure Example Flow
- In the pinata.testAuthentication() function node replace the API Key and API Secret placeholders with your values.
const pinata = pinataSDK('API Key','API Secret')
- In the pinata.pinFileToIPFS() function node replace the API Key and API Secret placeholders with your values.
const pinata = pinataSDK('API Key','API Secret')
- 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.
- Deploy flow to save changes.
7. Run Example
- Open the debug panel on the right side of Node-Red
- 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.
- 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.
- 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.
- Use the following gateway URL format to access your files directory
https://ipfs.io/ipfs/your file's hash
orhttps://gateway.pinata.cloud/ipfs/your file's hash
- Use the following gateway URL format to access your file directly
https://ipfs.io/ipfs/your file's hash/testfile.txt
orhttps://gateway.pinata.cloud/ipfs/your file's hash/testfile.txt