> 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 Editor 中输入一次此密钥——它会存储在您的项目中，并在运行时自动使用。

{% stepper %}
{% step %}

#### 复制您的 API 密钥

登录您的 Convai 仪表板，网址为 <code class="expression">space.vars.dashboard\_url</code>，前往 **账户设置**，然后复制您的 API 密钥。
{% endstep %}

{% step %}

#### 打开 Credentials（凭据）部分

在 Unity Editor 菜单栏中，打开 **Convai > 设置** （或者 **编辑 > 项目设置 > Convai SDK**）。SDK 设置页面会打开，其中包含一个 **凭据** 用于输入 API 密钥的部分。

**Convai > 账户** 仍会显示您的套餐、配额使用情况以及返回设置的链接，但不再有可编辑的 API 密钥字段。
{% endstep %}

{% step %}

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

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

按钮旁的状态徽章会报告结果： **密钥有效** 当 Convai 接受该密钥时，或者显示类似以下的错误消息： **密钥无效** 当它不被接受时。
{% endstep %}

{% step %}

#### 验证该密钥已被接受

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

<figure><img src="/files/c443b79a36162c65bef639433a6d993d654af1e4" alt="Scene validator dialog with no missing-API-key warning"><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
`Assets/Resources/ConvaiSettings.asset` 以混淆方式（XOR 加 Base64）存储 API 密钥，而不是加密——任何拥有 SDK 源代码的人都可以将其还原。是否将此文件提交到源代码管理，应根据您团队的安全策略决定。如果项目之前以纯文本存储了密钥，Unity Editor 首次加载时，SDK 会自动将其迁移为混淆格式。
{% endhint %}

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

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

对于在生产环境中部署、且不适合将密钥存储在源代码管理中的情况，请在运行时从安全来源提供凭据，而不要依赖 `ConvaiSettings`.

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