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

Coze API Go SDK 是由扣子官方提供的一个 SDK 工具包。你可以通过 Go SDK 将 Coze 的 OpenAPI 集成到你的应用程序中。Go SDK 支持 Coze 提供的所有官方鉴权方法，具有高度灵活性和高可用性，可以显著提升 OpenAPI 的使用效率。通过使用访问密钥初始化 SDK，可以高效、安全地通过 OpenAPI 访问你的 AI 智能体。
Coze API Go SDK 遵循用户友好的设计风格，可简化你的开发工作，适合所有技能水平开发者使用。
## 源码地址 {#5bb96dfa}
访问 [GitHub](https://github.com/coze-dev/coze-go) 获取 Coze API Go SDK 源码。
:::notice 注意
从扣子 GitHub 仓库获取的源码和示例代码中，`base_url` 默认设置为域名 `CozeComBaseURL`，使用源码前应手动将其修改为域名 `CozeCnBaseURL`。
:::
## 示例代码 {#bdea57ff}
Coze Go SDK 提供各种授权方式、各种使用方式的示例代码，便于开发者直接参考使用。
:::tip 说明
* Coze Go SDK 支持扣子所有 OpenAPI，对应的 API 文档可参考 [API 介绍](/developer_guides/coze_api_overview)。
* 示例代码将持续更新和补充，各种场景的 Coze Go SDK **最新版本**示例代码可参考 [GitHub](https://github.com/coze-dev/coze-go)。
:::
常见场景的示例代码及说明如下：
<!-- @cols-width: 121,280,312 -->
| | | | \
|**模块** |**示例文件** |**说明** |
|---|---|---|
| | | | \
|授权 |[pat_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/auth/token/main.go) |通过个人访问密钥实现 OpenAPI 鉴权。 |
|^^| | | \
| |[web_oauth_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/auth/web_oauth/main.go) |通过 OAuth 授权码方式实现授权与 OpenAPI 鉴权。 |
|^^| | | \
| |[jwt_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/auth/jwt_oauth/main.go) |通过 OAuth JWT 方式实现授权与 OpenAPI 鉴权。 |
|^^| | | \
| |[pkce_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/auth/pkce_oauth/main.go) |通过 OAuth PKCE 方式实现授权与 OpenAPI 鉴权。 |
|^^| | | \
| |[device_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/auth/device_oauth/main.go) |通过 OAuth 设备码方式实现授权与 OpenAPI 鉴权。 |
| | | | \
|对话 |[non_stream_chat_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/chats/chat/main.go) |发起对话，响应方式为非流式响应。 |
|^^| | | \
| |[stream_chat_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/chats/stream/main.go) |发起对话，响应方式为流式响应。 |
|^^| | | \
| |[multi_modal_chat_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/chats/chat_with_image/main.go) |发起对话，对话中上传文件，并发送多模态内容。 |
|^^| | | \
| |[submit_tool_output_example.go](https://github.com/coze-dev/coze-go/tree/main/examples/chats/submit_tool_output/main.go) |对话时调用端插件。 |
| | | | \
|会话和消息 |[conversation_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/conversations/crud/main.go) |创建对话、向对话中添加消息以及清除对话内容等。 |
|^^| | | \
| |[list_conversation_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/conversations/list/main.go) |查询对话列表。 |
|^^| | | \
| |[create_update_delete_message_example.go](https://github.com/coze-dev/coze-go/tree/main/examples/conversations/messages/crud/main.go) |创建、更新和删除消息 |
| | | | \
|工作流 |[non_stream_run_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/workflows/runs/create/main.go) |运行工作流，响应方式为非流式响应。 |
|^^| | | \
| |[stream_run_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/workflows/runs/stream/main.go) |运行工作流，响应方式为流式响应，且工作流中包含问答节点。 |
|^^| | | \
| |[async_workflow_run_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/workflows/runs/async_run/main.go) |异步运行工作流，并获取工作流运行结果。 |
|^^| | | \
| |[workflow_stream_chat_example.go](https://github.com/coze-dev/coze-go/tree/main/examples/workflows/chat/stream/main.go) |运行对话流。 |
| | | | \
|智能体管理 |[publish_bot_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/bots/publish/main.go) |创建一个草稿状态的智能体，更新智能体，并发布智能体为 API 服务。 |
| | | | \
|WebSocket 语音 |[websockets_audio_chat_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/websockets/chat/audio_chat/main.go) |WebSocket 语音通话。 |
|^^| | | \
| |[websockets_speech_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/websockets/speech/main.go) |语音合成，将文本转为语音，并将生成的语音保存为音频文件。 |
|^^| | | \
| |[websockets_transcriptions_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/websockets/transcriptions/main.go) |语音识别，将指定音频文件转录为文本。 |
| | | | \
|语音 |[audio_rooms_create_main.go](https://github.com/coze-dev/coze-go/blob/main/examples/audio/rooms/create/main.go) |RTC 音视频通话场景中，创建音视频房间。 |
|^^| | | \
| |[audio_speech_create_example.go](https://github.com/coze-dev/coze-go/tree/main/examples/audio/speech/create/main.go) |语音合成，将文本转为语音，并将生成的语音保存为音频文件。 |
|^^| | | \
| |[audio_transcription_create_example.go](https://github.com/coze-dev/coze-go/tree/main/examples/audio/speech/transcription/main.go) |语音识别，将指定音频文件转录为文本。 |
|^^| | | \
| |[audio_voices_clone_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/audio/voices/clone/main.go) |克隆音色。 |
|^^| | | \
| |[audio_voices_list_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/audio/voices/list/main.go) |查询音色列表。 |
| | | | \
|工作空间 |[list_workspace_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/workspaces/list/main.go) |查询所有工作空间列表。 |
| | | | \
|知识库 |[dataset_documents_example.go](https://github.com/coze-dev/coze-go/tree/main/examples/datasets) |知识库管理，包含： |\
| | | |\
| | |* 创建、查询、更新和删除知识库。 |\
| | |* 上传文本和图片类型的知识库文件。 |\
| | |* 更新图片知识库的描述。 |\
| | |* 查看知识库图片列表。 |
| | | | \
|文件管理 |[files_examples.go](https://github.com/coze-dev/coze-go/blob/main/examples/files/main.go) |文件上传。 |
| | | | \
|文件夹 |[folders_examples.go](https://github.com/coze-dev/coze-go/tree/main/examples/folders) |查询文件夹列表和文件夹详情。 |
| | | | \
|异常处理 |[handle_error_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/client/error/main.go) |处理 API 异常。 |
| | | | \
|客户端管理 |[init_client_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/client/init/main.go) |修改日志级别。 |
|^^|^^| | \
| | |初始化客户端。 |
| | | | \
|获取日志 |[get_log_id_example.go](https://github.com/coze-dev/coze-go/blob/main/examples/client/log/main.go) |获取日志。 |

## 异常处理 {#bb8edbfc}
当调用 API 失败时，SDK 会返回 [Error](https://github.com/coze-dev/coze-go/blob/main/error.go#L8C1-L8C5)。当鉴权失败的时候，SDK 会抛出 [AuthError](https://github.com/coze-dev/coze-go/blob/main/error.go#L8C1-L8C5)。开发者可以根据自己的需求捕获对应的异常，其中 [Error](https://github.com/coze-dev/coze-go/blob/main/error.go#L8C1-L8C5) 会获取到异常接口的错误码，API 错误码的相关说明及处理方式可参考[错误码](/developer_guides/coze_error_codes)。
