Skip to main content

Code examples

Code you can copy for streaming, Python, direct HTTP and embeddings.

Before you start

Set your API URL and API key first. Using API keys shows how.

Replace gpt-4o with a model ID from Models. Plans and tiers shows which ones your account can use.

cURL (streaming)

curl https://api.tokenreply.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-token-here" \
  -d '{
    "model": "gpt-4o",
    "stream": true,
    "messages": [
      {"role": "user", "content": "Hello!"}
    ]
  }'

Python (chat)

Install: pip install openai

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-token-here",
    base_url="https://api.tokenreply.com/v1"
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

print(response.choices[0].message.content)

Python (streaming)

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-token-here",
    base_url="https://api.tokenreply.com/v1"
)

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
    stream=True
)

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

JavaScript (direct HTTP)

No SDK needed. Run it on your server, not in the browser, so your key stays private:

const response = await fetch("https://api.tokenreply.com/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk-your-token-here"
  },
  body: JSON.stringify({
    model: "gpt-4o",
    messages: [{ role: "user", content: "Hello!" }]
  })
});

const data = await response.json();
console.log(data.choices[0].message.content);

Embeddings

For search and RAG. Use a model that supports embeddings on the Models page:

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-token-here",
    base_url="https://api.tokenreply.com/v1"
)

embedding = client.embeddings.create(
    model="text-embedding-3-large",
    input="TokenReply API documentation"
)

print(len(embedding.data[0].embedding))

Use environment variables

With the environment variables set, the SDK picks them up on its own:

export OPENAI_API_KEY="sk-your-token-here"
export OPENAI_BASE_URL="https://api.tokenreply.com/v1"

from openai import OpenAI

client = OpenAI()  # auto-reads OPENAI_API_KEY and OPENAI_BASE_URL

Next steps