The DeepSeek API offers powerful AI capabilities, leveraging a format compatible with the popular OpenAI API. This compatibility allows developers to seamlessly integrate DeepSeek's functionalities into existing projects using the OpenAI SDK or other compatible software. This guide will walk you through making your first API call to the DeepSeek platform.
One of the key advantages of the DeepSeek API is its alignment with the OpenAI API structure. This means you can often switch between the two with minimal code modifications, making DeepSeek a viable alternative for various AI-powered applications.
Before diving into the code, let's understand the crucial parameters you'll need:
https://api.deepseek.com
to ensure you are connecting to the DeepSeek API. For OpenAI compatibility, you can use https://api.deepseek.com/v1
, however note that the /v1
suffix has NO relationship with the model's version.Below are code snippets demonstrating how to make a simple chat completion API call using different programming languages. We'll be using the deepseek-chat
model, which has been upgraded to DeepSeek-V3.
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 with a JSON payload containing the model name, system message, and user message. Replace <DeepSeek API Key>
with your actual API key. The stream: false
parameter indicates that we want a non-streaming response.
First, install the OpenAI SDK:
pip3 install openai
Then, use the following Python 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 an OpenAI client with your API key and the DeepSeek API's base URL and makes a call to the chat.completions.create
method. Remember to replace <DeepSeek API Key>
with your actual API key. This example demonstrates a non-stream configuration.
First, install the OpenAI SDK:
npm install openai
Then, use the following Node.js 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 showcases how to use the OpenAI SDK in Node.js to interact with the DeepSeek API. Don't forget to replace <DeepSeek API Key>
with your actual API key.
After successfully making your first API call, consider exploring some of DeepSeek's other features. The DeepSeek API offers a variety of models, including the deepseek-reasoner
, a reasoning model based on DeepSeek-R1. To invoke DeepSeek-R1, specify model='deepseek-reasoner'
. You can also investigate parameters like the The Temperature Parameter to influence the creativity of the model.
This guide has provided a comprehensive introduction to making your first API call with the DeepSeek API. Its OpenAI compatibility, coupled with its powerful models, makes it an excellent choice for various AI-driven applications. By following these instructions, you can unlock the potential of DeepSeek and integrate it into your projects. Continue exploring the API Guides, like the Reasoning Model guide, to learn more about its potential in AI.