The DeepSeek API offers a powerful platform for developers looking to integrate advanced AI capabilities into their applications. Designed with compatibility in mind, the DeepSeek API mirrors the OpenAI API format, making it easier to transition and leverage existing tools and knowledge. This article will guide you through making your first API call to the DeepSeek API, equipping you with the essentials to start building intelligent applications.
One of the key advantages of DeepSeek API is its compatibility with the OpenAI API. This means you can potentially use the OpenAI SDK and other software already configured for OpenAI with minimal modifications.
Before diving into the code, you need to set up your environment with the necessary parameters:
base_url
: This is the foundation of your API requests. Use https://api.deepseek.com
as your base URL. For OpenAI compatibility, you can also use https://api.deepseek.com/v1
.api_key
: The key to accessing the DeepSeek API. Obtain yours by signing up and creating one at the DeepSeek Platform.Here's how to make your first API call using different programming languages. This example demonstrates a non-streaming request; you can easily enable streaming by setting the stream
parameter to true
.
Using 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
}'
Using Python (with OpenAI SDK):
# 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)
Using Node.js (with OpenAI SDK):
// 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();
model='deepseek-reasoner'
. Learn more in the Reasoning Model Guide.pip3 install openai
or npm install openai
).<DeepSeek API Key>
with your actual API key.The DeepSeek API offers more than just basic chat functionalities. Here are a few areas worth exploring and relevant API guides:
The DeepSeek team regularly releases updates and new features. Stay informed through the News section and the Change Log.
If you encounter any issues or have questions, DeepSeek provides several community channels. Consider checking out the FAQ.