The DeepSeek API is designed to be user-friendly and compatible with the OpenAI API format, which allows developers to integrate it seamlessly into existing workflows. This guide will walk you through making your first API call, explaining the necessary configurations and providing example scripts in various programming languages.
One of the key advantages of the DeepSeek API is its compatibility with OpenAI. If you're already familiar with OpenAI's SDK or other tools, you can easily adapt them to work with DeepSeek by modifying the base URL.
Before diving into the code, you'll need two essential parameters:
https://api.deepseek.com
. For OpenAI compatibility, you can also use https://api.deepseek.com/v1
.Once you have your API key, you can start making requests to the DeepSeek API. Here are examples using curl
, Python, and Node.js to invoke the chat API:
curl
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
}'
/chat/completions
endpoint.Content-Type
and Authorization
headers. Make sure to replace “<DeepSeek API Key>
” with your actual API key.-d
flag provides the JSON payload, specifying the model, messages, and stream parameter.# Please install OpenAI SDK first: `pip3 install openai`
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)
pip3 install openai
.OpenAI
class and initialize it with your API key and the DeepSeek base URL.chat.completions.create
method to send your request, specifying the model, messages, and stream parameter.response.choices[0].message.content
.// Please install OpenAI SDK first: `npm install openai`
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();
npm install openai
.OpenAI
class and configure it with your API key and the DeepSeek base URL.chat.completions.create
method within an async
function to send your request.completion.choices[0].message.content
.deepseek-chat
model has been upgraded to DeepSeek-V3. You can invoke it by specifying model='deepseek-chat'
.deepseek-reasoner
is the latest reasoning model, DeepSeek-R1. You can invoke DeepSeek-R1 by specifying model='deepseek-reasoner'
. See more about Reasoning Models.stream
parameter to true
.By following this guide, you should be able to successfully make your first API call to the DeepSeek API. Explore the DeepSeek API documentation for more advanced features and models. Don't forget to check out the API status page for updates and maintenance information.