跳至主要內容

快速開始

建立金鑰、設定 base URL、送出第一個請求——curl、Python 與 Node 範例。

快速開始

1. 建立 API 金鑰

登入 Dashboard,前往 API Keys,點選「建立金鑰」。金鑰只會在建立當下完整顯示一次,格式為 sk-inf- 開頭。

建立時可一併設定:

  • 每月花費上限:超過即回傳 spend_limit_reached。
  • 可用模型白名單:不在名單內的模型會回傳 model_not_allowed。

把金鑰存進環境變數,不要寫死在程式碼裡:

export INFERENCE_API_KEY="sk-inf-xxxxxxxxxxxxxxxxxxxxxxxx"

2. 儲值

Inference 採預付制。到 Dashboard 的 Billing 儲值 credits,餘額不足時 API 會回傳 HTTP 402 insufficient_credit。詳見計費說明。

3. 送出第一個請求

curl

curl https://api.alphacurve.io/v1/chat/completions \
  -H "Authorization: Bearer $INFERENCE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o-mini",
    "messages": [
      { "role": "system", "content": "你是簡潔的助理。" },
      { "role": "user", "content": "為什麼要用 API gateway?" }
    ],
    "temperature": 0.7
  }'

Python(openai SDK)

pip install openai
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.alphacurve.io/v1",
    api_key=os.environ["INFERENCE_API_KEY"],  # sk-inf-...
)

resp = client.chat.completions.create(
    model="anthropic/claude-sonnet-4-5",
    messages=[
        {"role": "system", "content": "你是簡潔的助理。"},
        {"role": "user", "content": "為什麼要用 API gateway?"},
    ],
)

print(resp.choices[0].message.content)
print(resp.usage)

Node / TypeScript(openai SDK)

npm install openai
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.alphacurve.io/v1",
  apiKey: process.env.INFERENCE_API_KEY, // sk-inf-...
});

const resp = await client.chat.completions.create({
  model: "google/gemini-2.5-flash",
  messages: [
    { role: "system", content: "你是簡潔的助理。" },
    { role: "user", content: "為什麼要用 API gateway?" },
  ],
});

console.log(resp.choices[0].message.content);

4. 換一個模型

切換供應商只要改 model 字串,其他都不用動:

resp = client.chat.completions.create(
    model="google/gemini-2.5-pro",   # 原本是 openai/gpt-4o
    messages=[{"role": "user", "content": "Hello"}],
)

想知道有哪些可用模型,呼叫 GET /v1/models:

curl https://api.alphacurve.io/v1/models \
  -H "Authorization: Bearer $INFERENCE_API_KEY"

5. 加上串流

stream = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "寫一首關於路由的俳句。"}],
    stream=True,
)

for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="", flush=True)

完整說明請見 Streaming。