How to Use DeepSeek V4 with OpenAI Python SDK
July 7, 2026 — 6 min read tutorial openai-sdk
One of the best things about DeepSeek V4 is that it's fully compatible with the OpenAI API format. If you already have code written for OpenAI, switching to DeepSeek is as simple as changing two lines.
In this guide, we'll walk through the complete setup: getting your API key, configuring the SDK, and making your first request — all in under 5 minutes.
What You'll Need
- A PayPal account (to purchase API credits)
- Python 3.8+ with the
openaipackage installed - 2 minutes of your time
Step 1: Get Your API Key
Head over to Token Gateway, choose a plan, and pay with PayPal. Your API key will be generated instantly and displayed on your dashboard.
That's it. No phone verification. No ID checks. No waiting for approval.
Step 2: Install the OpenAI SDK
You probably already have this, but if not:
pip install openai
Step 3: Make Your First API Call
The only changes needed are the base_url and api_key:
from openai import OpenAI
client = OpenAI(
api_key="tg-your-api-key-here",
base_url="https://token.mall199.com/v1"
)
response = client.chat.completions.create(
model="deepseek-flash", # or "deepseek-pro"
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a Python function to calculate Fibonacci numbers."}
]
)
print(response.choices[0].message.content)
Step 4: Streaming Responses
Streaming works exactly like OpenAI. Just add stream=True:
stream = client.chat.completions.create(
model="deepseek-flash",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Using cURL
No SDK? No problem. Raw HTTP works too:
curl https://token.mall199.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer tg-your-api-key-here" \
-d '{
"model": "deepseek-flash",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Using Node.js
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "tg-your-api-key-here",
baseURL: "https://token.mall199.com/v1",
});
const response = await client.chat.completions.create({
model: "deepseek-flash",
messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);
Integrating with LangChain
DeepSeek works seamlessly with LangChain through the OpenAI chat model wrapper:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="deepseek-flash",
openai_api_key="tg-your-api-key-here",
openai_api_base="https://token.mall199.com/v1"
)
response = llm.invoke("Explain quantum computing in simple terms")
print(response.content)
Model Options
| Model ID | Best For | Input Cost (per 1M) | Output Cost (per 1M) |
|---|---|---|---|
deepseek-flash |
Chat, content, high throughput | $0.50 | $1.00 |
deepseek-pro |
Reasoning, code, analysis | $1.50 | $3.00 |
Tips
- Start with
deepseek-flashfor most use cases — it's incredibly capable for its price. - Switch to
deepseek-profor complex reasoning tasks. - Monitor your usage from the dashboard to avoid surprises.
- All packages include generous rate limits — no hidden throttling.
Ready to build?
Get your API key in 2 minutes. PayPal accepted. No Chinese phone required.
Get Started →