DeepSeek API offers powerful language models, compatible with the OpenAI API format, making it easier than ever to integrate into existing projects. This article guides you through making your first API call to the DeepSeek API, providing the essential information and code snippets you need to get started.
One of the key advantages of the DeepSeek API is its compatibility with OpenAI. This means that you can leverage the familiar OpenAI SDK and other tools designed for the OpenAI API with minimal configuration changes to access DeepSeek's models.
Before making your first call, you'll need the following:
https://api.deepseek.com
as the base URL for your API requests or https://api.deepseek.com/v1
for OpenAI compatibility.pip3 install openai
) for Python or npm (npm install openai
) for Node.js.The following examples demonstrate how to access the DeepSeek API's chat completion endpoint using curl
, Python, and Node.js.
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
}'
This command sends a POST request to the /chat/completions
endpoint. It includes the following:
Content-Type
: Specifies that the request body is in JSON format.Authorization
: Authenticates the request using your DeepSeek API key. Remember to replace <DeepSeek API Key>
with your actual API key.data
: A JSON payload containing the model to use (deepseek-chat
), a list of messages forming the conversation, and stream: false
for a non-streaming response.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)
This Python script utilizes the OpenAI SDK to interact with the DeepSeek API. Key points:
OpenAI
client with your API key obtained from the DeepSeek platform and the DeepSeek API base URL.chat.completions.create
method to send your request.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();
This Node.js example demonstrates how to use the OpenAI SDK in a JavaScript environment. It showcases the asynchronous nature of the API call and follows a similar structure to the Python example.
DeepSeek offers diverse models tailored for different tasks:
deepseek-chat
model, which has been upgraded to DeepSeek-V3. This model is suitable for conversational tasks and general-purpose language understanding. Invoke DeepSeek-V3 by specifying model='deepseek-chat'
.model='deepseek-reasoner'
.Refer to the Models & Pricing page to explore each model's capabilities and associated costs.
To control the randomness and creativity of the model's output, adjust the temperature
parameter. A lower value will result in more predictable responses, while a higher value encourages more creative and unexpected results. The Temperature Parameter page can help you to understand the parameter deeply.
Keep up with the latest news and updates from DeepSeek:
By following these steps, you can easily make your first API call to the DeepSeek API and begin exploring its powerful language models. For further exploration including rate limits and error codes, refer to the Quick Start guide.