前置准备所有示例通用获取 PAT 令牌Coze 后台 → 个人中心 → API 管理 → 创建个人访问令牌pat_xxxxbot_id智能体发布 API 后Bot 编辑页 URL 末尾数字国内接口地址https://api.coze.cn/v3/chat核心区分流式 SSEAI 对话主流、非流式一次性返回、多轮会话上下文一、流式 SSE curl最常用对应 RESTful HTTPSSE 长连接完整可直接执行命令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 数据流示例plaintextdata: {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-streamstream: false等待 AI 生成完整后一次性输出 JSONbash运行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_idbash运行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会话IDchat_id对话ID \ -H Authorization: Bearer pat_你的PAT密钥 \ -H Content-Type: application/json七、核心关键点区分传统本地函数调用全程远程 HTTP 网络请求不是本机内存函数调用必须鉴权 Token、网络域名流式依赖-N与 SSE 头逐分片实时输出本地函数一次性阻塞返回conversation_id云端持久存储对话上下文普通本地函数无会话记忆能力单次请求自动执行意图识别→插件工具调用→知识库检索→LLM 生成完整智能体链路。八、常见报错排查401 UnauthorizedPAT 密钥错误、Bearer 后缺少空格404 Not Found智能体未发布 API、bot_id 填写错误无实时打字输出缺失-N或Accept: text/event-stream请求头上下文丢失每次请求conversation_id不一致每次新建会话