OpenAI API Access Guide: Chat Completions Compatibility & Key Configuration

  • 发布时间
  • 语言en

Access our large language models via the OpenAI Chat Completions compatible interface. Covers API endpoints, authentication, curl/SDK examples, parameters, streaming, and error codes.

I. Overview

This site provides compatibility with the OpenAI Chat Completions (/v1/chat/completions) format. For code already running with the official OpenAI SDK or OpenAI-compatible clients, simply point your request URL to our site's OpenAI gateway, replace the api_key with your sk- key from our platform, and keep all other call methods the same.

Current available GPT series models are listed in the Model Square (e.g., gpt-5.4, gpt-5.5, gpt-5.4-mini, gpt-4o, etc.). For a complete list and unit pricing, please query GET /v1/models.

II. API Endpoints

MethodPathDescription
POST/v1/chat/completionsChat completions (non-streaming / streaming); request body follows OpenAI payload.
GET/v1/modelsList of models and unit prices (no authentication required).

Gateway URL: https://www.relay-api.com. For OpenAI-compatible calls, please send requests to POST https://www.relay-api.com/v1, keeping the original path.

III. Authentication

Authorization: Bearer sk-your_api_key

Create a key starting with sk- in "User Center → API Keys". Use the same authentication header as the official OpenAI API; the SDK will automatically add the Authorization header.

IV. Quick Start

4.1 curl (Non-streaming)

curl -X POST https://www.relay-api.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your_api_key" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Introduce yourself in three sentences."}
    ],
    "temperature": 0.7,
    "max_tokens": 512
  }'

4.2 OpenAI SDK (Python)

from openai import OpenAI

client = OpenAI(
    base_url="https://www.relay-api.com/v1",
    api_key="sk-your_api_key",
)

resp = client.chat.completions.create(
    model="gpt-5.4",
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)

After setting base_url to the OpenAI entry point above and replacing the api_key, all other invocation methods are identical to the official SDK.

V. Request Parameters

FieldTypeRequiredDescription
modelstringYesModel slug, e.g., gpt-5.4
messagesarrayYesMessage list, elements are {role, content}; role values: system / user / assistant
temperaturenumberNoSampling temperature, default 1.0
top_pnumberNoNucleus sampling, default 1.0
max_tokensintNoMaximum output tokens
streamboolNoEnable streaming response, default false
stopstring / arrayNoStop sequence
presence_penalty / frequency_penaltynumberNoRepetition penalty, range -2 to 2

VI. Response Structure

{
  "id": "chatcmpl-123456",
  "object": "chat.completion",
  "created": 1710000000,
  "model": "gpt-5.4",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "Hello! Glad to meet you."},
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 12,
    "total_tokens": 30
  }
}
FieldDescription
idUnique ID for this completion
choices[].messageAssistant response; content contains the text
choices[].finish_reasonTermination reason: stop / length / content_filter
usage.prompt_tokensInput tokens (billing basis)
usage.completion_tokensOutput tokens (billing basis)
usage.total_tokensTotal tokens used

VII. Streaming Output

Adding "stream": true to the request body returns an SSE stream, where each data: line is a chunk:

data: {"id": "chatcmpl-123456", "object": "chat.completion.chunk", "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": null}]}

data: {"id": "chatcmpl-123456", "object": "chat.completion.chunk", "choices": [{"index": 0, "delta": {"content": "Hello"}, "finish_reason": null}]}

data: {"id": "chatcmpl-123456", "object": "chat.completion.chunk", "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}

data: [DONE]

Clients can concatenate choices[0].delta.content from each chunk to get the full response; receiving [DONE] indicates the end of the stream. The OpenAI SDK handles these events automatically when stream=True is passed.

VIII. Common Error Codes

HTTP StatusMeaningAdvice
400Invalid request parameters (model missing, empty messages, etc.)Check request body fields
401Invalid or missing API keyCheck Authorization header and key format
403Key deactivated, insufficient balance, or unauthorized model/routeTop up or check key permissions
404Path or model not foundConfirm base_url and model name
429Rate limit triggeredRetry with exponential backoff
500Internal server errorRetry later; contact support if the issue persists

Error response bodies consistently follow the structure: {"code": status_code, "message": "error_description", "data": null}.

IX. Compatibility Notes

  • Supports OpenAI SDKs (Python / Node.js, etc.) via custom base_url.
  • Supports various OpenAI-compatible clients (LobeChat, ChatBox, NextChat, One API, etc.) by configuring the custom API URL.
  • Model names must match the slugs returned by GET /v1/models.