コンテンツにスキップ
無料で登録
API リファレンス

Chat Completions API の使い方

完全なリクエスト URL、Bearer API Key、curl、Python、JavaScript の例を使って、BetterToken の OpenAI 互換 Chat Completions API を利用します。

POST https://bettertoken.ai/v1/chat/completions で OpenAI 互換の Chat Completions を呼び出します。ほとんどの外部ツールでは Base URL に https://bettertoken.ai/v1 を指定し、/chat/completions はツール側で追加されます。

主要設定

項目値
API KeyBetterToken API Key
Full request URLhttps://bettertoken.ai/v1/chat/completions
ModelYOUR_MODEL_ID

前提条件

インストール

curl

curl に SDK は不要です。ターミナルで curl --version が動作することを確認してください。

Python

ターミナルウィンドウ
python -m pip install openai

JavaScript

ターミナルウィンドウ
npm install openai

手動設定

curl

ターミナルウィンドウ
curl "https://bettertoken.ai/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
--data '{
"model": "YOUR_MODEL_ID",
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}'

Python SDK

from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://bettertoken.ai/v1",
)
response = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[
{"role": "user", "content": "Hello"},
],
)
print(response.choices[0].message.content)

JavaScript SDK

import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://bettertoken.ai/v1",
});
const response = await client.chat.completions.create({
model: "YOUR_MODEL_ID",
messages: [{ role: "user", content: "Hello" }],
});
console.log(response.choices[0].message.content);

接続の確認

右側の Playground で次の操作を行います。

  1. Authorization に YOUR_API_KEY を入力します。
  2. model を YOUR_MODEL_ID に置き換えます。
  3. messages を編集してリクエストを送信します。

Playground では完全な URL https://bettertoken.ai/v1/chat/completions がすでに使用されています。

200 レスポンスにモデル出力が choices[0].message.content として含まれていれば、接続は確認できています。

モデルの切り替え

YOUR_MODEL_ID を、モデル広場または Setup ダイアログに表示される完全な Model ID に置き換えます。表示名をもとに Model ID を短縮しないでください。

よくあるエラー

エラー対処方法
401Authorization: Bearer YOUR_API_KEY を確認してから、BetterToken API Key をもう一度コピーします。
404直接の HTTP 呼び出しには https://bettertoken.ai/v1/chat/completions を使用します。SDK の base_url には https://bettertoken.ai/v1 を使用します。
モデルが見つからないモデル広場または Setup ダイアログに表示される完全な Model ID を使用します。
無効なリクエストmessages が配列であり、各項目に role と content があることを確認します。

技術情報

Base URL と完全なリクエスト URL

GitHub Copilot や TRAE など、完全なリクエスト URL を必要とするツールでは https://bettertoken.ai/v1/chat/completions を使用します。Cursor、Cline、OpenCode、n8n、Dify など、パスを自ら追加するツールでは https://bettertoken.ai/v1 のみを使用します。

POST/v1/chat/completions

承認

Authorizationstringheader必須

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

ボディ application/json

modelstring必須

A model ID currently available in the model plaza. Check the model plaza for the latest options.

例:"YOUR_MODEL_ID"
messagesobject[]必須

Conversation messages. Each item should contain role and content.

Minimum array length: 1

Show child attributes
messages[].rolestring必須
例:"user"
messages[].contentstring必須
例:"Hello"
streambooleanデフォルト: false

Whether to request a streaming response.

レスポンス

200The Chat Completions result from the selected model.
400Invalid request format or parameters.
401The BetterToken API Key is missing or invalid.
402The account has insufficient credit or balance.
429The request exceeded a rate or concurrency limit, or the upstream service is busy.
500The gateway or upstream service returned an error.