> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstream.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 认证

> 如何对 API 请求进行签名认证

## 签名请求

ChainStream 使用 API 密钥对所有 API 调用进行认证。根据工作区环境类型，**基础 API URL** 将是以下之一：

* 主网: `https://api.chainstream.io/`

每个 API 请求必须包含以下请求头：

* `Authorization` - 此值应设置为 `Bearer <Access Token>`。访问令牌是一个 Base64 编码的 JSON Web Token (JWT)。

## 生成 JWT 令牌

你可以使用以下代码生成 JWT 令牌。`scope` 参数是可选的，用于控制特定 API 模块的访问权限。

### 可用的 Scope

当前支持以下 scope：

* `webhook.read` - Webhook API 端点的读取权限
* `webhook.write` - Webhook API 端点的写入权限
* `kyt.read` - KYT（了解您的交易）API 端点的读取权限
* `kyt.write` - KYT API 端点的写入权限

<Note>
  如果不指定 scope，令牌将可以访问所有通用 API 端点。仅在访问受保护的 Webhook 或 KYT 端点时才需要 scope。
</Note>

### JavaScript

**示例 1：不使用 scope（通用 API 访问）**

```javascript theme={null}
import { AuthenticationClient } from 'auth0';

const auth0Client = new AuthenticationClient({
    domain: `dex.asia.auth.chainstream.io`,
    clientId: 'your client id',
    clientSecret: 'your client secret'
  });

const response = await auth0Client.oauth.clientCredentialsGrant({
      audience: 'https://api.dex.chainstream.io'
    });

return response.data.access_token;
```

**示例 2：使用 Webhook 访问权限**

```javascript theme={null}
import { AuthenticationClient } from 'auth0';

const auth0Client = new AuthenticationClient({
    domain: `dex.asia.auth.chainstream.io`,
    clientId: 'your client id',
    clientSecret: 'your client secret'
  });

const response = await auth0Client.oauth.clientCredentialsGrant({
      audience: 'https://api.dex.chainstream.io',
      scope: 'webhook.read webhook.write'
    });

return response.data.access_token;
```

**示例 3：使用 KYT 访问权限**

```javascript theme={null}
import { AuthenticationClient } from 'auth0';

const auth0Client = new AuthenticationClient({
    domain: `dex.asia.auth.chainstream.io`,
    clientId: 'your client id',
    clientSecret: 'your client secret'
  });

const response = await auth0Client.oauth.clientCredentialsGrant({
      audience: 'https://api.dex.chainstream.io',
      scope: 'kyt.read kyt.write'
    });

return response.data.access_token;
```

**示例 4：使用多个 scope**

```javascript theme={null}
import { AuthenticationClient } from 'auth0';

const auth0Client = new AuthenticationClient({
    domain: `dex.asia.auth.chainstream.io`,
    clientId: 'your client id',
    clientSecret: 'your client secret'
  });

const response = await auth0Client.oauth.clientCredentialsGrant({
      audience: 'https://api.dex.chainstream.io',
      scope: 'webhook.read webhook.write kyt.read kyt.write'
    });

return response.data.access_token;
```

## 使用 SDK

生成 JWT 令牌后，你可以使用它来初始化 SDK：

### JavaScript SDK

```javascript theme={null}
import { ChainStreamClient } from "@chainstream-io/sdk";

// 从上一步生成的令牌
const dex = new ChainStreamClient("your-jwt-token");
```

<Note>
  请确保将示例中的占位值替换为你的实际 API 凭据。
</Note>
