计费
创建充值结账会话
生成一个托管结账链接,为你的个人余额或团队共享的信用额度池添加信用额度。
POST
/
api
/
v1
/
billing
/
checkout
创建充值结账会话
curl --request POST \
--url https://api.example.com/api/v1/billing/checkout \
--header 'Content-Type: application/json' \
--data '
{
"amount_cents": 123,
"scope": "<string>",
"success_url": "<string>"
}
'import requests
url = "https://api.example.com/api/v1/billing/checkout"
payload = {
"amount_cents": 123,
"scope": "<string>",
"success_url": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({amount_cents: 123, scope: '<string>', success_url: '<string>'})
};
fetch('https://api.example.com/api/v1/billing/checkout', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/api/v1/billing/checkout",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'amount_cents' => 123,
'scope' => '<string>',
'success_url' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/api/v1/billing/checkout"
payload := strings.NewReader("{\n \"amount_cents\": 123,\n \"scope\": \"<string>\",\n \"success_url\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/api/v1/billing/checkout")
.header("Content-Type", "application/json")
.body("{\n \"amount_cents\": 123,\n \"scope\": \"<string>\",\n \"success_url\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/api/v1/billing/checkout")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"amount_cents\": 123,\n \"scope\": \"<string>\",\n \"success_url\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body为调用方的个人余额或组织的共享团队计费池充值。返回一个托管结账链接,
你的应用可以在新标签页中打开该链接,或将用户重定向到该链接。
认证
需要具有billing 权限范围的 Bearer 令牌。参见
认证。
请求体
integer
必填
要添加的金额,单位为美分。最低
500(5.00),最高‘100000‘(1,000.00)。string
"user"(个人场景下的默认值)或 "org"(当调用方位于某个团队内且拥有
manageBilling 权限时的默认值)。当取值为 "org" 时,充值将计入该团队在
organizations.polar_customer_id 上的共享信用额度池,此后所有团队范围内的
API 用量都将从该额度池中扣除,直至耗尽。string
可选。结账成功后将用户重定向到的 URL。默认值为
/dashboard/billing?topup=success(当 scope: "org" 时为
/dashboard/billing?topup=success&scope=team)。示例:个人充值
curl
curl -X POST "https://api.hitheo.ai/api/v1/billing/checkout" \
-H "Authorization: Bearer $THEO_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "amount_cents": 2500 }'
示例:团队充值
curl
curl -X POST "https://api.hitheo.ai/api/v1/billing/checkout" \
-H "Authorization: Bearer $THEO_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "amount_cents": 10000, "scope": "org" }'
SDK
// 为团队额度池充值。拥有 `manageBilling` 权限的管理员可以调用此接口;
// 其他权限范围的调用将返回 403。
const { checkout_url } = await theo.billingCheckout({
amount_cents: 10_000,
scope: "org",
});
window.open(checkout_url, "_blank");
响应
{
"checkout_url": "https://checkout.hitheo.ai/...",
"scope": "org"
}
错误
| 状态码 | 错误码 | 含义 |
|---|---|---|
| 400 | missing_amount | 缺少 amount_cents 或其值不是整数。 |
| 400 | amount_too_low | 低于 $5.00 的最低限额。 |
| 400 | amount_too_high | 超过 $1,000.00 的最高限额。 |
| 400 | org_context_required | 请求了 scope: "org",但当前没有活跃的团队。 |
| 402 | team_payment_method_required | 团队尚未完成计费充值,暂时无法创建团队密钥。请在完成一次结账后重试。 |
| 403 | permission_denied | 调用方在当前活跃团队中不具备 manageBilling 权限。 |
| 503 | billing_unavailable | 此部署环境尚未配置计费功能。 |
生命周期
- 客户端调用此端点,传入
amount_cents及可选的scope。 - 计费服务确定正确的客户对象(个人或团队),创建结账会话,并返回
checkout_url。 - 用户完成结账。
- 后台 webhook 将此次充值路由到正确的信用额度池(个人或团队), 并将该客户标记为已绑定支付方式。
- 后续的 API 用量将从已充值的额度池中扣除。
此页面对您有帮助吗?
创建充值结账会话
curl --request POST \
--url https://api.example.com/api/v1/billing/checkout \
--header 'Content-Type: application/json' \
--data '
{
"amount_cents": 123,
"scope": "<string>",
"success_url": "<string>"
}
'import requests
url = "https://api.example.com/api/v1/billing/checkout"
payload = {
"amount_cents": 123,
"scope": "<string>",
"success_url": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({amount_cents: 123, scope: '<string>', success_url: '<string>'})
};
fetch('https://api.example.com/api/v1/billing/checkout', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/api/v1/billing/checkout",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'amount_cents' => 123,
'scope' => '<string>',
'success_url' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/api/v1/billing/checkout"
payload := strings.NewReader("{\n \"amount_cents\": 123,\n \"scope\": \"<string>\",\n \"success_url\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/api/v1/billing/checkout")
.header("Content-Type", "application/json")
.body("{\n \"amount_cents\": 123,\n \"scope\": \"<string>\",\n \"success_url\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/api/v1/billing/checkout")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"amount_cents\": 123,\n \"scope\": \"<string>\",\n \"success_url\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body