DeepSeek API offers a powerful suite of tools for developers looking to integrate advanced AI capabilities into their applications. Compatible with the OpenAI API format, DeepSeek makes it easy to transition and leverage existing knowledge and tools. This article will walk you through the essentials of making your first API call and exploring the key features.
One of the most advantageous aspects of DeepSeek API is its compatibility with the OpenAI API format. This means you can use the OpenAI SDK and other OpenAI-compatible software with minimal modifications to access DeepSeek's functionalities.
Before diving into code, you’ll need an API key.
With your API key in hand, you're ready to make your first call to the DeepSeek API. Let's explore different code examples using cURL, Python, and Node.js.
https://api.deepseek.com
. For OpenAI compatibility, you can also use https://api.deepseek.com/v1
. Note that the v1
here is for compatibility and doesn't relate to the model version.cURL is a versatile command-line tool for making HTTP requests. Here’s how to use it with the DeepSeek API:
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 JSON payload to the /chat/completions
endpoint, instructing the deepseek-chat
model (now upgraded to DeepSeek-V3) to respond to the user's "Hello!" message, while adopting the persona of a helpful assistant.
For Python developers, the OpenAI SDK provides a straightforward way to interact with the DeepSeek API.
First, install the OpenAI SDK:
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)
This script initializes the OpenAI client with your DeepSeek API key and base URL, then sends a similar request as the cURL example.
Node.js developers can also leverage the OpenAI SDK.
First, install the OpenAI package:
npm install openai
Then, use the following 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();
This code snippet performs the same function as the Python example, but within a Node.js environment.
DeepSeek offers more than just chat models. The deepseek-reasoner
model, powered by DeepSeek-R1, is designed for advanced reasoning tasks (Reasoning Model Guide). To use it, simply specify model='deepseek-reasoner'
in your API call.
When integrating DeepSeek API into your applications, keep the following points in mind:
To stay informed about the latest features, updates, and news regarding DeepSeek, consider the following:
By following this guide, you should now be well-equipped to start experimenting with the DeepSeek API and integrating it into your projects.