Chargebee
2026-03-27
新闻来源:网淘吧
围观:8
电脑广告
手机广告
Chargebee
通过托管的OAuth认证访问Chargebee API。管理订阅、客户、发票和计费工作流。
快速开始
# List customers
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/chargebee/api/v2/customers?limit=10')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
基础URL
https://gateway.maton.ai/chargebee/{native-api-path}
将{native-api-path}替换为实际的Chargebee API端点路径。网关会将请求代理到{subdomain}.chargebee.com(会自动替换为您的连接配置)并自动注入认证信息。
认证
所有请求都需要在Authorization头部携带Maton API密钥:
Authorization: Bearer $MATON_API_KEY
环境变量:将您的API密钥设置为MATON_API_KEY:
export MATON_API_KEY="YOUR_API_KEY"
获取您的API密钥
连接管理
请在以下地址管理您的Chargebee连接https://ctrl.maton.ai。
列出连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections?app=chargebee&status=ACTIVE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
创建连接
python <<'EOF'
import urllib.request, os, json
data = json.dumps({'app': 'chargebee'}).encode()
req = urllib.request.Request('https://ctrl.maton.ai/connections', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
获取连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应:
{
"connection": {
"connection_id": "21fd90f9-5935-43cd-b6c8-bde9d915ca80",
"status": "ACTIVE",
"creation_time": "2025-12-08T07:20:53.488460Z",
"last_updated_time": "2026-01-31T20:03:32.593153Z",
"url": "https://connect.maton.ai/?session_token=...",
"app": "chargebee",
"metadata": {}
}
}
在浏览器中打开返回的URL以完成OAuth授权。
删除连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}', method='DELETE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
指定连接
如果您有多个Chargebee连接,请使用Maton-Connection请求头来指定要使用哪一个:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/chargebee/api/v2/customers')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Maton-Connection', '21fd90f9-5935-43cd-b6c8-bde9d915ca80')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
如果省略,网关将使用默认的(最早创建的)活跃连接。
API参考
客户
列出客户
GET /chargebee/api/v2/customers?limit=10
获取客户
GET /chargebee/api/v2/customers/{customerId}
创建客户
POST /chargebee/api/v2/customers
Content-Type: application/x-www-form-urlencoded
first_name=John&last_name=Doe&email=john@example.com
更新客户
POST /chargebee/api/v2/customers/{customerId}
Content-Type: application/x-www-form-urlencoded
first_name=Jane
订阅
列出订阅
GET /chargebee/api/v2/subscriptions?limit=10
获取订阅
GET /chargebee/api/v2/subscriptions/{subscriptionId}
创建订阅
POST /chargebee/api/v2/subscriptions
Content-Type: application/x-www-form-urlencoded
plan_id=basic-plan&customer[email]=john@example.com&customer[first_name]=John
取消订阅
POST /chargebee/api/v2/subscriptions/{subscriptionId}/cancel
Content-Type: application/x-www-form-urlencoded
end_of_term=true
商品价格(产品目录 2.0)
列出商品价格
GET /chargebee/api/v2/item_prices?limit=10
商品
列出商品
GET /chargebee/api/v2/items?limit=10
发票
列出发票
GET /chargebee/api/v2/invoices?limit=10
下载发票 PDF
POST /chargebee/api/v2/invoices/{invoiceId}/pdf
托管页面
新订阅结账
POST /chargebee/api/v2/hosted_pages/checkout_new_for_items
Content-Type: application/x-www-form-urlencoded
subscription[plan_id]=basic-plan&customer[email]=john@example.com
门户会话
创建门户会话
POST /chargebee/api/v2/portal_sessions
Content-Type: application/x-www-form-urlencoded
customer[id]=cust_123
筛选
GET /chargebee/api/v2/subscriptions?status[is]=active
GET /chargebee/api/v2/customers?email[is]=john@example.com
GET /chargebee/api/v2/invoices?date[after]=1704067200
代码示例
JavaScript
const response = await fetch(
'https://gateway.maton.ai/chargebee/api/v2/customers?limit=10',
{
headers: {
'Authorization': `Bearer ${process.env.MATON_API_KEY}`
}
}
);
Python
import os
import requests
response = requests.get(
'https://gateway.maton.ai/chargebee/api/v2/customers',
headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'},
params={'limit': 10}
)
备注
- POST请求使用表单编码数据
- 嵌套对象使用括号表示法:
customer[email] - 时间戳是Unix时间戳
- 列表响应包含
next_offset用于分页 - 产品目录2.0:使用
item_prices和items - 重要提示:使用curl命令时,若URL包含方括号(
fields[]、sort[]、records[]),请使用curl -g以禁用通配符解析 - 重要提示:将curl输出通过管道传递给
jq或其他命令时,某些shell环境中可能无法正确展开如$MATON_API_KEY的环境变量。使用管道时可能出现“无效API密钥”错误。
错误处理
| 状态 | 含义 |
|---|---|
| 400 | 缺少Chargebee连接 |
| 401 | Maton API密钥无效或缺失 |
| 429 | 请求频率受限(每个账户每秒10次请求) |
| 4xx/5xx | 来自Chargebee API的透传错误 |
故障排除:API密钥问题
- 检查
MATON_API_KEY环境变量是否已设置:
echo $MATON_API_KEY
- 通过列出连接来验证API密钥是否有效:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
故障排除:无效的应用名称
- 确保您的URL路径以
chargebee开头。例如:
- 正确:
https://gateway.maton.ai/chargebee/api/v2/customers - 错误:
https://gateway.maton.ai/api/v2/customers
资源
文章底部电脑广告
手机广告位-内容正文底部
上一篇:Trend Watcher
下一篇:Eventbrite


微信扫一扫,打赏作者吧~