Skip to main content

๐Ÿ’ป Code Examples

This page keeps the more variant-oriented and advanced snippets, so it does not repeat the basic onboarding flow from "Using API Key".

๐Ÿ’ก Before You Start

If you have not completed the first integration yet, start with Using API Key. This page assumes you already know how to set the API URL and API key.

The gpt-4o in these snippets is a placeholder. Replace it with a model your account can call (see Models and Plans & Tiers).

๐ŸŒ€ 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 (Basic 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 (Raw HTTP)

If you do not want an SDK and just want to send raw HTTP requests, use this pattern:

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 Example

If you are integrating retrieval or RAG, this example is more representative.

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))

โš™๏ธ Environment Variable Auto-Read

If your runtime already has the environment variables set, the SDK can read them automatically:

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