简介:本文通过分步骤讲解、配图示例和错误排查指南,系统介绍如何使用Postman调用DeepSeek API接口,涵盖环境配置、请求构造、响应解析等核心环节,适合开发者快速上手并解决实际调用问题。
在AI模型接口调用场景中,Postman凭借其可视化操作界面、请求历史管理、环境变量配置等特性,成为开发者测试API的首选工具。相比直接编写代码调用,Postman能显著降低调试成本,尤其适合接口文档验证阶段。本文以DeepSeek官方API为例,详细拆解从环境搭建到完整请求发送的全流程。
调用DeepSeek API需完成以下前置步骤:
Client ID和Client Secret(注意:部分接口需单独申请白名单)⚠️ 安全提示:API Key等同于账户密码,切勿直接提交至公开代码仓库
(通过
{"base_url": "https://api.deepseek.com/v1","api_key": "your_actual_api_key_here","model": "deepseek-chat"}
{{base_url}}语法实现变量复用)DeepSeek API主要支持两种调用方式:
| 请求类型 | 适用场景 | 典型参数 |
|————-|————-|————-|
| POST /chat/completions | 对话生成 | messages, temperature |
| POST /embeddings | 文本嵌入 | input, encoding_format |
本文以对话接口为例展开说明。
{{base_url}}/chat/completions
Content-Type: application/jsonAuthorization: Bearer {{api_key}}
{"model": "{{model}}","messages": [{"role": "user","content": "用Python实现快速排序"}],"temperature": 0.7,"max_tokens": 200}
成功响应示例:
{"id": "chatcmpl-12345","object": "chat.completion","created": 1678901234,"model": "deepseek-chat","choices": [{"index": 0,"message": {"role": "assistant","content": "def quick_sort(arr):\n if len(arr) <= 1:\n return arr\n ..."},"finish_reason": "stop"}],"usage": {"prompt_tokens": 15,"completion_tokens": 120,"total_tokens": 135}}
关键字段说明:
choices[0].message.content:模型生成的回复内容finish_reason:结束原因(stop/length/content_filter)usage:计费相关的token消耗统计{"error":{"code":"invalid_authentication"}}Authorization格式不正确
{"error": {"code": "invalid_request","message": "messages must be a non-empty array"}}
messages字段)model字段值)
setTimeout(() => {}, 1000); // 1秒延迟
pm.test("Status code is 200", function() {pm.response.to.have.status(200);});pm.test("Response contains assistant role", function() {const jsonData = pm.response.json();pm.expect(jsonData.choices[0].message.role).to.eql("assistant");});
production/test)密钥管理:
__postman_environment__变量存储敏感信息数据传输安全:
日志审计:
通过Postman完成接口验证后,开发者可基于生成的cURL命令快速迁移至代码实现。例如Python调用示例:
import requestsurl = "https://api.deepseek.com/v1/chat/completions"headers = {"Content-Type": "application/json","Authorization": "Bearer your_api_key"}data = {"model": "deepseek-chat","messages": [{"role": "user", "content": "Hello"}]}response = requests.post(url, headers=headers, json=data)print(response.json())
本文作为系列教程的第一篇,重点解决了基础调用问题。后续将深入探讨流式响应处理、多模型对比测试等高级主题。建议读者结合DeepSeek官方文档与Postman官方指南进行交叉验证,确保调用稳定性。”