Switch language

Python client

Call Bitdeer AI from Python using the community openai package and a custom base URL.

This guide uses the openai package as a convenient HTTP client. Configure base_url to Bitdeer and use your Bitdeer API key.

Install

pip install openai

Configure

Client options
api_keystrrequired

Your Bitdeer API key. Never hardcode into source control.

base_urlstrrequired
timeoutfloatdefault: 600

Request timeout in seconds.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["BITDEER_API_KEY"],
    base_url="https://api-inference.bitdeer.ai/v1",
)

Chat completion

curl https://api-inference.bitdeer.ai/v1/chat/completions \
  -H "Authorization: Bearer $BITDEER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-ai/DeepSeek-V4-Pro",
    "messages": [{"role": "user", "content": "Hello!"}],
    "max_tokens": 256
  }'

Async

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(
    api_key=os.environ["BITDEER_API_KEY"],
    base_url="https://api-inference.bitdeer.ai/v1",
)

async def main():
    chat = await client.chat.completions.create(
        model="deepseek-ai/DeepSeek-V4-Pro",
        messages=[{"role": "user", "content": "Hello!"}],
        max_tokens=256,
    )
    print(chat.choices[0].message.content)

asyncio.run(main())

Streaming

stream = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[{"role": "user", "content": "Tell me a joke."}],
    max_tokens=256,
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)

Error handling

from openai import APIError, RateLimitError

try:
    client.chat.completions.create(...)
except RateLimitError:
    # back off and retry
    ...
except APIError as err:
    # err.status_code, err.code, err.message
    raise

See Errors for the full error reference.

Last updated on

On this page

Python client · Bitdeer AI