MeshAd

SDK

Put MeshAd inside your agent.

Building an AI coding tool? Embed one signed, sponsored line that only shows while your user is waiting — and disappears on the first token or keystroke. Same privacy guarantees as the official client.

npm install @meshad/sdk
  • v0.2.1
  • CommonJS · Node 22+
  • TypeScript types included
  • Apache-2.0

Quickstart

Install, init once, wrap the calls that make your user wait. That is the whole integration.

1 · Install

npm install @meshad/sdk. The package pulls @meshad/schema (the telemetry allowlist) and @meshad/creative (the art contract) from npm.

2 · init + withMeshAd / trackStream

init() is synchronous. Wrap any async function with withMeshAd, or iterate a stream through trackStream — the first item hides the ad, and the content is never inspected.

3 · terminalType, stateDir, publisherKey

Use terminalType: 'other' until your vendor is registered. Set stateDir so the anonymous id is stable across runs. publisherKey is optional and credits your publisher account.

const path = require('node:path');
const os = require('node:os');
const { init } = require('@meshad/sdk');

// init() is synchronous: it returns the SDK instance, not a Promise.
const meshad = init({
  terminalType: 'other', // closed enum; 'other' until your vendor is registered
  stateDir: path.join(os.homedir(), '.my-agent'), // stable anonymous id
  publisherKey: process.env.MESHAD_PUBLISHER_KEY, // optional
});

// Wrap any async function that waits on a model or tool:
const callModel = meshad.withMeshAd(myModelCall);

// Streams: the first item hides the ad; content is never inspected.
async function answer(stream) {
  for await (const chunk of meshad.trackStream(stream)) render(chunk);
}

// Typing hides the ad:
process.stdin.on('data', () => meshad.onUserInput());

// On exit: await meshad.destroy();

Privacy: it never reads your user's work

The SDK never reads, receives, stores or sends prompts, code, responses, file paths, repository names or any user content. Lifecycle events carry no arguments — timing only. Every event is validated against the closed @meshad/schema allowlist before it is queued, and network egress is locked to the API origin.

Never leaves the process

  • prompts
  • source code
  • model responses
  • file paths
  • repository names
  • cwd, hostname, username

Timing only

onRequestStart, onFirstToken, onStreamToken, onToolCallStart/End, onUserInput and onRequestEnd take no arguments. There is nowhere to put content.

Everything that leaves the process

This table is the same allowlist enforced by test/privacy-canary.test.js.

RequestFields
GET /v1/keys/public—
GET /v1/ads/packanon_id, terminal_type, max, features=game
POST /v1/telemetry/eventsevent, ts, anon_id, terminal_type, sdk_version (+ duration_ms, ad_id)
POST /v1/publisher/devicesanon_id, platform, sdk_version
GET /v1/publisher/me—

Headers: only content-type and authorization (the publisherKey, sent to the API only). The privacy-canary test runs a full session with canary strings — prompt, response, paths, repo, cwd, hostname, user — and fails if any shows up in any URL, header or body.

API reference

Types are the source of truth: packages/sdk/index.d.ts. init(options) returns a MeshAdSdk instance.

Options

OptionDefaultWhat it does
terminalType—Your agent's terminal_type (closed enum). Required. 'other' until registered.
stateDir—Directory where the SDK may persist the anonymous id (file meshad-anon-id, mode 0600). Without it the id is ephemeral and earns no credit.
publisherKey—Optional. Links this device to your publisher account so waits are credited.
renderer'terminal''terminal' draws with the CLI renderer; 'headless' only calls onAd/onClear (Ink, VS Code).
onAd / onClear—Callbacks for headless UIs. onClear must remove the ad immediately.
minWaitMs8000Quiet time before an ad may appear.
idleThresholdMs1500Quiet time before a wait counts.
cooldownMs60000Gap after an ad hides before the next one.
maxPerSession10Max ads per SDK instance.
capPerHour6Max ads per sliding hour.
maxDisplayMs300000An ad never stays up longer than this (5 min).
disabled / interactive—Host kill switch / force past the TTY probe. Never overrides MESHAD_DISABLE, CI or meshad pause.

0.2 hooks and adapters

Lifecycle hooks: timing only, never content. onRequestEnd returns { impression, duration_ms }.

withMeshAdtrackStreamonRequestStartonFirstTokenonStreamTokenonToolCallStartonToolCallEndonUserInputonRequestEnd

0.1 methods (frozen contract v1)

Still valid and covered by a conformance test. A breaking change would bump SDK_API_VERSION.

onProcessingStartonProcessingEndcurrentAdclickgetBalanceflushdestroy

Diagnostics and utilities

diagnostics() reports state without content: enabled, disabled_reason, state, anon_id_source, device_link, ads_cached, invalid_signatures_dropped, events_pending, shown_this_session. formatAd(ad, cols?, rows?) renders to ANSI lines for custom terminal UIs.

diagnosticsformatAdverifyAdcanonicalAdJsonAgentIdleFsmFSM_DEFAULTS

When an ad appears

idle → waiting → showing → cooldown. Nothing shows before minWaitMs (8 s) of quiet; the ad hides on the first token, tool start or user input. Never in non-interactive environments (no TTY, CI=true, TERM=dumb); NO_COLOR is respected.

User opt-out always wins

  • MESHAD_DISABLE=1 — no ads, no requests, no timers.
  • meshad pause / meshad pause forever — the SDK honours the official CLI config.
  • init({ disabled: true }) — the host's own kill switch.
  • CI=true and a missing TTY also turn it off. diagnostics().disabled_reason says which.

Signed packs: verified before they are shown

Every ad in the pack is signed with Ed25519 by the network. The SDK fetches the public key and verifies each ad before drawing it.

  1. 1A valid signature is required to display anything.
  2. 2An invalid signature — or one made by a different key — drops the ad; nothing is drawn (counted in invalid_signatures_dropped).
  3. 3You can audit it yourself with verifyAd and canonicalAdJson.
  4. To audit an installation and its keys as a user, run meshad verify (official CLI).
const { verifyAd, canonicalAdJson } = require('@meshad/sdk');

verifyAd(ad, publicKeyB64); // DER-SPKI base64 key -> true | false
canonicalAdJson(ad);        // the exact bytes the network signed