第一个请求
基础地址(Base URL):https://api.oceantoken.ai/v1。API 兼容 OpenAI,所以 OpenAI SDK 只需要基础地址和你的密钥。Claude 模型也可以通过 Anthropic Messages API 调用;Anthropic SDK 会自己加上 /v1,所以它要填 https://api.oceantoken.ai(见下文)。
本页的每个示例都是一个真实的文件,发布前我们都会实际运行一遍。把环境变量 OCEANTOKEN_API_KEY 设为你的密钥,示例即可原样运行。
- curl
- Python
- TypeScript
# Chat completion with curl. Any OpenAI-compatible client works the same way:
# point it at the base URL and use your OceanToken key.
curl -sS "${OCEANTOKEN_BASE_URL:-https://api.oceantoken.ai/v1}/chat/completions" \
-H "Authorization: Bearer $OCEANTOKEN_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-5-mini",
"messages": [{"role": "user", "content": "Say hello in one short sentence."}]
}'
# pip install openai
import os
from openai import OpenAI
client = OpenAI(
base_url=os.getenv("OCEANTOKEN_BASE_URL", "https://api.oceantoken.ai/v1"),
api_key=os.environ["OCEANTOKEN_API_KEY"],
)
response = client.chat.completions.create(
model="openai/gpt-5-mini",
messages=[{"role": "user", "content": "Say hello in one short sentence."}],
)
print(response.choices[0].message.content)
// npm install openai
import OpenAI from "openai";
const client = new OpenAI({
baseURL: process.env.OCEANTOKEN_BASE_URL ?? "https://api.oceantoken.ai/v1",
apiKey: process.env.OCEANTOKEN_API_KEY,
});
const response = await client.chat.completions.create({
model: "openai/gpt-5-mini",
messages: [{ role: "user", content: "Say hello in one short sentence." }],
});
console.log(response.choices[0].message.content);
成功的响应是标准的 chat completion 对象:回答在 choices[0].message.content 里,usage 里是本次计费的 token 数。
流式输出回答
只要有人在等结果,就用流式输出。token 一生成就会送达,长回答也不会碰到限制中说明的请求超时。
# Streaming: tokens arrive as they are generated. Use this for anything a person
# is waiting on, and for long answers, which would otherwise hit the request timeout.
import os
from openai import OpenAI
client = OpenAI(
base_url=os.getenv("OCEANTOKEN_BASE_URL", "https://api.oceantoken.ai/v1"),
api_key=os.environ["OCEANTOKEN_API_KEY"],
)
with client.chat.completions.create(
model="openai/gpt-5-mini",
messages=[{"role": "user", "content": "Count from one to five, one number per line."}],
stream=True,
) as stream:
for chunk in stream:
delta = chunk.choices[0].delta.content if chunk.choices else None
if delta:
print(delta, end="", flush=True)
print()
通过 Anthropic SDK 使用 Claude
# pip install anthropic
# Claude models are served on /v1/messages. The Anthropic SDK works unchanged:
# give it the OceanToken host and your OceanToken key. It adds /v1 itself, so it
# takes the base URL without the /v1 that OpenAI SDKs need.
import os
import anthropic
client = anthropic.Anthropic(
base_url=os.getenv("OCEANTOKEN_BASE_URL", "https://api.oceantoken.ai/v1").removesuffix("/v1"),
api_key=os.environ["OCEANTOKEN_API_KEY"],
)
message = client.messages.create(
model="anthropic/claude-haiku-4.5",
max_tokens=64,
messages=[{"role": "user", "content": "Say hello in one short sentence."}],
)
print(message.content[0].text)
查看可以调用的模型
# The models your key can call, in the standard list format.
curl -sS "${OCEANTOKEN_BASE_URL:-https://api.oceantoken.ai/v1}/models" \
-H "Authorization: Bearer $OCEANTOKEN_API_KEY"
返回的列表取决于你的密钥:如果你的账户只开放了部分模型,列表里只会出现这些模型。带价格的完整目录在模型页和控制台里。