DIM · Dagon Inference Marketplace

Buyer quickstart

This quickstart takes you from nothing to a first paid token with the TypeScript SDK, @im/sdk. You pin a discovery index, find routes for one exact model, open a channel from your vault, stream a reply directly from the seller, and pay for it with a voucher after checking its receipt. It is one file of about 60 lines.

Before you start

You need:

  • Bun and a checkout of the DIM repository. The SDK is the workspace package @im/sdk; it is not published to npm, and it runs from TypeScript under Bun (or from a Node 22+ ESM bundle you build). Create a small workspace package for your script, for example demo/first-token/package.json with "type": "module" and the dependencies "@im/sdk": "workspace:*", "@im/protocol": "workspace:*" and "@solana/web3.js": "^1.98.4", then run bun install at the repository root.
  • A discovery index and its pinned key. The index's operator publishes its URL and attestation public key. Get the key through a channel you trust; the copy at /.well-known/im-indexer.json is informational only.
  • At least one probe operator key whose measurements you trust. Without one, routes still work but carry no probed evidence.
  • A funded vault on the program's cluster. The default program build targets Solana devnet with a test mint. Creating and funding the vault is covered in funding.
  • The exact model identity ID you want: 64 hex characters. Find it by family with the index, see choosing a route.

Set them as environment variables:

export DIM_INDEXER_URL=https://index.example.com     # your index
export DIM_INDEXER_KEY=<base58 attestation key>
export DIM_PROBE_OPERATOR_KEY=<base58 operator key>
export DIM_MODEL_IDENTITY_ID=<64 hex characters>
export SOLANA_RPC_URL=https://api.devnet.solana.com
export DIM_OWNER_KEYPAIR=$HOME/.config/solana/dim-owner.json
export DIM_VAULT=<base58 vault address>

The whole program

import { readFileSync } from "node:fs";
import { Connection, Keypair } from "@solana/web3.js";
import { ChannelWallet, DiscoveryClient, MarketplaceClient } from "@im/sdk";
import { SolanaChannelBackend } from "@im/sdk/solana";

function env(name: string): string {
  const value = process.env[name];
  if (!value) throw new Error(`set ${name}`);
  return value;
}

// 1. Find routes for one exact model, verified against pinned keys.
const discovery = new DiscoveryClient({
  indexer: { url: env("DIM_INDEXER_URL"), key: env("DIM_INDEXER_KEY") },
  operators: [env("DIM_PROBE_OPERATOR_KEY")],
});
const modelIdentityId = env("DIM_MODEL_IDENTITY_ID");
const routes = await discovery.list({ modelIdentityId });
const usable = routes.filter((route) => route.capacity !== null && route.capacity.headroom > 0 && route.policy.zdr);
if (usable.length === 0) throw new Error("no route with fresh capacity and zero data retention");

// 2. Connect your vault on Solana.
const owner = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(readFileSync(env("DIM_OWNER_KEYPAIR"), "utf8"))));
const backend = new SolanaChannelBackend({
  connection: new Connection(env("SOLANA_RPC_URL"), "confirmed"),
  signer: owner,
  vault: env("DIM_VAULT"),
});
const wallet = new ChannelWallet({ backend });
const sdk = new MarketplaceClient({
  wallet,
  domain: backend.domain,
  models: { "my-model": modelIdentityId },
  policy: { zdr: true },
  defaultCapMicros: 20_000n, // at most 0.02 USDC per request
});
sdk.setRoutes(usable);
await sdk.reattach(); // picks up channels from earlier runs; harmless on the first run

// 3. Fund a 2 USDC channel with the first usable seller, if you have none yet.
const seller = usable[0]!;
if (wallet.channels(seller.sellerId).length === 0) {
  const now = Math.floor(Date.now() / 1000);
  await wallet.open(seller, { budget: 2_000_000n, serviceEnd: now + 24 * 3600, graceSeconds: 3600 });
}

// 4. Stream a reply straight from the seller. The SDK signs a hold and checks the receipt.
const stream = await sdk.chat.completions.create({
  model: "my-model",
  messages: [{ role: "user", content: "Say hello in five words." }],
  stream: true,
});
for await (const chunk of stream) process.stdout.write(String(chunk.choices[0]?.delta.content ?? ""));

// 5. Pay: sign a voucher for the verified receipts and wait for the seller's acknowledgment.
await sdk.flushVouchers();
await wallet.closeStore();

Save it as demo/first-token/first-token.ts and run bun demo/first-token/first-token.ts.

What just happened

  1. discovery.list fetched every page of signed routes for your model identity. The SDK checked the index's signature against your pinned key, the query binding and freshness, each seller's listing signature, and recomputed every route ID, model identity ID and price digest. Observations from operators you did not pin are ignored.
  2. wallet.open sent one open_channel transaction moving 2 USDC from your vault's free balance into a channel with that seller, bound to the seller's settler and payout account, with a fresh session key.
  3. create chose a route with the SDK's planner, signed a hold with a fresh request ID and salt, and posted your request directly to the seller's endpoint.
  4. The stream ended with an im_receipt event. The SDK verified the seller's signature, its binding to your hold, and that the charge matches the signed price schedule.
  5. flushVouchers signed a voucher for the new running total and posted it with the receipt. The seller acknowledged it; its settler will redeem it on-chain later.

Next steps

  • Keep the SDK's wallet journal: the Solana backend stores it under ~/.im-sdk by default. Restarting is covered in recovery.
  • Give agents their own budget with a Mandate instead of the owner key; see funding.
  • When you are done, close the channel to get the unspent budget back.