Skip to main content

IPFS Client

The IPFSClient handles pinning policy JSON to IPFS and retrieving it. It supports multiple backends for redundancy.

Usage

import { IPFSClient } from "@policykit/sdk";

const ipfs = new IPFSClient({
  backends: [
    {
      type: "pinata",
      jwt: process.env.PINATA_JWT,
    },
  ],
});

// Pin a policy
const cid = await ipfs.pin(policy);
console.log(cid); // "QmXyz..."

// Retrieve a policy
const retrieved = await ipfs.get(cid);
console.log(retrieved); // Policy object

Constructor

new IPFSClient(options: IPFSClientOptions)

Options

OptionTypeRequiredDescription
backendsIPFSBackend[]YesArray of IPFS backend configurations
timeoutnumberNoRequest timeout in milliseconds (default: 30000)

IPFSBackend

// Pinata backend
interface PinataBackend {
  type: "pinata";
  jwt: string;          // Pinata JWT token
  gateway?: string;     // Custom gateway URL
}

// Custom backend
interface CustomBackend {
  type: "custom";
  endpoint: string;     // API endpoint URL
  headers?: Record<string, string>;
}

Methods

pin(policy)

Pin a policy document to IPFS. The policy is serialized to JSON and pinned to all configured backends.
const cid = await ipfs.pin(policy);
ParameterTypeDescription
policyPolicyThe policy object to pin
Returns: Promise<string> — The IPFS CID The client pins to all backends in parallel for redundancy. The CID is deterministic — the same policy always produces the same CID.

get(cid)

Retrieve and parse a policy from IPFS.
const policy = await ipfs.get("QmXyz...");
ParameterTypeDescription
cidstringThe IPFS content identifier
Returns: Promise<Policy> The client tries each backend in order until one succeeds. The retrieved JSON is validated against the Policy schema.

unpin(cid)

Unpin a policy from all backends.
await ipfs.unpin("QmXyz...");
ParameterTypeDescription
cidstringThe IPFS CID to unpin
Returns: Promise<void>
Unpinning removes the policy from your IPFS backends. If no other node has pinned the content, it may eventually be garbage collected from the IPFS network.

cidFromPolicy(policy)

Calculate the CID for a policy without pinning it. Useful for checking if a policy is already pinned.
const cid = await ipfs.cidFromPolicy(policy);
ParameterTypeDescription
policyPolicyThe policy object
Returns: Promise<string> — The IPFS CID

Error Handling

The client throws specific errors:
ErrorDescription
IPFSPinErrorFailed to pin to any backend
IPFSRetrieveErrorFailed to retrieve from any backend
IPFSValidationErrorRetrieved JSON doesn’t match Policy schema
IPFSTimeoutErrorRequest exceeded timeout