このガイドでは、新しいアカウントから最初の生成までを順に進めます。アカウントと API キーを作成し、残高を追加して、チャットのリクエストを送り、残高を確認します。ベース URL は https://api.sakuranbo.ai/v1 です。
1. アカウントを作成する
Section titled “1. アカウントを作成する”ダッシュボードでサインアップします。サインアップの画面の手順に従ってください。
2. API キーを作成する
Section titled “2. API キーを作成する”ダッシュボードのAPI キーページを開き、キーを作成します。キーに名前を付けます。利用上限と有効期限も設定できます。詳しくは残高とキーの利用上限を参照してください。
ダッシュボードにキーの全文が表示されるのは、作成した直後の 1 回だけです。そのときにコピーしてください。キーは cherry_ で始まります。このドキュメントのすべての例が読み込む環境変数 SAKURANBO_API_KEY に、キーを保存します。
export SAKURANBO_API_KEY="paste-your-key-here"キーを、クライアント側のコードや公開リポジトリに置かないでください。詳しくは認証を参照してください。
3. 残高を追加する
Section titled “3. 残高を追加する”ダッシュボードの入金ページを開き、カードまたは暗号資産で残高を追加します。ページには、利用できる支払い手段が表示されます。1 回の入金の最低額は、既定で $10.00 です。詳しくは料金と課金を参照してください。
4. チャットのリクエストを送る
Section titled “4. チャットのリクエストを送る”API キーを付けて、POST /v1/chat/completions にリクエストを送ります。例では、モデル sakuranbo/cherrytext-1.0-flash を使います。
curl -X POST "https://api.sakuranbo.ai/v1/chat/completions" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $SAKURANBO_API_KEY" \ -d '{ "model": "sakuranbo/cherrytext-1.0-flash", "messages": [ { "role": "system", "content": "You are a concise assistant." }, { "role": "user", "content": "Summarize the benefits of prepaid API credits in two lines." } ], "max_tokens": 2048}'const response = await fetch("https://api.sakuranbo.ai/v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.SAKURANBO_API_KEY}`, }, body: JSON.stringify({ "model": "sakuranbo/cherrytext-1.0-flash", "messages": [ { "role": "system", "content": "You are a concise assistant." }, { "role": "user", "content": "Summarize the benefits of prepaid API credits in two lines." } ], "max_tokens": 2048 }),});const data = await response.json();console.log(data);import os
import requests
response = requests.post( "https://api.sakuranbo.ai/v1/chat/completions", headers={ "Authorization": f"Bearer {os.environ['SAKURANBO_API_KEY']}", }, json={ "model": "sakuranbo/cherrytext-1.0-flash", "messages": [ { "role": "system", "content": "You are a concise assistant.", }, { "role": "user", "content": "Summarize the benefits of prepaid API credits in two lines.", }, ], "max_tokens": 2048, },)print(response.json())OpenAI SDK でも、同じリクエストを送れます。SDK のベース URL を https://api.sakuranbo.ai/v1 に、API キーを Sakuranbo AI のキーに設定します。
TypeScript の OpenAI SDK:
import OpenAI from "openai";
const apiKey = process.env.SAKURANBO_API_KEY;if (!apiKey) throw new Error("Set the SAKURANBO_API_KEY environment variable.");
const client = new OpenAI({ baseURL: "https://api.sakuranbo.ai/v1", apiKey,});
const completion = await client.chat.completions.create({ model: "sakuranbo/cherrytext-1.0-flash", messages: [ { role: "system", content: "You are a concise assistant." }, { role: "user", content: "Summarize the benefits of prepaid API credits in two lines." } ], max_tokens: 2048});
console.log(completion.choices[0]?.message.content);Python の OpenAI SDK:
import os
from openai import OpenAI
client = OpenAI( base_url="https://api.sakuranbo.ai/v1", api_key=os.environ["SAKURANBO_API_KEY"],)
completion = client.chat.completions.create( model="sakuranbo/cherrytext-1.0-flash", messages=[ { "role": "system", "content": "You are a concise assistant.", }, { "role": "user", "content": "Summarize the benefits of prepaid API credits in two lines.", }, ], max_tokens=2048,)
print(completion.choices[0].message.content)レスポンスは chat completion オブジェクトです。生成されたテキストは choices[0].message.content に入ります。usage には、トークン数と、リクエストにかかった金額である usage.cost が入ります。usage.cost は、micro USD の金額を amount_micro_usd に、10 進文字列の金額を amount_usd に持ちます。
5. 残高を確認する
Section titled “5. 残高を確認する”GET /v1/balance で残高を読み取ります。
curl "https://api.sakuranbo.ai/v1/balance" \ -H "Authorization: Bearer $SAKURANBO_API_KEY"const response = await fetch("https://api.sakuranbo.ai/v1/balance", { headers: { Authorization: `Bearer ${process.env.SAKURANBO_API_KEY}`, },});const data = await response.json();console.log(data);import os
import requests
response = requests.get( "https://api.sakuranbo.ai/v1/balance", headers={ "Authorization": f"Bearer {os.environ['SAKURANBO_API_KEY']}", },)print(response.json())available_usd は、新しいリクエストに使える金額です。held_usd は、実行中のリクエストが確保している金額です。いま送ったリクエストは終わっているため、確保は解除され、その料金はすでに available_usd から引かれています。
次のステップ
Section titled “次のステップ”価格はすべて USD 表示です。表示価格には当社のマージンを含みます。