介绍
# Xero
通过托管的 OAuth 身份验证访问 Xero API。管理联系人、发票、付款、银行交易,并运行财务报表。
## 快速开始
```bash # List contacts python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://gateway.maton.ai/xero/api.xro/2.0/Contacts') 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/xero/{native-api-path} ```
将 `{native-api-path}` 替换为实际的 Xero API 端点路径。网关将请求代理到 `api.xero.com` 并自动注入您的 OAuth 令牌和 Xero-Tenant-Id 请求头。
## 身份验证
所有请求都需要在 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` 管理您的 Xero OAuth 连接。
### 列出连接
```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections?app=xero&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': 'xero'}).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": "xero", "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 ```
### 指定连接
如果您有多个 Xero 连接,请使用 `Maton-Connection` 请求头指定要使用的连接:
```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://gateway.maton.ai/xero/api.xro/2.0/Contacts') 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 参考
### 联系人
#### 列出联系人
```bash GET /xero/api.xro/2.0/Contacts ```
#### 获取联系人
```bash GET /xero/api.xro/2.0/Contacts/{contactId} ```
#### 创建联系人
```bash POST /xero/api.xro/2.0/Contacts Content-Type: application/json
{ "Contacts": [{ "Name": "John Doe", "EmailAddress": "[email protected]", "Phones": [{"PhoneType": "DEFAULT", "PhoneNumber": "555-1234"}] }] } ```
### 发票
#### 列出发票
```bash GET /xero/api.xro/2.0/Invoices ```
#### 创建发票
```bash POST /xero/api.xro/2.0/Invoices Content-Type: application/json
{ "Invoices": [{ "Type": "ACCREC", "Contact": {"ContactID": "xxx"}, "LineItems": [{ "Description": "Service", "Quantity": 1, "UnitAmount": 100.00, "AccountCode": "200" }] }] } ```
### 账户
#### 列出账户
```bash GET /xero/api.xro/2.0/Accounts ```
### 付款
#### 列出付款
```bash GET /xero/api.xro/2.0/Payments ```
### 银行交易
#### 列出银行交易
```bash GET /xero/api.xro/2.0/BankTransactions ```
### 报表
#### 损益表
```bash GET /xero/api.xro/2.0/Reports/ProfitAndLoss?fromDate=2024-01-01&toDate=2024-12-31 ```
#### 资产负债表
```bash GET /xero/api.xro/2.0/Reports/BalanceSheet?date=2024-12-31 ```
#### 试算平衡表
```bash GET /xero/api.xro/2.0/Reports/TrialBalance?date=2024-12-31 ```
### 组织
```bash GET /xero/api.xro/2.0/Organisation ```
## 发票类型
- `ACCREC` - 应收账款(销售发票) - `ACCPAY` - 应付账款(账单)
## 代码示例
### JavaScript
```javascript const response = await fetch( 'https://gateway.maton.ai/xero/api.xro/2.0/Contacts', { headers: { 'Authorization': `Bearer ${process.env.MATON_API_KEY}` } } ); ```
### Python
```python import os import requests
response = requests.get( 'https://gateway.maton.ai/xero/api.xro/2.0/Contacts', headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'} ) ```
## 注意事项
- `Xero-Tenant-Id` 请求头会自动注入 - 日期格式为 `YYYY-MM-DD`
### Python
```python import os import requests
response = requests.get( 'https://gateway.maton.ai/xero/api.xro/2.0/Contacts', headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'} ) ```
## 注意事项
- `Xero-Tenant-Id` 请求头会自动注入 - 日期格式为 `YYYY-MM-DD` - 可以使用数组在单个请求中创建多条记录 - 使用 `where` 查询参数进行筛选 - 重要:使用 curl 命令时,如果 URL 包含括号(`fields[]`、`sort[]`、`records[]`),请使用 `curl -g` 以禁用 glob 解析 - 重要:将 curl 输出通过管道传递给 `jq` 或其他命令时,某些 shell 环境中可能无法正确展开 `$MATON_API_KEY` 等环境变量。通过管道传输时,您可能会遇到“Invalid API key”错误。
## 错误处理
| 状态 | 含义 | |--------|---------| | 400 | 缺少 Xero 连接 | | 401 | Maton API 密钥无效或缺失 | | 429 | 请求速率受限(每账户每秒 10 次请求)| | 4xx/5xx | 来自 Xero 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 路径以 `xero` 开头。例如:
- 正确:`https://gateway.maton.ai/xero/api.xro/2.0/Contacts` - 错误:`https://gateway.maton.ai/api.xro/2.0/Contacts`
## 资源
- [Xero API 概述](https://developer.xero.com/documentation/api/accounting/overview) - [联系人](https://developer.xero.com/documentation/api/accounting/contacts) - [发票](https://developer.xero.com/documentation/api/accounting/invoices) - [账户](https://developer.xero.com/documentation/api/accounting/accounts) - [付款](https://developer.xero.com/documentation/api/accounting/payments) - [报表](https://developer.xero.com/documentation/api/accounting/reports) - [Maton 社区](https://discord.com/invite/dBfFAcefs2) - [Maton 支持](mailto:[email protected])