Updated 2026-10-11
How to Use DeepSeek in Node.js — TypeScript, Streaming, Tool Use
DeepSeek V4.1 Flash is OpenAI-compatible and native multimodal, so Node.js applications can keep the official openai package while moving to the exact Model ID `deepseek-flash`. Use it for normal text traffic and image-aware workflows without maintaining a separate experimental Vision default.
1. Install and configure
The official openai npm package works with DeepSeek via base_url override. Put your key in .env — never commit it, and never ship it to the browser.
npm install openai zod
echo "DEEPSEEK_API_KEY=sk-..." >> .env.local2. First chat completion in TypeScript
Instantiate the client once and reuse it. Set baseURL to the DeepSeek endpoint and use `deepseek-flash` for new configurations. The model accepts native text and image input; older V4 IDs can stay temporarily where saved integrations still reference them.
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: "https://api.deepseek.com/v1",
});
export async function ask(question: string): Promise<string> {
const res = await client.chat.completions.create({
model: "deepseek-flash",
messages: [
{ role: "system", content: "You are a concise technical assistant." },
{ role: "user", content: question },
],
temperature: 0.2,
});
return res.choices[0].message.content ?? "";
}3. Streaming from a Next.js route handler
For a chat UI you want tokens to arrive as they are produced. The OpenAI SDK returns an async iterable when stream is true. You can pipe that directly into a Next.js Response as a ReadableStream.
// app/api/chat/route.ts
import OpenAI from "openai";
export const runtime = "nodejs";
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: "https://api.deepseek.com/v1",
});
export async function POST(req: Request) {
const { messages } = await req.json();
const stream = await client.chat.completions.create({
model: "deepseek-flash",
messages,
stream: true,
});
const encoder = new TextEncoder();
const body = new ReadableStream({
async start(controller) {
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content ?? "";
if (delta) controller.enqueue(encoder.encode(delta));
}
controller.close();
},
});
return new Response(body, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}4. Function calling with Zod-validated arguments
DeepSeek V4 handles OpenAI-style tool use reliably enough to run real agents. Declare the tool schema, dispatch on the returned tool_call, and always validate the arguments — the model can and will occasionally hallucinate fields.
import { z } from "zod";
const GetWeatherArgs = z.object({ city: z.string().min(1) });
const tools = [{
type: "function" as const,
function: {
name: "get_weather",
description: "Get the current weather for a city",
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
}];
const res = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "Weather in Tokyo?" }],
tools,
});
const call = res.choices[0].message.tool_calls?.[0];
if (call?.function.name === "get_weather") {
const args = GetWeatherArgs.parse(JSON.parse(call.function.arguments));
// … execute tool, feed result back as a { role: "tool" } message …
}5. Cost control for Node.js workloads
Official Pro API pricing: Off-peak: $0.66 input / $1.98 output per 1M tokens (cache-miss input); Peak: $1.32 input / $3.96 output per 1M tokens (cache-miss input). Peak hours are 09:00–12:00 and 14:00–18:00 Beijing time (01:00–04:00 and 06:00–10:00 UTC), Monday–Friday, excluding Chinese public holidays. All other hours, weekends, and Chinese public holidays are off-peak. Cached input is billed separately; see the complete pricing table.
Official Flash API pricing: Off-peak: $0.15 input / $0.6 output per 1M tokens (cache-miss input); Peak: $0.3 input / $1.2 output per 1M tokens (cache-miss input). Peak hours are 09:00–12:00 and 14:00–18:00 Beijing time (01:00–04:00 and 06:00–10:00 UTC), Monday–Friday, excluding Chinese public holidays. All other hours, weekends, and Chinese public holidays are off-peak. Cached input is billed separately; see the complete pricing table.
Use maxTokens to cap runaway answers, summarise old turns on long chats, and cache retrieval chunks rather than regenerating them. If your workload is large, compare the quota and access terms of current plans on /pricing.
6. Production checklist
Never expose the API key to the browser — always proxy through a server route. Set a request timeout (AbortController) and retry 429 / 5xx with exponential backoff.
For structured output, combine response_format: { type: "json_object" } with a Zod schema parse on the server so you fail fast on malformed responses.
Log model, promptTokens, completionTokens, and latency per request. A simple daily aggregate catches cost regressions before the invoice does.
FAQ
Can I call DeepSeek directly from the browser?
No. Your API key would be exposed. Always call it from a Node.js server, serverless function, or Next.js route handler.
Does this work with Cloudflare Workers / Bun / Deno?
Yes. The openai SDK ships ESM builds and works on any runtime with fetch. Edge runtimes work, but prefer Node.js for long-lived streams.
Which model name for Node.js?
Use the exact Model ID `deepseek-flash` for new text and image traffic. Retain an older V4 ID only as a temporary compatibility entry.
Where do I find the cheapest API key?
/pricing lists inventory-backed DeepSeek access plans when available, with the delivered Base URL, quota, and access term shown before checkout.
Can I use LangChain / LlamaIndex / Vercel AI SDK?
Yes. Any library that accepts an OpenAI-compatible baseURL can point at DeepSeek.
If you already ship Node.js or Next.js apps, V4.1 Flash remains a small configuration change: keep the baseURL, add `deepseek-flash`, verify text and image requests, and migrate saved integrations before removing their older IDs.
Related model comparisons
Continue from this guide into structured DeepSeek-first comparison pages with model tables, routing advice, and pricing context.