本指南带你从新账户走到第一次生成。你将创建账户和 API 密钥,充值,发送一个对话请求,并查看余额。基础 URL 为 https://api.sakuranbo.ai/v1。
1. 创建账户
章节标题“1. 创建账户”在控制台中注册,并按注册界面的提示完成各步骤。
2. 创建 API 密钥
章节标题“2. 创建 API 密钥”打开控制台的 API 密钥页面,创建一个密钥。为密钥起一个名称。你也可以设置支出上限和到期日。详见余额与 API 密钥限额。
控制台只会在密钥创建后显示一次完整的密钥,请当时就复制。密钥以 cherry_ 开头。请把它保存在环境变量 SAKURANBO_API_KEY 中,本文档的所有示例都从这个变量读取密钥:
export SAKURANBO_API_KEY="paste-your-key-here"切勿把密钥放进客户端代码或公开仓库。详见认证。
3. 充值
章节标题“3. 充值”打开控制台的充值页面,使用银行卡或加密货币充值。页面会显示你可以使用的支付方式。默认的最低充值额为 $10.00。详情请参阅定价与计费。
4. 发送对话请求
章节标题“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 密钥。
OpenAI SDK(TypeScript):
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);OpenAI SDK(Python):
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 包含 token 数量和 usage.cost,后者是该请求的费用。usage.cost 以 amount_micro_usd 给出 micro USD 金额,并以 amount_usd 给出十进制字符串形式的金额。
5. 查看余额
章节标题“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 中扣除。
下一步
章节标题“下一步”所有价格均以 USD 计价,标价已包含我们的加价。