DeepSeek API: Set Up Your Key and Send Your First Request

To use the DeepSeek API, you need an API key, available account credit and a small piece of code that sends your request. Start with deepseek-flash: it has lower published prices than deepseek-v4-pro, making it a sensible choice for your first call.

Choose the Python or Node.js example below to summarize a short delivery update from your terminal. The model runs on DeepSeek’s servers, so you do not need a local GPU.

Get your API key and check your balance

Sign in to the DeepSeek Platform’s API keys page and create a key. Keep it somewhere private; you will use it in the example you choose. This must be a DeepSeek key, even though the code uses a library called openai.

DeepSeek API keys dashboard showing the Create new API key button and key management interface

Check your API balance before continuing. Requests use paid or granted credit; add funds through the official top-up page if needed. Creating a key does not guarantee a free allowance.

Send your first request with Python

You need Python 3.10 or newer for the current OpenAI Python library. Open PowerShell on Windows or Terminal on macOS or Linux, then install the library:

python -m pip install --upgrade openai

If your system uses python3 instead of python, use that name in both the installation and run commands.

Save the following as deepseek_example.py. The library will send the request to DeepSeek because base_url points to https://api.deepseek.com. You do not need an OpenAI API key.

Official DeepSeek API documentation showing base URL, API key configuration and supported model names
from getpass import getpass
from openai import OpenAI

client = OpenAI(
    api_key=getpass("DeepSeek API key: ").strip(),
    base_url="https://api.deepseek.com",
)

response = client.chat.completions.create(
    model="deepseek-flash",
    messages=[{
        "role": "user",
        "content": (
            "Summarize this delivery update in one sentence: "
            "The parcel left the warehouse on Monday. "
            "Heavy rain delayed the truck. Delivery is now expected Thursday."
        ),
    }],
    max_tokens=300,
    extra_body={"thinking": {"type": "disabled"}},
)

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

In the terminal, open the folder where you saved the file and run:

python deepseek_example.py

Paste your DeepSeek key at the prompt and press Enter. The key prompt hides what you type in a normal terminal; the script does not save the key in the file.

You should get a short summary mentioning the rain delay and Thursday delivery. The wording can vary. The final line extracts the answer from response.choices[0].message.content; the rest of the response contains information such as token usage.

To try your own task, replace the delivery note in content. Keep max_tokens=300 for a short reply, or increase it if the answer needs more room. This is an output-token limit, not a word count.

Thinking mode is disabled here to keep the first request simple. DeepSeek enables it by default; you can turn it on later for a task that benefits from additional reasoning. The request format and these controls are documented in the Chat Completions reference.

Prefer JavaScript? Use the OpenAI SDK with Node.js

This sends the same request as the Python example. Use Node.js 24 LTS, which the OpenAI JavaScript library supports. Open a terminal in your project folder and install the library:

npm install openai

In that folder, create a file named .env with the line below, replacing your_key_here with your DeepSeek key. Keep this file private and out of version control.

DEEPSEEK_API_KEY=your_key_here

Save this as deepseek_example.mjs in the same folder:

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:
      "Summarize this delivery update in one sentence: " +
      "The parcel left the warehouse on Monday. " +
      "Heavy rain delayed the truck. Delivery is now expected Thursday.",
  }],
  max_tokens: 300,
  thinking: { type: "disabled" },
});

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

Run the following in the same folder. Node’s environment-file option loads the key on Windows, macOS and Linux:

node --env-file=.env deepseek_example.mjs

The answer should summarize the delay and revised delivery day. In JavaScript, use baseURL and apiKey; the Python library names these settings base_url and api_key. Both examples use DeepSeek’s documented Chat Completions format.

Which model should you use, and what does it cost?

deepseek-flash accepts text and images; deepseek-v4-pro currently takes text only. If Flash’s answer is not good enough for your task, compare it with Pro and decide whether the improvement is worth the higher price.

Your bill depends on input and output tokens, caching and peak or off-peak rates. Longer prompts and replies cost more.

At current peak rates, 1,000 Flash requests with 1,000 uncached input tokens and 200 output tokens each would cost about $0.54. That assumes $0.30 per million input tokens and $1.20 per million output tokens.

These details were checked on October 3, 2026. Check DeepSeek’s current models and pricing before budgeting. Our DeepSeek pricing guide covers the topic in more detail.

Official DeepSeek API pricing and model comparison table for deepseek-flash and deepseek-v4-pro

If your first request fails

Read the error code before changing the example. A rejected key, an empty balance and a busy service need different fixes.

ErrorWhat to do
401Check that you entered a valid DeepSeek API key, without extra spaces or missing characters.
402Check your API balance and add credit if necessary.
400 or 422Read the returned message and check the model name, request format and parameter values. Return to the example before adding more options.
429Reduce the number of requests you send at once. Wait before retrying.
500 or 503Wait briefly and retry. If the problem continues, check the official service status.
Official DeepSeek API error codes table showing HTTP 400, 401, 402, 422, 429, 500 and 503 with causes and solutions

These are the common cases in DeepSeek’s error-code reference. For service incidents, use the official status page.

If Python reports No module named 'openai', rerun the installation command using the same Python command you use to launch the script. That error occurs on your computer, before a request reaches DeepSeek.

Installation reports “externally-managed-environment”

On macOS or Linux, run these commands one at a time from the folder containing deepseek_example.py:

python3 -m venv .venv
.venv/bin/python -m pip install --upgrade openai
.venv/bin/python deepseek_example.py

This installs the library in a separate environment for this project. See the Python packaging guide for more about virtual environments.

Using DeepSeek in an existing app

If your app supports a custom OpenAI-compatible provider, use https://api.deepseek.com as the base URL, your DeepSeek API key for authentication and deepseek-flash as the model name. The full Chat Completions endpoint is https://api.deepseek.com/chat/completions; use the base URL when the setting specifically asks for a base URL.

For a direct HTTP request, authentication uses the header Authorization: Bearer YOUR_DEEPSEEK_API_KEY. The OpenAI libraries handle that header in both examples above. DeepSeek’s official quickstart also includes a cURL example.

Compatibility does not mean every feature of another provider will work. If your app uses Responses rather than Chat Completions, check DeepSeek’s Responses compatibility documentation: for example, it does not support continuing a conversation through previous_response_id. A basic text request is a useful starting point before connecting the rest of your app.

When you move from a local script to a website, keep the key on your server or in its secret settings. Do not embed it in browser JavaScript that visitors can read.

Privacy and cookie settings