How to get DeepSeek API key
Quick answer
To get a
DeepSeek API key, sign up at the official DeepSeek platform website and create an account. After logging in, navigate to the API section to generate and copy your API key for use in your Python projects with the openai SDK.PREREQUISITES
Python 3.8+pip install openai>=1.0DeepSeek account registration
Setup
Install the openai Python package to interact with DeepSeek's API, which is OpenAI-compatible. Set your DeepSeek API key as an environment variable for secure usage.
pip install openai>=1.0 Step by step
Follow these steps to obtain and use your DeepSeek API key in Python:
- Register at the official DeepSeek website.
- Log in and go to the API keys section.
- Create a new API key and copy it.
- Set the key in your environment variables, e.g.,
export OPENAI_API_KEY='your_deepseek_api_key'on Unix or set it in your OS environment variables. - Use the key in your Python code to call DeepSeek models.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "Hello from DeepSeek!"}]
)
print(response.choices[0].message.content) output
Hello from DeepSeek! How can I assist you today?
Common variations
You can use different DeepSeek models like deepseek-reasoner for reasoning tasks. The API usage pattern remains the same. For asynchronous calls, use Python's asyncio with compatible HTTP clients or SDK wrappers.
import asyncio
import os
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.acreate(
model="deepseek-chat",
messages=[{"role": "user", "content": "Async call test."}]
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Async call test. How can I help you?
Troubleshooting
If you receive authentication errors, verify your API key is correctly set in the environment variable OPENAI_API_KEY. Ensure your DeepSeek account is active and the key has not expired or been revoked. Check your network connection and DeepSeek service status if requests fail.
Key Takeaways
- Register on DeepSeek's official platform to generate your API key.
- Set your API key securely in the environment variable
OPENAI_API_KEY. - Use the OpenAI-compatible
openaiPython SDK to call DeepSeek models. - DeepSeek supports multiple models like
deepseek-chatanddeepseek-reasoner. - Check API key validity and environment setup if authentication errors occur.