@hazbase/storage is a lightweight SDK for the hazBase IPFS storage API (backed by Pinata).
It works in browsers and Node.js and provides concise functions for pinning (upload), unpinning (delete), and building public gateway URLs.
Under the hood, it integrates with @hazbase/auth to validate your client key.
- Base endpoint:
{{API_ENDPOINT}}/api/ipfs/*(configure via@hazbase/auth) - Main functions:
pinFile(files),unpin(cid),gatewayUrl(cid) - Internals: Node uses
formdata-nodeandform-data-encoderfor streamingFormData
- Node.js: 18+ (built‑in
fetch/ReadableStream/duplex: "half") - TypeScript: 5+ (recommended)
- Runtime: Browser or Node.js
npm i @hazbase/storage @hazbase/auth
# or
pnpm add @hazbase/storage @hazbase/auth@hazbase/storage does not read environment variables directly. Configure @hazbase/auth once at app start.
// All comments in English (project convention)
import { setClientKey } from '@hazbase/auth';
setClientKey(process.env.HAZBASE_CLIENT_KEY!); // required for validation & loggingimport { pinFile, gatewayUrl } from '@hazbase/storage';
async function onUpload(e: Event) {
const input = e.target as HTMLInputElement;
const files = Array.from(input.files ?? []);
// 1) Pin files to IPFS
const results = await pinFile(files, { name: 'my-assets', folder: 'v1' });
// 2) Show gateway URLs
for (const r of results) {
console.log('CID:', r.cid, 'URL:', gatewayUrl(r.cid));
}
}import { readFile } from 'node:fs/promises';
import { pinFile } from '@hazbase/storage';
const buf = await readFile('./image.png');
const res = await pinFile([buf], { name: 'image.png', contentType: 'image/png' });
console.log(res[0].cid);import { unpin } from '@hazbase/storage';
await unpin('bafybeigdyrzt...'); // Remove from Pinata (via HAZAMA BASE API)- What it does: Accepts
File[] | Blob[] | Buffer[], posts to/api/ipfs/pinFiles, and returns CIDs. - Params
files:File[] | Blob[] | Buffer[]opts?:UploadOptions(name?: string,folder?: string,contentType?: string)
- Returns:
PinResult[](at minimum includes{ cid: string })
Example
// Pin multiple assets
const pinned = await pinFile([file1, file2], { name: 'batch-2025-09-11', folder: 'assets' });- What it does: Unpins an existing CID.
- Params:
cid: string(CIDv0/v1) - Returns: backend response object (Pinata
unpinequivalent)
Example
await unpin('bafybeigdyrzt...');- What it does: Returns a public gateway URL by concatenating a base (default:
https://gateway.pinata.cloud/ipfs/) with the CID. - Example
const url = gatewayUrl('bafybeigdyrzt...');
// -> https://gateway.pinata.cloud/ipfs/bafybeigdyrzt...// Basic aliases
export type CID = string;
export interface UploadOptions {
/** Optional logical name for the batch/file */
name?: string;
/** Optional folder tag (Pinata metadata keyvalues.folder) */
folder?: string;
/** Content-Type (used for Node Buffer uploads) */
contentType?: string;
}
export interface PinResult {
/** Content identifier returned by IPFS/Pinata */
cid: CID;
/** Optional fields may be present (e.g., size, isDuplicate, name) */
[k: string]: any;
}The response can be extended by your backend. Design for at least a
cidfield.
- On HTTP errors,
request()throwsError(status, statusText, body)(e.g.,Pinata POST /pinFiles: 401 Unauthorized ...). pinFile/unpinpropagate exceptions. In UIs, add retry and user‑visible messages.
- Key hygiene: call
setClientKey()at app startup. In browsers, persist as little as possible. - CORS: if you call the API directly from browsers, ensure environment‑appropriate CORS settings.
- Large files: uploads >100MB take longer. Provide progress UI and consider chunking/multipart strategies.
- Public URLs: if you change the default gateway, consider CDN/LB in front of it.
Client key not set: callsetClientKey()first.Nonce request failed: sign‑in/validation may be failing; recheck@hazbase/authsetup.403/401: client‑key privileges or expiration; reissue or switch environment.TypeError: fetch failed: network/proxy or server routing to/api/ipfsis broken.
Apache-2.0