Skip to content

Getting Started

Installation

npm install @offchain-wtf/sdk

Basic Usage

import { OffchainClient } from '@offchain-wtf/sdk';
 
const client = new OffchainClient({
  gatewayUrl: 'https://gateway.example.com',
  privateKey: process.env.PRIVATE_KEY,
});
 
const content = await client.get('bafkreigyc3t7nakfpzdp4gb2hhmx5whfhnqp6qmzq2fm36aqyfmnmtq7ya');
console.log(content.data);

That's it. Payment happens automatically. Each request costs $0.01 USDC.

Add Caching

Since requests aren't free, you probably want to cache:

const cache = new Map();
 
async function getCached(cid) {
  if (cache.has(cid)) return cache.get(cid);
  const content = await client.get(cid);
  cache.set(cid, content);
  return content;
}

Configuration

Optional settings:

const client = new OffchainClient({
  gatewayUrl: 'https://gateway.example.com',
  privateKey: process.env.PRIVATE_KEY,
  network: 'base',              // or 'base-sepolia'
  timeout: 30000,               // milliseconds
  verbose: false,               // debug logging
});

Error Handling

try {
  const content = await client.get(cid);
} catch (error) {
  console.error(error.message);
}

Common issues:

  • Invalid key format (must be 64 hex characters)
  • Insufficient USDC balance
  • Gateway URL unreachable

Important

Keep your private key on your backend. Never expose it to the browser.

Your backend receives user requests → signs payments → fetches content → returns it.