> 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 SDK 使用与您的 Convai 账户关联的 API 密钥对每个会话进行身份验证。您只需在 Unity 编辑器中输入一次该密钥——它会保存在您的项目中，并在运行时自动使用。

{% stepper %}
{% step %}

#### 复制您的 API 密钥

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

{% step %}

#### 打开 Convai 配置窗口

在 Unity 编辑器菜单栏中，打开 **Convai > 账户**.

Convai 配置窗口会打开到“账户”部分。
{% endstep %}

{% step %}

#### 粘贴 API 密钥

将您的 API 密钥粘贴到 **API 密钥** 字段中。

该密钥会立即保存到 `Assets/Resources/ConvaiSettings.asset`。无需额外的保存步骤。
{% endstep %}

{% step %}

#### 验证密钥是否被接受

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

<figure><img src="/files/c443b79a36162c65bef639433a6d993d654af1e4" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
`Assets/Resources/ConvaiSettings.asset` 会将 API 密钥以明文形式存储。如果您的项目使用源代码管理，请根据团队的安全策略决定是否提交此文件。
{% endhint %}

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

SDK 会从 `ConvaiSettings` 通过 `ICredentialProvider` 接口读取密钥，然后再发起任何与 Convai 的连接。您无需在代码中手动传递密钥——SDK 会在启动时自动解析它。

对于不允许将密钥存储在源代码管理中的生产部署，请实现一个自定义的 `ICredentialProvider` ，以便从安全来源读取密钥。

{% 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:

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

The question should be specific, self-contained, and written in natural language.
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.
