前置准备(所有示例通用)
- 获取 PAT 令牌:Coze 后台 → 个人中心 → API 管理 → 创建个人访问令牌(
pat_xxxx) - bot_id:智能体发布 API 后,Bot 编辑页 URL 末尾数字
- 国内接口地址:
https://api.coze.cn/v3/chat - 核心区分:流式 SSE(AI 对话主流)、非流式一次性返回、多轮会话上下文
一、流式 SSE curl(最常用,对应 RESTful HTTP+SSE 长连接)
完整可直接执行命令
bash
运行
curl -N --location POST https://api.coze.cn/v3/chat \ -H "Authorization: Bearer pat_你的PAT密钥" \ -H "Content-Type: application/json" \ -H "Accept: text/event-stream" \ -d '{ "bot_id": "替换为你的bot_id", "user_id": "user_001", "conversation_id": "conv_00001", "stream": true, "auto_save_history": true, "additional_messages": [ { "role": "user", "content_type": "text", "content": "帮我查询南京今日天气,并给出出行穿搭建议" } ] }'关键参数解释
-N / --no-buffer:关闭 curl 输出缓存,SSE 分片实时打印,实现打字机效果(流式必备)Accept: text/event-stream:告知服务端客户端支持 SSE 事件流,开启分段推送"stream": true:开启 AI 增量流式输出conversation_id:固定 ID 维持云端会话上下文,多轮对话自动记忆历史
终端返回 SSE 数据流示例
plaintext
data: {"id":"chat_xxxx","type":"thought","delta":{"content":"需要调用天气插件获取南京气温"}} data: {"id":"chat_xxxx","type":"tool_call","delta":{"tool_name":"天气查询","params":"{\"city\":\"南京\"}"}} data: {"id":"chat_xxxx","type":"message","delta":{"content":"南京今日22~28℃,多云微风,"}} data: {"id":"chat_xxxx","type":"message","delta":{"content":"推荐短袖搭配薄防晒外套,适合短途骑行。"}} data: {"id":"chat_xxxx","type":"done"}二、多轮上下文会话 curl(复用conversation_id)
复用同一会话 ID,智能体自动读取上一轮天气信息,无需重复提问:
bash
运行
curl -N --location POST https://api.coze.cn/v3/chat \ -H "Authorization: Bearer pat_你的PAT密钥" \ -H "Content-Type: application/json" \ -H "Accept: text/event-stream" \ -d '{ "bot_id": "替换为你的bot_id", "user_id": "user_001", "conversation_id": "conv_00001", "stream": true, "auto_save_history": true, "additional_messages": [ { "role": "user", "content_type": "text", "content": "那今天适合户外骑行吗?" } ] }'三、非流式一次性返回(短问答,无 SSE)
去掉-N与Accept: text/event-stream,stream: false,等待 AI 生成完整后一次性输出 JSON:
bash
运行
curl --location POST https://api.coze.cn/v3/chat \ -H "Authorization: Bearer pat_你的PAT密钥" \ -H "Content-Type: application/json" \ -d '{ "bot_id": "替换为你的bot_id", "user_id": "user_001", "stream": false, "auto_save_history": true, "additional_messages": [ { "role": "user", "content_type": "text", "content": "你好,介绍下你的功能" } ] }'四、单行压缩版流式 curl(适合脚本复制)
bash
运行
curl -N -L POST https://api.coze.cn/v3/chat -H "Authorization: Bearer pat_xxx" -H "Content-Type: application/json" -H "Accept: text/event-stream" -d '{"bot_id":"123456","user_id":"u001","conversation_id":"conv001","stream":true,"auto_save_history":true,"additional_messages":[{"role":"user","content_type":"text","content":"讲一段简短小故事"}]}'五、调试专用 curl(-v 打印完整请求 / 响应日志)
排查 401 鉴权、流式失效、路由报错时使用:
bash
运行
curl -N -v --location POST https://api.coze.cn/v3/chat \ -H "Authorization: Bearer pat_你的PAT密钥" \ -H "Content-Type: application/json" \ -H "Accept: text/event-stream" \ -d '{ "bot_id": "替换为你的bot_id", "user_id": "user_001", "stream": true, "additional_messages": [{"role":"user","content_type":"text","content":"测试调试接口"}] }'六、配套常用辅助接口 curl 示例
1. 创建会话(v1/conversation/create)
手动新建独立会话,拿到专属 conversation_id:
bash
运行
curl --location POST https://api.coze.cn/v1/conversation/create \ -H "Authorization: Bearer pat_你的PAT密钥" \ -H "Content-Type: application/json" \ -d '{ "meta_data": {"client": "curl调试"}, "messages": [] }'2. 查询对话完整结果(v3/chat/retrieve)
流式对话结束后,拉取完整对话记录:
bash
运行
curl --location 'https://api.coze.cn/v3/chat/retrieve?conversation_id=会话ID&chat_id=对话ID' \ -H "Authorization: Bearer pat_你的PAT密钥" \ -H "Content-Type: application/json"七、核心关键点(区分传统本地函数调用)
- 全程远程 HTTP 网络请求,不是本机内存函数调用,必须鉴权 Token、网络域名;
- 流式依赖
-N与 SSE 头,逐分片实时输出,本地函数一次性阻塞返回; conversation_id云端持久存储对话上下文,普通本地函数无会话记忆能力;- 单次请求自动执行:意图识别→插件工具调用→知识库检索→LLM 生成完整智能体链路。
八、常见报错排查
- 401 Unauthorized:PAT 密钥错误、Bearer 后缺少空格
- 404 Not Found:智能体未发布 API、bot_id 填写错误
- 无实时打字输出:缺失
-N或Accept: text/event-stream请求头 - 上下文丢失:每次请求
conversation_id不一致,每次新建会话