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.
| Request | Fields |
|---|---|
| GET /v1/keys/public | — |
| GET /v1/ads/pack | anon_id, terminal_type, max, features=game |
| POST /v1/telemetry/events | event, ts, anon_id, terminal_type, sdk_version (+ duration_ms, ad_id) |
| POST /v1/publisher/devices | anon_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
| Option | Default | What 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. |
| minWaitMs | 8000 | Quiet time before an ad may appear. |
| idleThresholdMs | 1500 | Quiet time before a wait counts. |
| cooldownMs | 60000 | Gap after an ad hides before the next one. |
| maxPerSession | 10 | Max ads per SDK instance. |
| capPerHour | 6 | Max ads per sliding hour. |
| maxDisplayMs | 300000 | An 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 }.
withMeshAdtrackStreamonRequestStartonFirstTokenonStreamTokenonToolCallStartonToolCallEndonUserInputonRequestEnd0.1 methods (frozen contract v1)
Still valid and covered by a conformance test. A breaking change would bump SDK_API_VERSION.
onProcessingStartonProcessingEndcurrentAdclickgetBalanceflushdestroyDiagnostics 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_DEFAULTSWhen 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.
- 1A valid signature is required to display anything.
- 2An invalid signature — or one made by a different key — drops the ad; nothing is drawn (counted in invalid_signatures_dropped).
- 3You can audit it yourself with verifyAd and canonicalAdJson.
- 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