DeepSeek Node.js 接入
当前最省事的方式是直接使用 OpenAI Node SDK,只修改 apiKey、baseURL 和模型名。
安装
npm install openai
最小示例
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: 'https://api.deepseek.com',
})
const response = await client.chat.completions.create({
model: 'deepseek-flash',
messages: [
{ role: 'user', content: '给我三个 Node.js 日志设计建议' },
],
})
console.log(response.choices[0].message.content)
Express 中使用
不要让浏览器直接拿 DeepSeek Key。应该由你的服务端持有 Key:
app.post('/api/ai', async (req, res) => {
const response = await client.chat.completions.create({
model: 'deepseek-flash',
messages: [{ role: 'user', content: req.body.prompt }],
})
res.json({ text: response.choices[0].message.content })
})
环境变量
.env:
DEEPSEEK_API_KEY=sk-xxxxxxxx
.gitignore:
.env
.env.*
常见误区
- 不要在 Vue / React 前端直接写真实 Key。
- 新项目优先使用
deepseek-flash,不要照抄过期教程里的旧模型名。 - Agent 场景出现 thinking 相关错误时,优先确认客户端对
reasoning_content的处理。
官方参考:首次调用 API