MeshKit

Examples

Copy-paste MeshKit SDK examples by workflow.

Use these examples as starting points for application features. Each example uses the SDK facade and keeps production infrastructure out of feature code.

For production, move provider configuration into meshkit.config.json or an infrastructure module, keep secrets in environment variables, and validate the provider path with the CLI.

Shared Client Factory

// src/lib/meshkit.ts
import { meshkit } from "@meshkit/meshkit";

export function createMeshStorage(identity = "app-device") {
  return meshkit({ identity });
}

Save A User Upload

import { createMeshStorage } from "./meshkit";

export async function saveUpload(fileName: string, bytes: Uint8Array, contentType: string) {
  const mesh = await createMeshStorage();

  const saved = await mesh.files.put(fileName, bytes, {
    contentType,
    metadata: { source: "user-upload" },
  });

  return {
    cid: saved.cid,
    size: saved.size,
    provider: saved.proof.provider,
  };
}

Persist the returned CID with your application's upload record.

Store Private Preferences

import { createMeshStorage } from "./meshkit";

type Preferences = {
  theme: "light" | "dark";
  notifications: boolean;
};

export async function savePreferences(userId: string, preferences: Preferences) {
  const mesh = await createMeshStorage(`user:${userId}`);

  const saved = await mesh.records.put<Preferences>("preferences", preferences, {
    metadata: { userId, schema: "preferences.v1" },
  });

  return saved.cid;
}

Use app-level schema versions because MeshKit does not validate record schemas.

Share A Support Bundle

import { createMeshStorage } from "./meshkit";

export async function saveAndShareSupportBundle(bytes: Uint8Array) {
  const mesh = await createMeshStorage("support-bot");

  await mesh.identity.create("incident-owner");

  const bundle = await mesh.files.put("support/bundle.zip", bytes, {
    contentType: "application/zip",
    metadata: { category: "support-bundle" },
  });

  const capsule = await mesh.share.file(bundle).with("incident-owner", {
    expiresIn: "24h",
  });

  await mesh.share.send(capsule, "incident-owner");

  return {
    cid: bundle.cid,
    capsuleId: capsule.id,
  };
}

In production, replace the local identity example with your contact, directory, DID, wallet, or passkey binding model.

Queue Offline Work

import { createMeshStorage } from "./meshkit";

export async function queueDraftSync(draftId: string) {
  const mesh = await createMeshStorage();

  return mesh.sync.enqueue({
    kind: "upload-draft",
    payload: { draftId },
    conflictKey: `draft:${draftId}`,
    conflictPolicy: "replace",
  });
}

Your app still owns when mesh.sync.flush() runs.

Read Errors Safely

import { MeshKitError } from "@meshkit/meshkit";
import { createMeshStorage } from "./meshkit";

export async function openStoredFile(cid: string) {
  const mesh = await createMeshStorage();

  try {
    return await mesh.files.get(cid);
  } catch (error) {
    if (error instanceof MeshKitError) {
      return {
        error: error.code,
        suggestion: error.suggestion,
      };
    }

    throw error;
  }
}

Log error codes and sanitized suggestions. Do not log provider tokens, private keys, capability token secrets, or decrypted content.

Choose The Dedicated Guide

If you are building...Read
Uploads, downloads, and attachmentsFiles
JSON-like private stateRecords
Recipient accessSharing
Inbox workflowsMessages
Large contentStreaming
Offline or retryable jobsSync
Production providersProduction setup

On this page