Xero
2026-03-24
新闻来源:网淘吧
围观:98
电脑广告
手机广告
Xero
通过托管的OAuth认证访问Xero API。管理联系人、发票、付款、银行交易,并运行财务报告。
快速开始
# 列出联系人
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/{原生API路径}
将{原生API路径}替换为实际的Xero API端点路径。网关会将请求代理到api.xero.com并自动注入您的OAuth令牌和Xero-Tenant-Id请求头。
认证
所有请求都要求在Authorization请求头中包含Maton API密钥:
Authorization: Bearer $MATON_API_KEY
环境变量:将您的API密钥设置为MATON_API_KEY。
export MATON_API_KEY="YOUR_API_KEY"
获取您的API密钥
- 登录或前往以下网址创建账户maton.ai
- 访问maton.ai/settings
- 复制您的API密钥
连接管理
在以下网址管理您的Xero OAuth连接https://ctrl.maton.ai。
列出连接
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
创建连接
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
获取连接
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": "xero",
"metadata": {}
}
}
打开返回的网址在浏览器中完成 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
指定连接
如果您有多个 Xero 连接,请通过Maton-Connection请求头指定要使用哪一个:
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 参考
联系人
列出联系人
GET /xero/api.xro/2.0/Contacts
获取联系人
GET /xero/api.xro/2.0/Contacts/{contactId}
创建联系人
POST /xero/api.xro/2.0/Contacts
Content-Type: application/json
{
"Contacts": [{
"Name": "John Doe",
"EmailAddress": "john@example.com",
"Phones": [{"PhoneType": "DEFAULT", "PhoneNumber": "555-1234"}]
}]
}
发票
列出发票
GET /xero/api.xro/2.0/Invoices
创建发票
POST /xero/api.xro/2.0/Invoices
Content-Type: application/json
{
"Invoices": [{
"Type": "ACCREC",
"Contact": {"ContactID": "xxx"},
"LineItems": [{
"Description": "服务",
"Quantity": 1,
"UnitAmount": 100.00,
"AccountCode": "200"
}]
}]
}
账户
列出账户
GET /xero/api.xro/2.0/Accounts
付款
列出付款
GET /xero/api.xro/2.0/Payments
银行交易
列出银行交易
GET /xero/api.xro/2.0/BankTransactions
报告
损益表
GET /xero/api.xro/2.0/Reports/ProfitAndLoss?fromDate=2024-01-01&toDate=2024-12-31
资产负债表
GET /xero/api.xro/2.0/Reports/BalanceSheet?date=2024-12-31
试算表
GET /xero/api.xro/2.0/Reports/TrialBalance?date=2024-12-31
组织
GET /xero/api.xro/2.0/Organisation
发票类型
ACCREC- 应收账款(销售发票)ACCPAY- 应付账款(账单)
代码示例
JavaScript
const response = await fetch(
'https://gateway.maton.ai/xero/api.xro/2.0/Contacts',
{
headers: {
'Authorization': `Bearer ${process.env.MATON_API_KEY}`
}
}
);
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以禁用通配符解析 - 重要提示:将 curl 输出通过管道传递给
jq或其他命令时,环境变量如$MATON_API_KEY在某些shell环境中可能无法正确展开。通过管道传递时,您可能会遇到“无效API密钥”错误。
错误处理
| 状态码 | 含义 |
|---|---|
| 400 | 缺少Xero连接 |
| 401 | 无效或缺少Maton API密钥 |
| 429 | 请求频率受限(每个账户10次/秒) |
| 4xx/5xx | 来自Xero 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路径以
xero开头。例如:
- 正确的:
https://gateway.maton.ai/xero/api.xro/2.0/Contacts - 错误的:
https://gateway.maton.ai/api.xro/2.0/Contacts
资源
文章底部电脑广告
手机广告位-内容正文底部


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