Automation & toolsAny device8 min read

How to Call the DeepSeek API With the OpenAI Python and JavaScript SDK in 2026

Install openai, set base_url to https://api.deepseek.com and model to deepseek-flash. V4.1 Flash costs $0.30 in, $1.20 out per 1M tokens at peak.

On this page
  1. What you need
  2. Python
  3. JavaScript and TypeScript
  4. curl
  5. Thinking on or off
  6. What it costs
  7. The same code on other hosts
  8. Call DeepSeek through Off Grid AI Boost
  9. Not working? Symptom, check, fix
  10. FAQ

To call the DeepSeek API with the OpenAI SDK, install openai, create the client with base_url="https://api.deepseek.com" and your DEEPSEEK_API_KEY, then call chat.completions.create with model="deepseek-flash". DeepSeek V4.1 Flash costs $0.30 in, $1.20 out per million tokens at peak, half that off-peak. Boost runs DeepSeek on a flat plan from $4.99 a month.

See the Boost plans

What you need

  • A DeepSeek key from platform.deepseek.com/api_keys, saved as DEEPSEEK_API_KEY.
  • The base URL https://api.deepseek.com, from DeepSeek’s API docs.
  • The model name deepseek-flash, served by DeepSeek-V4.1-Flash-1017. deepseek-v4-pro is the bigger model.
export DEEPSEEK_API_KEY=<your key>

Python

pip3 install openai

This is DeepSeek’s own sample from its quick start:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get('DEEPSEEK_API_KEY'),
    base_url="https://api.deepseek.com",
)

response = client.chat.completions.create(
    model="deepseek-flash",
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Hello"},
    ],
    stream=False,
    reasoning_effort="high",
    extra_body={"thinking": {"type": "enabled"}},
)

print(response.choices[0].message.content)

reasoning_effort is a normal SDK argument. thinking isn’t part of OpenAI’s API, so the Python SDK needs it inside extra_body; pass it as a plain argument and Python raises a TypeError.

Stream the reply

stream = client.chat.completions.create(
    model="deepseek-flash",
    messages=[{"role": "user", "content": "Explain base URLs in two sentences."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

JavaScript and TypeScript

npm install openai

Save this as deepseek.mjs and run node deepseek.mjs:

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 helpful assistant" },
    { role: "user", content: "Hello" },
  ],
  reasoning_effort: "high",
  thinking: { type: "enabled" },
});

console.log(response.choices[0].message.content);

The Node SDK sends extra fields like thinking as they are. Its README says it doesn’t validate the request at runtime. In TypeScript, put // @ts-expect-error on the line above thinking so the compiler accepts it.

curl

From DeepSeek’s quick start:

curl https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
  -d '{
        "model": "deepseek-flash",
        "messages": [
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
        ],
        "thinking": {"type": "enabled"},
        "reasoning_effort": "high",
        "stream": false
      }'

Thinking on or off

DeepSeek’s thinking mode guide says thinking is on by default at high effort.

  • Turn it off for quick, cheap replies: extra_body={"thinking": {"type": "disabled"}} in Python, thinking: { type: "disabled" } in JavaScript.
  • Set the effort with reasoning_effort: low, high or max.
  • Read the reasoning from response.choices[0].message.reasoning_content, next to content.
  • Skip temperature in thinking mode. DeepSeek ignores it, along with presence_penalty and frequency_penalty.

For simple calls like classification or short rewrites, turning thinking off skips the reasoning step.

What it costs

From DeepSeek’s pricing page, per million tokens:

deepseek-flash Peak Off-peak
Input, cache hit $0.006 $0.003
Input, cache miss $0.30 $0.15
Output $1.20 $0.60

Peak hours are 01:00 to 04:00 and 06:00 to 10:00 UTC, Monday to Friday. Every other hour, weekends included, is off-peak.

Example. 50M input tokens and 10M output tokens in a month, with 20M of the input as cache hits, all at peak:

  • Cache hits: 20 × $0.006 = $0.12
  • Cache misses: 30 × $0.30 = $9.00
  • Output: 10 × $1.20 = $12.00
  • Total: $21.12. Run the same job off-peak and it’s $10.56.

The same code on other hosts

DeepSeek V4.1 Flash is an open model, so other hosts serve it on the same OpenAI SDK code. Change base_url and the model name, and keep everything else.

