@dialectlabs/sdk

中文文档
2.0.0-beta.7发布于 2025/06/19Apache-2.0TypeScriptESM + CJS近 30 天下载 8869

Overview

原始使用文档
文档目录16 节
  1. Overview
  2. Messaging
  3. Dapp Notifications & Subscriptions
  4. Authentication
  5. Messaging
  6. Create an SDK client & connect your wallet to it
  7. Get existing threads for a wallet
  8. Get a thread by its id & read thread messages
  9. Create a new thread
  10. Send a message
  11. Delete a thread
  12. Dapp Notifications & Subscriptions
  13. Register a dapp
  14. Get your dapp
  15. Get all dapps
  16. Fetch all whitelisted dapp messages for a given wallet

SDK

Overview

The Dialect SDK is a single typescript library for interacting with Dialect's on and off chain resources. These resources include:

Messaging

All messaging, user to user or dapp to user, is powered by the same Dialect messaging protocol, which supports both on and off chain messaging. With the messaging SDK you can:

  • view all message threads
  • create & delete threads, &
  • send & receive messages within threads.

Dapp Notifications & Subscriptions

In addition to general messaging, Dialect supports an API for dapps to send notifications to their users, both via the Dialect messaging protocols, as well as via web2 channels, including email, Telegram, & SMS.

To support this, the Dialect SDK includes APIs for:

  • dapps to register themselves using their messaging keypair.
  • wallets to opt in to receiving messages from dapps.
  • wallets to configure what channels they'd like to receive dapp messages to, including wallet (using Dialect's protocol), email, Telegram, & SMS.

Authentication

  • For all on-chain transactions, authentication & authorization is baked in since wallets must sign all transactions.
  • For all off-chain requests, the Dialect API requires all requests to be authenticated using the wallet signMessage method, which proves that the entity making the request has custody of the wallet keypair in question.

Messaging

Create an SDK client & connect your wallet to it

Run the create-solana-sdk example script.

To use the Dialect SDK, you'll need to create an SDK client with an assigned wallet to:

import {
  Backend,
  ConfigProps,
  Dialect,
  DialectSolanaWalletAdapterWrapper,
  EncryptionKeysStore,
  NodeDialectSolanaWalletAdapter,
  TokenStore,
} from '@dialectlabs/sdk';
import type {
  DialectSdk,
} from '@dialectlabs/sdk';

// Backends tell the SDK where messages can be sent and received to, & in what priority.
const backends = [Backend.DialectCloud, Backend.Solana];
// Provide configuration options for how to target the DialectCloud backend
const dialectCloud = {
  url: 'https://dialectapi.to',
  // Provide a keystore for storing authentication headers, which are expiring tokens generated from the wallet's signMessage method
  tokenStore: TokenStore.createInMemory(),
};
// Convenience for setting smart defaults for backends. Variables above are optional if this is provided.
const environment = 'production';
// Provide a keystore for storing e.g. encryption & decryption keys.
const encryptionKeysStore = EncryptionKeysStore.createInMemory();
// Provide override parameters for Solana backend configuration, including RPC url
const solana = {
  rpcUrl: 'https://api.mainnet-beta.solana.com',
};
// A node wallet for running in a node environment. N.b. this would be a mobile or web wallet object instead. The Wrapper class handles conversion to the API expected by the Dialect SDK, but is mostly consistent with expected wallet APIs.
const wallet = DialectSolanaWalletAdapterWrapper.create(
  // IMPORTANT: must set environment variable DIALECT_SDK_CREDENTIALS
  // to your dapp's Solana messaging wallet keypair e.g. [170,23, . . . ,300]
  NodeDialectSolanaWalletAdapter.create(),
);

// Now construct the SDK client from all of the above
const sdk: DialectSdk = Dialect.sdk({
  backends,
  dialectCloud,
  environment,
  encryptionKeysStore,
  solana,
  wallet,
} as ConfigProps);

Get existing threads for a wallet

Run the get-threads example script.

import type {
  // ... previous imports
  Thread,
  ThreadId,
} from '@dialectlabs/sdk';

// ... code from previous examples

// Fetch all threads the wallet is a part of, across all provided backends
const threads: Thread[] = await sdk.threads.findAll();

Note that the threads returned here are fetched from both on- and off-chain backends: Backends.Solana & Backends.DialectCloud.

Get a thread by its id & read thread messages

Run the get-messages example script.

import type {
  // ... previous imports
  Message,
} from '@dialectlabs/sdk';

// ... code from previous examples

// Choose a given thread
const thread = threads[0];

// Fetch for a single thread by its id. N.b. the ThreadId type specifies both the address of the thread *as well as* the specified backend; threads of a given id may exist in any kind of backend. See the ThreadId type.
const query: FindThreadByIdQuery = {
  id: thread.id,
}
const thread = await sdk.threads.find(query);
// Call the messages() method to read messages
const messages = await thread.messages();

