[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.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)