Host base_url Model name Price per 1M (in / out)
DeepSeek API https://api.deepseek.com deepseek-flash $0.30 / $1.20 peak
NanoGPT https://api.nano-gpt.com/api/v1 deepseek/deepseek-v4.1-flash $0.039 / $0.40
Hugging Face https://router.huggingface.co/v1 deepseek-ai/DeepSeek-V4.1-Flash:cheapest from $0.051 / $0.567
OpenRouter https://openrouter.ai/api/v1 deepseek/deepseek-v4.1-flash $0.051 / $0.567 + 5.5% fee
DeepInfra https://api.deepinfra.com/v1/openai deepseek-ai/DeepSeek-V4.1-Flash $0.14 / $0.42
Off Grid AI Boost Arrives with your key Arrives with your key Flat plan from $4.99 a month, usage worth twice the price

The thinking field is DeepSeek’s own. Other hosts may ignore it or use their own switch, so check the host’s docs before you rely on it. Where your prompts go matters too: DeepSeek’s privacy policy says it stores data in China and may use it for training, with an opt-out by email. The hosts that don’t keep prompts are in the best private LLM APIs.

Call DeepSeek through Off Grid AI Boost

Boost is one OpenAI-compatible key for open models, with DeepSeek V4.1 Flash and Qwen 3.8 named. Your code changes in two places:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["BOOST_API_KEY"],
    base_url=os.environ["BOOST_BASE_URL"],  # sent with your key in January 2027
)

Then set model to the Boost name for DeepSeek, listed with your key. Plans are flat: Starter $4.99, Pro $9.99 or Max $49.99 a month for cohort 1, half the regular price for as long as you stay, each with usage worth its full regular price every month. “We don’t keep your prompts or train on them.”

Boost is a pre-order. Your key arrives by email in January 2027 and your plan starts that day; if it misses January, you get all your money back. Ship on DeepSeek’s API or NanoGPT now, and swap two environment variables when your key lands.

Not working? Symptom, check, fix

Symptom Check Fix
401 error DEEPSEEK_API_KEY is empty in this shell echo $DEEPSEEK_API_KEY, then export it again
Requests go to OpenAI base_url or baseURL is missing Set it to https://api.deepseek.com
TypeError: unexpected keyword argument 'thinking' You passed thinking straight to the Python SDK Move it into extra_body
TypeScript error on thinking The SDK’s types don’t include it Add // @ts-expect-error above the line
temperature has no effect Thinking mode ignores it Turn thinking off, or drop temperature
The bill is higher than expected Your heavy jobs run in peak hours Schedule batch work outside 01:00 to 04:00 and 06:00 to 10:00 UTC on weekdays, when prices halve
Charges at the V4 Pro price The model is deepseek-v4-pro Use deepseek-flash

FAQ

Can I use the OpenAI SDK with DeepSeek? Yes. DeepSeek’s API uses the OpenAI format. Create the OpenAI client with base_url https://api.deepseek.com and your DeepSeek key, then call chat.completions.create with model deepseek-flash.

What is the DeepSeek model name for V4.1 Flash? deepseek-flash. DeepSeek’s docs say it’s served by DeepSeek-V4.1-Flash-1017. The older name deepseek-v4-flash is still accepted and goes to the same model at the Flash price.

How do I turn off thinking in the DeepSeek API? Thinking is on by default at high effort. Send thinking with type disabled; in the Python SDK that’s extra_body={"thinking": {"type": "disabled"}}. In JavaScript, put thinking straight into the request object.

How much does the DeepSeek API cost? deepseek-flash costs $0.30 per million input tokens, $1.20 per million output tokens and $0.006 per million cached input tokens at peak. Off-peak prices are half that.

Is there a cheaper way to call DeepSeek V4.1 Flash? Yes. NanoGPT serves it at $0.039 in and $0.40 out per million tokens with the same OpenAI SDK code; change base_url and the model name. For a flat bill, Off Grid AI Boost is a pre-order from $4.99 a month, with keys in January 2027.

Where DeepSeek is cheapest on every host: cheapest OpenRouter alternatives, price per million tokens and the API price index. Using it in an editor? See DeepSeek in Cursor.

Pre-order Boost from $4.99

DID THIS LAND?
UPDATES FROM THE CREATOR