> 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 编辑器中进行本地开发——迭代场景、运行示例场景，或在仅由您控制的机器上测试。

{% hint style="warning" %}
API 密钥模式仅用于本地开发，不适用于您分发的构建版本。以 API 密钥模式生成的玩家构建会在随附的 `ConvaiSettings` 资源中。在构建任何您计划发布的内容之前，请阅读 [身份验证](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication.md) 并切换到 Auth Token 模式。
{% endhint %}

{% stepper %}
{% step %}

#### 复制您的 API 密钥

登录到您的 [Convai 仪表板](https://convai.com)，转到 **账户设置**，并复制您的 API 密钥。
{% endstep %}

{% step %}

#### 打开“凭据”部分

在 Unity 编辑器菜单栏中，打开 **Convai > 设置** （或 **Edit > Project Settings > Convai SDK**），然后选择 **Credentials** 部分。将 **身份验证模式** 设置为 **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 编辑器首次加载时，SDK 会自动将其迁移为混淆格式。

### 在发布前切换到 Auth Token 模式

API 密钥模式没有等同于服务器签发的短期凭证——解锁您的 Convai 项目的同一个账户密钥会包含在构建中。对于任何分发给测试人员、玩家或最终用户的构建，请改为使用 Auth Token 模式；在该模式下，构建处理器会在构建前移除账户密钥，而您控制的服务器会在连接时签发短期令牌。

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

{% content-ref url="/pages/fdc49f930fecd7fb00d337f46a900594f8f26fb5" %}
[自定义凭证提供器](/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" %}
[导入并运行示例场景](/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
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=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
