Your First API Call | DeepSeek API Docs

Getting Started with DeepSeek API: Your First API Call

DeepSeek API offers powerful AI capabilities, and the best part is its compatibility with the OpenAI API format. This means you can leverage existing OpenAI SDKs and tools with minimal configuration changes to access DeepSeek's advanced models. This guide will walk you through making your first API call to the DeepSeek API.

DeepSeek API: OpenAI Compatibility

The DeepSeek API is designed to be easily adopted by developers already familiar with OpenAI. By simply adjusting the base URL and API key, you can seamlessly integrate DeepSeek's models into your existing projects.

Here's how it works:

  • OpenAI Compatibility: The API structure mirrors OpenAI, making migration straightforward.
  • SDK Support: Use the OpenAI SDK or other compatible software.
  • Configuration: Modify the base URL and API key to point to DeepSeek.

Essential Parameters for DeepSeek API Access

Before making your first call, you'll need the following:

PARAMETER VALUE
base_url https://api.deepseek.com (or https://api.deepseek.com/v1 for OpenAI compatibility)
api_key Obtain an API key from the DeepSeek Platform.

Note: While you can use https://api.deepseek.com/v1 for base_url for OpenAI compatibility, the /v1 here does NOT correlate with the model's version.

Making Your First Chat API Call

Once you have your API key, you can use various programming languages to interact with the DeepSeek API. Here are a few examples:

1. cURL

curl https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <Your DeepSeek API Key>" \
  -d '{
    "model": "deepseek-chat",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello!"}
    ],
    "stream": false
  }'

2. Python

# Please install OpenAI SDK first: `pip install openai`
from openai import OpenAI

client = OpenAI(
    api_key="<Your 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)

3. Node.js

// Please install OpenAI SDK first: `npm install openai`
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: 'https://api.deepseek.com',
  apiKey: '<Your 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();

Explanation:

  • Replace <Your DeepSeek API Key> with your actual API key.
  • The model parameter specifies which DeepSeek model to use (e.g., "deepseek-chat").
  • The messages array contains the conversation history. The system role sets the behavior of the assistant, and the user role contains the user's input.
  • stream: false indicates a non-streaming response. Set to true for a streaming response.

Important Updates and Models

  • DeepSeek-V3: The deepseek-chat model has been upgraded to DeepSeek-V3. The API remains unchanged; continue using model='deepseek-chat' to invoke DeepSeek-V3.
  • DeepSeek-R1: The latest reasoning model, DeepSeek-R1, can be invoked with model='deepseek-reasoner'.

Further Exploration

You've now successfully made your first API call! To enhance your experience, explore these resources:

By understanding the core components and leveraging the provided examples, you're well-equipped to integrate DeepSeek's powerful AI models into your projects.

. . .