DeepSeek Free code examples in Python and JavaScript
Low-cost chat and reasoning models, OpenAI-compatible.
WHAT THIS PAGE COVERS
Reading the reasoning trace separately from the answer.
deepseek-flash
openai
Model ids change. If a sample returns a 404 on the model, that is the first thing to check against the official docs.
The OpenAI SDK, pointed at DeepSeek. There is also an Anthropic-format endpoint at /anthropic if your code already speaks that.
pip install openai
export DEEPSEEK_API_KEY="sk-..." # macOS / Linux
setx DEEPSEEK_API_KEY "sk-..." # Windows, then open a new shell
base_url has no /v1 suffix — DeepSeek serves the OpenAI routes from the domain root. Adding /v1 yourself is the usual first 404.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com",
)
response = client.chat.completions.create(
model="deepseek-flash",
messages=[
{"role": "system", "content": "You are a concise technical assistant."},
{"role": "user", "content": "Explain a database index in two sentences."},
],
)
print(response.choices[0].message.content)
deepseek-v4-pro thinks before answering, and you control how hard. Budget for it: reasoning tokens are generated, billed and counted against max_tokens even though the user never sees them.
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "Plan a zero-downtime migration."}],
extra_body={
"thinking": {"type": "enabled"},
"reasoning_effort": "high", # trade cost against depth
},
)
print(response.choices[0].message.content)
# Reasoning tokens are counted here even though they are not in the text.
print(response.usage.completion_tokens, "output tokens billed")
Compatible does not mean identical, and none of these announce themselves.
# 1. MODEL IDS MOVE, AND THE OLD ONES DO NOT ERROR HELPFULLY.
# deepseek-chat / deepseek-reasoner are legacy names for retired
# models. Keep the id in config so one edit fixes every call site.
MODEL = os.environ.get("DEEPSEEK_MODEL", "deepseek-flash")
# 2. NO /v1 ON THE BASE URL. https://api.deepseek.com, not .../v1.
# 3. VENDOR-SPECIFIC PARAMETERS GO IN extra_body. The OpenAI SDK strips
# anything it does not recognise from the top level, so a "thinking"
# argument passed directly is silently dropped — you get a normal
# answer, no error, and no reasoning.
# 4. IT ALSO SPEAKS ANTHROPIC. Point the anthropic SDK at
# https://api.deepseek.com/anthropic if that is the client you have.
The OpenAI SDK, pointed at DeepSeek. Server-side only.
npm install openai
export DEEPSEEK_API_KEY="sk-..."
baseURL with no /v1 — DeepSeek serves the OpenAI routes from the domain root.
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: "system", content: "You are a concise technical assistant." },
{ role: "user", content: "Explain a database index in two sentences." },
],
});
console.log(response.choices[0].message.content);
Vendor parameters the SDK does not know about are passed as extra body fields. Sent at the top level they are stripped in silence.
const response = await client.chat.completions.create(
{
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "Plan a zero-downtime migration." }],
},
{
body: {
thinking: { type: "enabled" },
reasoning_effort: "high",
},
},
);
console.log(response.choices[0].message.content);
// Reasoning tokens are billed even though they are not in the text.
console.log(response.usage.completion_tokens, "output tokens billed");
Four things, none of which fail loudly.
// 1. MODEL IDS MOVE. deepseek-chat / deepseek-reasoner are legacy names
// for retired models. Keep the current id in config.
const MODEL = process.env.DEEPSEEK_MODEL ?? "deepseek-flash";
// 2. NO /v1 ON THE BASE URL.
// 3. VENDOR PARAMETERS GO IN THE REQUEST OPTIONS' body, not the params
// object — the SDK drops unknown top-level keys without complaint,
// so you get a normal answer and no reasoning.
// 4. IT ALSO SPEAKS ANTHROPIC, at https://api.deepseek.com/anthropic,
// if that is the client you already have.
QUESTIONS
Which languages are the DeepSeek examples in?
Python and JavaScript. Every sample runs as pasted and reads its credentials from an environment variable — no key is ever written into the code.
Which package do I install for DeepSeek?
The samples use openai. The install step on each language tab gives the exact command.
Which DeepSeek model do these examples use?
deepseek-flash, checked against the official documentation on 25 September 2026. Model ids change often; if a call returns a model-not-found error, check the current list in the official docs.
Where is the official DeepSeek documentation?
https://api-docs.deepseek.com/ — it is linked at the bottom of this page, and it is the authority whenever it disagrees with a sample here.
ALSO IN LANGUAGE MODELS
The authority
Official DeepSeek documentation
Model names, pricing and rate limits change faster than any page here can. When this page and the official docs disagree, the official docs are right.
https://api-docs.deepseek.com/