> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/getting-started/configure-api-key.md).

# 配置 API 密钥

在 Unity 编辑器中输入并验证用于本地开发的 Convai API 密钥，这样 Convai 角色就能在你构建场景时进行身份验证。

Convai Unity SDK 的 **API 密钥** 身份验证模式会直接从项目保存的设置中读取与您的 Convai 账户关联的 API 密钥。您可以在此页面输入该密钥，用于在 Unity Editor 中进行本地开发，例如迭代场景、运行示例场景，或在仅由您控制的设备上进行测试。

{% hint style="warning" %}
API 密钥模式适用于本地开发，不适用于要分发的构建版本。使用 API 密钥模式生成的玩家构建版本会在随附的 `ConvaiSettings` 资源中包含您的账户 API 密钥。该密钥经过混淆，但未加密。在构建任何准备发布的内容之前，请阅读 [身份验证](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication.md) 并切换到身份验证令牌模式。
{% endhint %}

{% stepper %}
{% step %}

#### 复制 API 密钥

登录 [Convai 控制面板](https://convai.com)，前往 **账户设置**，然后复制您的 API 密钥。
{% endstep %}

{% step %}

#### 打开凭据部分

在 Unity Editor 菜单栏中，打开 **Convai > Settings** （或 **Edit > Project Settings > Convai SDK**），然后选择 **凭据** 部分。将 **身份验证模式** 保留为 **API 密钥**（默认设置）。
{% endstep %}

{% step %}

#### 粘贴并验证 API 密钥

将您的 API 密钥粘贴到 **API 密钥** 字段中，然后选择 **验证并保存**。Convai 会检查密钥；如果密钥有效，SDK 就会将其保存到项目中。

按钮旁的状态标记会显示结果： **密钥有效** 表示 Convai 接受了该密钥；如果未接受，则会显示类似 **密钥无效** 的错误消息。
{% endstep %}

{% step %}

#### 确认密钥已被接受

运行 **GameObject > Convai > Validate Scene Setup**。如果缺少 API 密钥，验证器对话框中会显示警告。如果没有出现 API 密钥警告，说明密钥配置正确。

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2Fgit-blob-f8d3457881d188638d6edb0ac6540e24d701b7f0%2Fimage.png?alt=media" alt="Scene validator dialog with no missing-API-key warning"><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

### 密钥在运行时的使用方式

SDK 会通过 `ConvaiSettings` 和 `ICredentialProvider` 接口读取密钥，然后才会与 Convai 建立连接。您无需在代码中手动传递密钥——SDK 会在启动时自动解析密钥。

`Assets/Resources/ConvaiSettings.asset` 会以混淆形式（XOR 加 Base64）存储密钥，而非加密存储——任何拥有 SDK 源代码的人都能还原密钥。请根据团队的安全策略决定是否将此文件提交到源代码管理中。如果项目之前以明文形式存储了密钥，Unity Editor 首次加载时，SDK 会自动将其迁移为混淆格式。

### 发布前切换到身份验证令牌模式

API Key 模式无法提供与服务器签发的短期凭证相当的机制——解锁 Convai 项目的同一个账户密钥会嵌入构建产物中。对于要分发给测试人员、玩家或最终用户的任何构建，请改用 Auth Token 模式：构建处理器会在构建前移除账户密钥，并由你控制的服务器在连接时签发短期令牌。

{% content-ref url="/pages/5c8198db09e24a6ea14c4fe27e4c76651e64ccca" %}
[身份验证](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication.md)
{% endcontent-ref %}

{% content-ref url="/pages/fdc49f930fecd7fb00d337f46a900594f8f26fb5" %}
[自定义凭据提供程序](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/advanced-topics/custom-providers/custom-credential-provider.md)
{% endcontent-ref %}

### 后续步骤

设置好 API 密钥后，导入一个示例场景，以确认 SDK 正常工作，然后再创建自己的场景。

{% content-ref url="/pages/339e02ef9174c5dbcb43ce3f1d3e39f9753fd9ae" %}
[导入并运行示例场景](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/getting-started/import-and-run-sample-scenes.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/getting-started/configure-api-key.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