Create a new thread

Run the create-thread example script.

import type {
  // ... previous imports
  ThreadMemberScope,
} from '@dialectlabs/sdk';

const recipient = new PublicKey('3vuCFLbVWsNeWgyxkb2xiLQuxKDW83HWiTMmodT8gmtk') // Make this arbitrary
const command: CreateThreadCommand = {
  encrypted: false,
  me: {
    scopes: [ThreadMemberScope.ADMIN, ThreadMemberScope.WRITE],
  },
  otherMembers: [
    {
      publicKey: recipient,
      scopes: [ThreadMemberScope.ADMIN, ThreadMemberScope.WRITE],
    },
  ],
};
// Uses the default backend.DialectCloud offchain. The command above may optionally take a preferred backend.
const thread = await sdk.threads.create(command);

Send a message

Run the send-message example script.

// ... code from previous examples

const text = 'gm world';
const command: SendMessageCommand = {
  text,
}
await thread.send(command);

Delete a thread

Run the delete-thread example script.

const threadId = thread.id; // Keep for re-querying to confirm deletion
await thread.delete();
const query: FindThreadByIdQuery = {
  id: threadId,
};
const refetchedThread = await sdk.threads.find(query); // Will be null

Dapp Notifications & Subscriptions

The following examples describe how to interact use the Dialect SDK to manage dapp and notification related resources.

Register a dapp

To allow users to subscribe to notifications from you, you first need to create a dapp in the Dialect API.

// Construct the SDK client from all of the above. Let's use 'development' for the environment, and pass
const environment = 'development';
const dialectCloud = {
  // N.b. we omit the url value here, since 'development' will default to https://dev.dialectapi.to.
  tokenStore: TokenStore.createInMemory(),
};
// Use ConfigProps values from example above.
const sdk: DialectSdk = Dialect.sdk({
  backends,
  dialectCloud,
  environment,
  encryptionKeysStore,
  solana,
  wallet,
} as ConfigProps);

// N.b. this created dapp is associated with the wallet public key connected to the sdk instance.
const dapp = await sdk.dapps.create({
  name: 'My test dapp',
  description: `My test dapp's description.`
});

Note that all dapps created with Dialect must be associated with a wallet keypair. This keypair will be used for all authenticated requests made for the dapp, and for all user messaging.

The sdk.dapps.create method above uses the wallet public key connected to the sdk client instance.

You can only create one dapp per public key.

Dapp whitelist

Dialect manages a list of verified dapps that have integrated with Dialect. While all messaging is done via wallets and keypairs and sent along the core messaging rails, this whitelist is convenient for surfacing just messages that are specifically from the most well-known dapps on Solana.

Whether or not this list continues to be maintained is to be determined.

If you'd like to be added to this list, please reach out to us on twitter or email us at [email protected].

Get your dapp

If the wallet you've connected to your sdk client instance is associated with a dapp, you can get the dapp record by making a simple find call.

const dapp = await sdk.dapps.find();

Get all dapps

You can query for all registered dapps in the the Dialect API DB.

const dapps = await sdk.dapps.findAll();

Fetch all whitelisted dapp messages for a given wallet

The Dialect SDK provides an endpoint for querying all messages from whitelisted dapps for a given wallet. This is convenient for producing a single feed of notifications.

// ... code from previous examples

const messages = await sdk.wallet.dappMessages.findAll({
  dappVerified: true,
});

版本与发布记录

@dialectlabs/sdk 共发布 91 个版本,首次发布于 2022/06/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0-beta.7latest2025/06/191.4 年前
2.0.0-beta.62025/06/181.4 年前
2.0.0-beta.52025/05/291.5 年前
2.0.0-beta.42025/05/291.5 年前
2.0.0-beta.32025/05/281.5 年前
2.0.0-beta.22025/05/081.5 年前

其他发布通道:beta → 2.0.0-beta.1。

关于 @dialectlabs/sdk 的常见问题

@dialectlabs/sdk 是什么?

Overview

如何安装 @dialectlabs/sdk?

在项目目录运行 npm install @dialectlabs/sdk,或使用 pnpm add / yarn add / bun add。

@dialectlabs/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dialectlabs/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dialectlabs/sdk 可以商用吗?

@dialectlabs/sdk 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dialectlabs/sdk 的下载量有多少?

根据 npm 官方下载统计,@dialectlabs/sdk 在 2026/10/12 – 2026/11/10 这 30 天内被下载 8,869 次,日均约 296 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dialectlabs/sdk 还在维护吗?

最新版本 2.0.0-beta.7 发布于 2025/06/19(1.4 年前);至今共发布 91 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。