介绍
# Chargebee
通过托管的 OAuth 身份验证访问 Chargebee API。管理订阅、客户、发票和计费工作流。
## 快速开始
```bash # 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`:
```bash export MATON_API_KEY="YOUR_API_KEY" ```
### 获取 API 密钥
1. 在 [maton.ai](https://maton.ai) 登录或创建账户 2. 前往 [maton.ai/settings](https://maton.ai/settings) 3. 复制您的 API 密钥
## 连接管理
在 `https://ctrl.maton.ai` 管理您的 Chargebee 连接。
### 列出连接
```bash 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 ```
### 创建连接
```bash 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 ```
### 获取连接
```bash 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 ```
**响应:** ```json { "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 授权。
### 删除连接
```bash 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` 请求头指定要使用的连接:
```bash 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 参考
### 客户 (Customers)
#### 列出客户
```bash GET /chargebee/api/v2/customers?limit=10 ```
#### 获取客户
```bash GET /chargebee/api/v2/customers/{customerId} ```
#### 创建客户
```bash POST /chargebee/api/v2/customers Content-Type: application/x-www-form-urlencoded
first_name=John&last_name=Doe&[email protected] ```
#### 更新客户
```bash POST /chargebee/api/v2/customers/{customerId} Content-Type: application/x-www-form-urlencoded
first_name=Jane ```
### 订阅 (Subscriptions)
#### 列出订阅
```bash GET /chargebee/api/v2/subscriptions?limit=10 ```
#### 获取订阅
```bash GET /chargebee/api/v2/subscriptions/{subscriptionId} ```
#### 创建订阅
```bash POST /chargebee/api/v2/subscriptions Content-Type: application/x-www-form-urlencoded
plan_id=basic-plan&customer[email][email protected]&customer[first_name]=John ```
#### 取消订阅
```bash POST /chargebee/api/v2/subscriptions/{subscriptionId}/cancel Content-Type: application/x-www-form-urlencoded
end_of_term=true ```
### 项目价格 (Item Prices) (产品目录 2.0)
#### 列出项目价格
```bash GET /chargebee/api/v2/item_prices?limit=10 ```
### 项目 (Items)
#### 列出项目
```bash GET /chargebee/api/v2/items?limit=10 ```
### 发票 (Invoices)
#### 列出发票
```bash GET /chargebee/api/v2/invoices?limit=10 ```
#### 下载发票 PDF
```bash POST /chargebee/api/v2/invoices/{invoiceId}/pdf ```
### 托管页面 (Hosted Pages)
#### 结账新订阅
```bash POST /chargebee/api/v2/hosted_pages/checkout_new_for_items Content-Type: application/x-www-form-urlencoded
subscription[plan_id]=basic-plan&customer[email][email protected] ```
### 门户会话 (Portal Sessions)
#### 创建门户会话
```bash POST /chargebee/api/v2/portal_sessions Content-Type: application/x-www-form-urlencoded
customer[id]=cust_123 ```
## 筛选
```bash GET /chargebee/api/v2/subscriptions?status[is]=active GET /chargebee/api/v2/customers?email[is][email protected] GET /chargebee/api/v2/invoices?date[after]=1704067200 ```
## 代码示例
### JavaScript
```javascript const response = await fetch( 'https://gateway.maton.ai/chargebee/api/v2/customers?limit=10', { headers: { 'Authorization': `Bearer ${process.env.MATON_API_KEY}` } } ); ```
### Python
```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 请求使用 form-urlencoded 数据格式 - 嵌套对象使用方括号表示法:`customer[email]` - 时间戳为 Unix 时间戳 - 列表响应包含用于分页的 `next_offset` - 产品目录 2.0:使用 `item_prices` 和 `items` - 重要:使用 curl 命令时,如果 URL 包含方括号(`fields[]`、`sort[]`、`records[]`),请使用 `curl -g` 以禁用 glob 解析 - 重要:将 curl 输出通过管道传递给 `jq` 或其他命令时,`$MATON_API_KEY` 等环境变量可能无法在某些 shell 环境中正确展开。通过管道传输时可能会收到“无效的 API 密钥”错误。
## 错误处理
| 状态 | 含义 | |--------|---------| | 400 | 缺少 Chargebee 连接 | | 401 | Maton API 密钥无效或缺失 | | 429 | 请求频率受限(每个账户每秒 10 次请求) | | 4xx/5xx | 来自 Chargebee API 的透传错误 |
### 故障排除:API 密钥问题
1. 检查是否已设置 `MATON_API_KEY` 环境变量:
```bash echo $MATON_API_KEY ```
2. 通过列出连接来验证 API 密钥是否有效:
```bash 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 ```
### 故障排除:无效的应用名称
1. 确保您的 URL 路径以 `chargebee` 开头。例如:
- 正确:`https://gateway.maton.ai/chargebee/api/v2/customers` - 错误:`https://gateway.maton.ai/api/v2/customers`
## 资源
- [Chargebee API 概览](https://apidocs.chargebee.com/docs/api) - [客户](https://apidocs.chargebee.com/docs/api/customers) - [订阅](https://apidocs.chargebee.com/docs/api/subscriptions) - [发票](https://apidocs.chargebee.com/docs/api/invoices) - [托管页面](https://apidocs.chargebee.com/docs/api/hosted_pages) - [Maton 社区](https://discord.com/invite/dBfFAcefs2) - [Maton 支持](mailto:[email protected])