Claude Skills Guide

Claude Code for IPFS Decentralized Storage Workflow

Decentralized storage is becoming increasingly important for developers who need persistent, censorship-resistant data storage solutions. IPFS (InterPlanetary File System) offers a peer-to-peer approach to file storage that ensures content availability through content addressing rather than location-based URLs. Combined with Claude Code’s powerful CLI capabilities, you can build robust decentralized storage workflows that automate file uploads, manage pinning services, and handle content retrieval smoothly.

This guide walks you through building practical IPFS workflows using Claude Code, with real code examples you can adapt for your projects.

Understanding IPFS and Content Addressing

Before diving into workflows, it’s essential to understand how IPFS differs from traditional file storage. In a traditional system, files are accessed by their location (e.g., https://example.com/file.txt). In IPFS, files are accessed by their content hash, known as a CID (Content Identifier). This means the same file uploaded by different people will produce the same CID—ensuring verifiability and deduplication.

When you add a file to IPFS, the network generates a unique CID based on the file’s content. Any node that has the file can serve it, creating redundancy without central servers. However, for persistent storage, you’ll typically need to “pin” content to ensure it remains available.

Setting Up Your IPFS Environment

Before building workflows with Claude Code, ensure you have IPFS installed. You can use a local IPFS node or connect to a pinning service API. For production workflows, services like Pinata, Infura, or nft.storage provide reliable APIs.

Here’s a basic setup script you can run with Claude Code:

#!/bin/bash
# Install IPFS if not present
if ! command -v ipfs &> /dev/null; then
    echo "Installing IPFS..."
    brew install ipfs
    ipfs init
fi

For cloud-based workflows, you’ll need API keys from your preferred pinning service. Store these securely using environment variables:

export PINATA_API_KEY="your-api-key"
export PINATA_SECRET_KEY="your-secret-key"

Building Your First Upload Workflow

The most fundamental IPFS workflow is uploading a file and retrieving its CID. Here’s a Claude Code script that handles file uploads to IPFS:

#!/usr/bin/env python3
import subprocess
import json
import os

def upload_to_ipfs(file_path):
    """Upload a file to local IPFS node and return the CID."""
    result = subprocess.run(
        ["ipfs", "add", "-Q", file_path],
        capture_output=True,
        text=True
    )
    
    if result.returncode == 0:
        cid = result.stdout.strip()
        print(f"Uploaded successfully! CID: {cid}")
        return cid
    else:
        raise Exception(f"Upload failed: {result.stderr}")

def pin_content(cid, pinata_api_key=None, pinata_secret=None):
    """Pin content to a remote service for persistence."""
    if not pinata_api_key:
        pinata_api_key = os.getenv("PINATA_API_KEY")
        pinata_secret = os.getenv("PINATA_SECRET_KEY")
    
    # Use curl to pin via Pinata API
    cmd = f"""curl -X POST "https://api.pinata.cloud/pinning/pinByHash" \
      -H "pinata_api_key: {pinata_api_key}" \
      -H "pinata_secret_api_key: {pinata_secret}" \
      -H "Content-Type: application/json" \
      -d '{{"hashToPin": "{cid}"}}' """
    
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    return json.loads(result.stdout)

# Example usage
if __name__ == "__main__":
    cid = upload_to_ipfs("documents/important-file.json")
    pin_result = pin_content(cid)
    print(f"Pinned! IPFS Gateway URL: https://ipfs.io/ipfs/{cid}")

This script handles both local IPFS uploading and remote pinning. Claude Code can execute this directly, making it easy to integrate into larger automation pipelines.

Automating Directory Uploads

When working with complex projects, you’ll often need to upload entire directories while preserving their structure. IPFS handles this through directory formatting:

import subprocess
import json

def upload_directory(dir_path):
    """Upload a directory to IPFS with recursive structure."""
    result = subprocess.run(
        ["ipfs", "add", "-r", dir_path],
        capture_output=True,
        text=True
    )
    
    lines = result.stdout.strip().split('\n')
    # The last line contains the directory CID
    dir_cid = lines[-1].split()[1]
    
    return dir_cid

def get_directory_structure(cid):
    """List all files in an IPFS directory."""
    result = subprocess.run(
        ["ipfs", "ls", cid],
        capture_output=True,
        text=True
    )
    
    files = []
    for line in result.stdout.strip().split('\n'):
        if line:
            parts = line.split()
            files.append({
                "name": parts[0],
                "cid": parts[1],
                "size": parts[2]
            })
    return files

This approach is particularly useful when deploying static websites to IPFS. You can maintain version history by uploading new directory versions and tracking their CIDs in a manifest.

Integrating with Claude Code Skills

Claude Code’s skills system pairs excellently with IPFS workflows. You can create a skill that standardizes file uploads across your team:

// IPFS Upload Skill for Claude Code
{
  "name": "ipfs-upload",
  "description": "Upload files to IPFS with automatic pinning",
  "parameters": {
    "file_path": "string",
    "pin_service": "string (optional, default: local)"
  },
  "execute": async (params) => {
    const { file_path, pin_service = 'local' } = params;
    
    // Validate file exists
    const fs = require('fs');
    if (!fs.existsSync(file_path)) {
      throw new Error(`File not found: ${file_path}`);
    }
    
    // Upload to IPFS
    const cid = await ipfsAdd(file_path);
    
    // Handle pinning based on service
    if (pin_service !== 'local') {
      await pinContent(cid, pin_service);
    }
    
    return {
      success: true,
      cid,
      gateway_url: `https://ipfs.io/ipfs/${cid}`
    };
  }
}

This skill can then be invoked naturally in conversations with Claude Code: “Upload the build artifacts to IPFS and pin them to Pinata.”

Handling Large Files and Streaming

For large files, consider using chunked uploads to avoid memory issues. IPFS automatically chunks large files, but streaming uploads provide better control:

import subprocess

def stream_upload_large_file(file_path, chunk_size=1024*1024):
    """Upload large files with progress tracking."""
    import hashlib
    
    with open(file_path, 'rb') as f:
        chunk_num = 0
        while True:
            chunk = f.read(chunk_size)
            if not chunk:
                break
            
            # Process chunk (add to IPFS in chunks)
            result = subprocess.run(
                ["ipfs", "add", "-Q", "-", file_path],
                input=chunk,
                capture_output=True
            )
            
            chunk_num += 1
            print(f"Processed chunk {chunk_num}")
    
    return result.stdout.decode().strip()

Best Practices for Production Workflows

When deploying IPFS workflows in production, consider these essential practices:

Always pin critical content: Without pinning, content may become unavailable when no nodes have a copy. Use multiple pinning services for redundancy.

Track your CIDs systematically: Maintain a database or manifest that maps application data to IPFS CIDs. This enables version management and rollback capabilities.

Use IPNS for mutable references: IPFS CIDs are content-addressed and immutable. For dynamic content that updates, use IPNS (InterPlanetary Name System) to create mutable pointers.

Implement retry logic: Network operations can fail. Build retry mechanisms into your workflows:

import time

def upload_with_retry(file_path, max_retries=3):
    """Retry IPFS upload on failure."""
    for attempt in range(max_retries):
        try:
            return upload_to_ipfs(file_path)
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            print(f"Attempt {attempt + 1} failed, retrying...")
            time.sleep(2 ** attempt)  # Exponential backoff

Monitor gateway performance: Different IPFS gateways have varying performance characteristics. Implement fallback gateway selection for critical reads.

Conclusion

Claude Code provides an excellent platform for building IPFS decentralized storage workflows. By combining Claude Code’s automation capabilities with IPFS’s content-addressed storage, you can create robust systems that ensure data persistence without relying on centralized infrastructure.

Start with simple single-file uploads, then expand to directory management, pinning services, and production-grade error handling. The workflows outlined in this guide provide a solid foundation for building censorship-resistant applications and decentralized data pipelines.

Remember to always consider your redundancy needs, implement proper error handling, and maintain systematic CID tracking. With these patterns, you’re well-equipped to use decentralized storage effectively in your projects.

Built by theluckystrike — More at zovo.one