@amityco/ts-sdk

中文文档
7.33.1发布于 2026/11/06CC-BY-ND-4.0TypeScriptESM + CJS近 30 天下载 4.4万

Amity Social Cloud Typescript SDK

原始使用文档
文档目录6 节
  1. Getting Started
  2. Runtime support
  3. Creating a client instance
  4. Observe an object using Live Object
  5. Observe a collection using Live Collection
  6. Documentation

Amity Social Cloud Typescript SDK

Getting Started

  • With npm: npm install @amityco/ts-sdk
  • With yarn: yarn add @amityco/ts-sdk

Runtime support

  • Modern browsers (es2017 compliant)
  • NodeJS (via esm imports)
  • React-Native

Creating a client instance

To connect your users to our platform, you need to create a client instance and connect it.

import { Client } from '@amityco/ts-sdk';

(async () => {
  const sessionHandler: Amity.SessionHandler = {
    sessionWillRenewAccessToken(renewal: Amity.Renewal) {
      renewal.renew();
      /*
       * If using an auth token
       *
       * try {
       *  renew.renewWithAuthToken(authToken)
       * } catch() {
       *  sdk will try to renew again at a later time
       *
       *  renew.unableToRetrieveAuthToken()
       * }
       */
    },
  };
  const client = Client.createClient(API_KEY, API_REGION);

  let isConnected = await Client.login({ userId: USER_ID }, sessionHandler);
})();

The API_KEY and API_REGION are given at the time you create an application in the Amity Portal.

You can listen to disconnection by using the onClientDisconnected function:

import { Client } from '@amityco/ts-sdk';

const unsub = Client.onClientDisconnected(() => (isConnected = false));

The unsub's return value is a dispose function which is used to clean the memory of your client's application. It's been designed to fit perfectly with React, in a useEffect such as:

useEffect(() => onClientConnected(() => setConnected(false)), [])

## Going further

### Domain types

All the necessary types are automatically exposed in the `Amity` namespace. As long as you import the SDK once, the namespace will be available in your workspace. You can start to type `Amity.` and see the list of types in your intellisense after having imported the SDK in any file of your project.

### Subscribing to real time events

We expose a couple of functions for your to receive simply the events that could fire from our servers, for example, if another user would change their display name, or if a channel would receive a new message.

The `subscribeTopic` method retuns an unsubscriber that can be called on clean up to stop recieving further events.

```ts
import { subscribeTopic, getUserTopic } from '@amityco/ts-sdk'

const unsub = subscribeTopic(getUserTopic({} as Amity.User))

Observe an object using Live Object

For an 'all-in-one' approach, we expose live objects, which allow to fetch an object and register to its changes all at once. This has been designed to fit with React's useState and useEffect:

import { UserRepository } from '@amityco/ts-sdk'

const Component = ({ userId }: { userId: string }) => {
  const [user, setUser] = useState<Amity.User>({})

  useEffect(() => UserRepository.getUser(userId, ({ data: user, loading, error }) => {
    // ensure you call unsub() on cleanup or unmount
    const unsub = subscribeTopic(getUserTopic(user))

    setUser(user)
  }), [userId])

  return <div>{JSON.stringify(user, null, 2)}
}

Observe a collection using Live Collection

Similar to the concept of live object, we expose live collection, which allow to fetch a collection and register to its changes all at once. This has been designed to fit with React's useState and useEffect:

import { UserRepository.getUsers } from '@amityco/ts-sdk'

const Component = () => {
  const [users, setUsers] = useState<Amity.User[]>({})

  useEffect(() => UserRepository.getUsers({}, ({ data, loading, error, nextPage, hasNextPage }) => {
    // ensure you call unsub() on cleanup or unmount
    // Subscribe to each user in collection for getting real time updates

    setUsers(users)
  }), [userId])

  return <div>{JSON.stringify(user, null, 2)}
}

Documentation

To see the comprehensive documentation please visit our documentation page.

If you have any questions, you can visit our forum.

版本与发布记录

@amityco/ts-sdk 共发布 2,485 个版本,首次发布于 2021/08/12;过去 12 个月发布了 492 个版本。

版本发布日期距今
7.33.1latest2026/11/061 天前
7.33.1-ed90356d.02026/11/061 天前
7.33.1-3b1a9bbf.02026/11/052 天前
7.33.1-37a3a70.02026/11/034 天前
7.33.1-b8614357.02026/11/034 天前
7.33.1-d2b15777.02026/11/034 天前

其他发布通道:dev/43 → 0.0.1-8cd7315.0,dev/40 → 0.0.1-0cf87e0.0,dev/95 → 0.0.1-3edad36.0,dev/123 → 0.0.1-fdf0d8d.0,dev/268 → 0.0.1-0fe3ce9.0,dev/277 → 0.0.1-83fb808.0。

关于 @amityco/ts-sdk 的常见问题

@amityco/ts-sdk 是什么?

Amity Social Cloud Typescript SDK

如何安装 @amityco/ts-sdk?

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

@amityco/ts-sdk 支持 TypeScript 吗?

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

@amityco/ts-sdk 支持 ESM 还是 CommonJS?

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

@amityco/ts-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@amityco/ts-sdk 可以商用吗?

@amityco/ts-sdk 使用 CC-BY-ND-4.0 许可。声明的许可为 CC-BY-ND-4.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@amityco/ts-sdk 的下载量有多少?

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

@amityco/ts-sdk 还在维护吗?

最新版本 7.33.1 发布于 2026/11/06(1 天前);至今共发布 2485 个版本,其中过去 12 个月发布了 492 个;首次发布于 2021/08/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。