DeepSeek API tutorial

How to authenticate, which endpoint to call, and the mistakes that cost people an afternoon.

The DeepSeek API is OpenAI-compatible: the official documentation shows requests built with the standard OpenAI SDKs. In practice that means three values decide whether your first call works — the API key, the base URL and the model ID.

1. Create an account and an API key

2. Point your client at the right endpoint

https://api.deepseek.com
Anthropic-format endpoint: https://api.deepseek.com/anthropic

If you are migrating existing code, keep your current SDK and change only base_url and the model name. If you see 401 responses, the key is usually wrong; if you see 404 on the model, the model ID is wrong or deprecated.

3. A minimal working request

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["DEEPSEEK_API_KEY"],
    base_url="https://api.deepseek.com",
)

resp = client.chat.completions.create(
    model="deepseek-flash",
    messages=[{"role": "user", "content": "Write a Python function that retries an HTTP call."}],
)
print(resp.choices[0].message.content)

4. Watch out for these

5. Controlling cost

6. Next steps

Facts on this page were checked against the official pages linked above on 2026-09-19. Prices and model IDs change frequently: confirm them on the vendor’s own pricing page before you rely on them.