The DeepSeek API provides powerful AI capabilities, and the best part is its compatibility with the OpenAI API format. This means you can leverage existing OpenAI SDKs and tools with minimal configuration changes to access DeepSeek's advanced models. This guide will walk you through making your first API call to the DeepSeek API.
One of the key advantages of the DeepSeek API is its design to be compatible with the OpenAI API. By adjusting the base URL, you can seamlessly integrate DeepSeek's models into your existing projects that already use the OpenAI SDK or other OpenAI-compatible software.
Before you can make your first API call, you need to:
https://api.deepseek.com
or https://api.deepseek.com/v1
as your base URL. Note that the /v1
in the URL does not correspond to the model version but is for compatibility.Here are examples using curl
, Python, and Node.js to invoke the Chat API. This example demonstrates a non-streaming request. To receive a stream of responses, set the stream
parameter to true
.
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
Make sure you have the OpenAI SDK installed:
pip3 install openai
Then, use the following code:
from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
stream=False
)
print(response.choices[0].message.content)
First, install the OpenAI SDK:
npm install openai
Then, use this code:
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: 'https://api.deepseek.com',
apiKey: '<DeepSeek API Key>'
});
async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "deepseek-chat",
});
console.log(completion.choices[0].message.content);
}
main();
deepseek-chat
model has been upgraded to DeepSeek-V3, with the API remaining the same. You can invoke DeepSeek-V3 by specifying model='deepseek-chat'
. For reasoning tasks, consider using the reasoning model, DeepSeek-R1, released by DeepSeek. You can invoke DeepSeek-R1 by specifying model='deepseek-reasoner'
. See more on Reasoning Models.stream
to true
enables you to receive responses as they are generatedNow that you've made your first API call, explore other features and capabilities of the DeepSeek API. Check out the documentation for:
By leveraging the DeepSeek API, you can integrate cutting-edge AI capabilities into your applications with ease.