[1098] Getting Started with the DeepSeek API
References:
Making API Requests with Python
Now that you have your API key and necessary dependencies, you’re ready to start making API requests to DeepSeek. Here’s a simple example of how to do that in Python.
Step 1: Create Your Python Script
Open your code editor (such as Visual Studio Code) and create a new Python file. Let’s name it deepseek.py
. In this file, you’ll write the code to send a request to the DeepSeek API.
Step 2: Add the Code
Below is the basic Python script that sends a request to the DeepSeek API using the requests
library.
import requests
# Replace with your OpenRouter API key
API_KEY = 'your_openrouter_api_key'
API_URL = 'https://openrouter.ai/api/v1/chat/completions'
# Define the headers for the API request
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
# Define the request payload (data)
data = {
"model": "deepseek/deepseek-chat:free",
"messages": [{"role": "user", "content": "What is the meaning of life?"}]
}
# Send the POST request to the DeepSeek API
response = requests.post(API_URL, json=data, headers=headers)
# Check if the request was successful
if response.status_code == 200:
print("API Response:", response.json())
else:
print("Failed to fetch data from API. Status Code:", response.status_code)
Step 3: Run the Script
Once you’ve added the code, save the file and open your terminal or command prompt. Navigate to the directory where your deepseek.py
file is located, and run the script using:
python deepseek.py
If everything is set up correctly, you should see a response from DeepSeek in your terminal.