DeepSeek API Tutorial: V4 Flash / Pro with Python and JavaScript
Call DeepSeek V4 Flash and Pro through APIMaster with curl, Python and JavaScript. Configure your API key, stream responses and troubleshoot common errors.
Can you call DeepSeek with the OpenAI SDK? Yes. Through APIMaster, set the API base URL to https://apimaster.ai/v1, use your APIMaster API key, and select deepseek-v4-flash or deepseek-v4-pro. This guide covers that gateway configuration; a key issued by another provider is not interchangeable.
Last tested: 2026-09-07. Both models returned HTTP 200 and the expected text in our Chat Completions test. Codex CLI 0.153.4 and Claude Code 2.1.239 also completed text and file-reading tool tests through the same gateway. These are short functional checks, not a long-context or reliability benchmark. The Python, JavaScript and streaming examples below demonstrate the corresponding API usage; they are not separate benchmark results.
1. Get your key and choose a model
Create an account and get an API key. Ensure the key has access to the selected model and sufficient quota. Check current prices in the model marketplace; rates can vary by route and time.
| Setting | Value |
|---|---|
| SDK base URL | https://apimaster.ai/v1 |
| HTTP endpoint | POST https://apimaster.ai/v1/chat/completions |
| Authorization | Authorization: Bearer YOUR_APIMASTER_API_KEY |
| Flash model | deepseek-v4-flash |
| Pro model | deepseek-v4-pro |
Both models use the same request shape. Start with either model, then compare results on your own workload. Replace YOUR_APIMASTER_API_KEY below with your key locally; do not commit it to source control or put it in browser code.
macOS / Linux:
export APIMASTER_API_KEY='YOUR_APIMASTER_API_KEY'
Windows PowerShell:
$env:APIMASTER_API_KEY = 'YOUR_APIMASTER_API_KEY'
These variables apply to the current terminal. Run the following examples from that terminal.
2. Make your first request with curl
The following command uses macOS / Linux shell syntax. Windows users can use the cross-platform Python example below.
curl --fail-with-body 'https://apimaster.ai/v1/chat/completions' \
-H "Authorization: Bearer $APIMASTER_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "deepseek-v4-flash",
"messages": [{"role": "user", "content": "Reply with exactly: DEEPSEEK_TEST_OK"}],
"max_tokens": 256,
"stream": false
}'
Expected: HTTP 200 and choices[0].message.content equal to DEEPSEEK_TEST_OK. Change only model to deepseek-v4-pro to repeat the test. The response may include a versioned model name; continue using the public model IDs above in requests.
3. Call DeepSeek from Python
Install the SDK:
python -m pip install openai
Save as deepseek_example.py, then run python deepseek_example.py:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["APIMASTER_API_KEY"],
base_url="https://apimaster.ai/v1",
)
response = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[{"role": "user", "content": "Reply with exactly: DEEPSEEK_TEST_OK"}],
max_tokens=256,
)
print(response.choices[0].message.content)
For a coding task, replace the prompt with your requirements. Increase the output budget for longer answers; the short test budget is not suitable for generating a complete application.
4. Call DeepSeek from JavaScript
Use a server-side Node.js environment:
npm install openai
Save as deepseek_example.mjs, then run node deepseek_example.mjs:
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.APIMASTER_API_KEY,
baseURL: "https://apimaster.ai/v1",
});
const response = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "Reply with exactly: DEEPSEEK_TEST_OK" }],
max_tokens: 256,
});
console.log(response.choices[0].message.content);
5. Stream the final answer
Using the Python client above:
stream = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[{"role": "user", "content": "Explain a Python dictionary in three sentences."}],
max_tokens=1024,
stream=True,
)
for chunk in stream:
if not chunk.choices:
continue
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
print()
This prints final-answer text from content. Some responses also expose reasoning_content; it is separate from the final answer. Do not assume every streaming chunk contains text or a choices entry.
Troubleshooting
| Symptom | What to check |
|---|---|
| 401 | Use an APIMaster key, check whitespace and the Bearer header, and confirm the environment variable is set in this terminal. |
| 403 or model access error | Check the key's model permissions and account restrictions. |
| 404 | Use /v1/chat/completions; do not duplicate /v1 or append the endpoint to an SDK base URL. |
| 429 or insufficient quota | Read the error body to distinguish a rate limit from quota exhaustion. Retry with backoff for rate limits; check balance for quota errors. |
| Empty or truncated answer | Inspect finish_reason, increase the token budget, and distinguish content from reasoning fields. |
| Timeout or 5xx | Record the timestamp and request ID, retry with backoff, and contact support if it persists. Never send your full key in a bug report. |
Frequently asked questions
Is the API key the same as a DeepSeek official key?
No. This tutorial uses a key issued by APIMaster with APIMaster's endpoint. Keep the key and its issuing provider's endpoint paired.
Can I switch between Flash and Pro without rewriting my integration?
Yes, change the model field between deepseek-v4-flash and deepseek-v4-pro, provided your key can access both. Check each model's output and current pricing for your workload.
Does a successful request prove the underlying model's identity?
No. HTTP success and a returned model name establish that a request completed; they do not independently verify model identity.
Can I use these models in Codex and Claude Code?
Our tested configurations completed text and file-reading tool requests in both CLIs. The protocols differ: Codex uses /v1/responses, while Claude Code uses /v1/messages. Follow the dedicated guides rather than substituting the Chat Completions endpoint into a CLI configuration.