English 繁體中文 简体中文

How can a Python program use the DeepSeek API?

Howard Pong Yuen Lam · AI Notes · November 3, 2026 · 5 min read

Chatting with an AI model does not have to happen in a browser. A program can send a message to DeepSeek over the internet and receive a written reply, ready to be printed, saved, or fed into the next step of a larger program. This is how programs get AI answers without a human copy-pasting from a chat window.

The program in this note asks DeepSeek the question “What is the traditional Chinese for Hong Kong?” and prints the reply, in 21 lines.

Getting a key first

DeepSeek needs to know who is asking, so the program presents an API key: a secret string of characters that works like a password. API stands for Application Programming Interface, the way one program talks to another over the internet. To get a key, sign up at https://platform.deepseek.com/; a small balance is enough for trying things out, and the key is created on the API keys page.

The key must not be written inside the program itself, where anyone reading the code could take it. Instead it lives in a small file named .env in the same folder, containing one line:

DEEPSEEK_API_KEY=paste-your-key-here

Create this file yourself after downloading the demo; it is not included in the zip, because a key is personal.

The program, piece by piece

import os
from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

Three libraries are brought in. os stands for operating system; here it is used to read environment variables, settings stored outside the code. load_dotenv reads the .env file and copies each line into those environment variables. OpenAI is the connector from the openai package. It is named after another AI company, but DeepSeek deliberately speaks the same message format, so the same connector works for both: you only change the address it talks to.

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

This creates the connector. os.environ.get("DEEPSEEK_API_KEY") reads the key that load_dotenv() just loaded from .env, and base_url is the internet address of DeepSeek's service. From now on, every request the connector sends goes to that address and carries the key, so DeepSeek knows whose account has the request.

response = client.chat.completions.create(
    model="deepseek-flash",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the traditional Chinese for Hong Kong?"},
    ],
    stream=False,
)

This sends the question and waits for the reply. model="deepseek-flash" picks which DeepSeek model answers; DeepSeek's documentation lists the current names, and they do change over time. The messages list is the conversation so far: a system message sets the model's manners, and a user message is the actual question. stream=False asks for the whole reply in one piece, instead of watching it arrive word by word.

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

The reply sits a few layers down inside the answer package. choices is the list of candidate replies — almost always just one — so [0] takes the first, and message.content is its text. That text is what gets printed.

The whole program

import os
from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

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": "What is the traditional Chinese for Hong Kong?"},
    ],
    stream=False,
)

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

Running it on your own computer

Download the zip file and unzip it to a folder. The zip holds the program and a pyproject.toml project file. Add your own .env file with the key, as shown above. Then open the folder in Visual Studio Code and type these three lines in the terminal, one at a time:

uv python pin 3.12.10
uv sync
uv run deepseek_chat.py

The first line records which Python version this folder uses. The second line sets up the project and installs the two packages the program needs, openai and python-dotenv. The third line runs the program. A few seconds later, the reply appears:

The traditional Chinese for “Hong Kong” is **香港** — the same characters are used in both traditional and simplified Chinese.

- Cantonese: **Hoeng1 Gong2**
- Mandarin: **Xiānggǎng**

The wording will probably differ when you run it, because the model writes a fresh reply each time.

If you have not set up uv yet, start with How to install Visual Studio Code? and then the uv note for Windows or Mac.

Limitations

Further reading