账小二开放平台 API

通过API将账小二的AI记账能力集成到您的系统中

适用版本: v2.0+
支持格式: JSON
基础URL: https://api.zhangxiaoer.com
快速开始:本文档适用于具备基本HTTP接口调用能力的开发者。如需技术支持,请联系我们的技术团队

认证授权

所有接口请求都需要在HTTP Header中携带API Key进行身份认证。

安全提示:请勿在客户端代码中暴露API Key,建议在服务端调用接口。

获取API Key

企业版用户可在「设置 - 开发者」中获取API Key。如需开通API权限,请联系客服。

请求头格式

Authorization Header
Authorization: Bearer {your_api_key}
示例
GET /api/v2/accounts/list HTTP/1.1
Host: api.zhangxiaoer.com
Authorization: Bearer sk_live_51H8x...9j2K
Content-Type: application/json

核心接口

POST /api/v2/invoice/recognize

上传票据图片或PDF文件,AI自动识别票据内容并返回结构化数据。

请求参数

参数名 类型 必填 说明
file File 票据图片或PDF文件,支持jpg、png、pdf格式,最大10MB
account_id String 账套ID
invoice_type String 票据类型,可选值:vat(专票)、normal(普票)、receipt(收据)

响应参数

参数名 类型 说明
invoice_id String 票据唯一标识
amount Decimal 票据金额
category String 票据分类
entries Array 会计分录数组

代码示例

cURL
curl -X POST https://api.zhangxiaoer.com/api/v2/invoice/recognize \
  -H "Authorization: Bearer {your_api_key}" \
  -F "file=@/path/to/invoice.pdf" \
  -F "account_id=acc_123456"
Python
import requests

url = "https://api.zhangxiaoer.com/api/v2/invoice/recognize"
headers = {
    "Authorization": "Bearer {your_api_key}"
}
files = {
    "file": open("/path/to/invoice.pdf", "rb")
}
data = {
    "account_id": "acc_123456"
}

response = requests.post(url, headers=headers, files=files, data=data)
print(response.json())
POST /api/v2/voucher/create

创建会计凭证,支持多借多贷的复合分录。

请求参数

参数名 类型 必填 说明
account_id String 账套ID
date String 凭证日期,格式:YYYY-MM-DD
entries Array 分录数组,每条包含:abstract(摘要)、account_code(科目编码)、debit(借方金额)、credit(贷方金额)
attachments Array 附件ID数组

响应参数

参数名 类型 说明
voucher_id String 凭证唯一标识
voucher_no String 凭证号

代码示例

Python
import requests

url = "https://api.zhangxiaoer.com/api/v2/voucher/create"
headers = {
    "Authorization": "Bearer {your_api_key}",
    "Content-Type": "application/json"
}
payload = {
    "account_id": "acc_123456",
    "date": "2024-03-15",
    "entries": [
        {
            "abstract": "采购办公用品",
            "account_code": "560203",
            "debit": 500.00,
            "credit": 0
        },
        {
            "abstract": "支付办公用品款",
            "account_code": "1002",
            "debit": 0,
            "credit": 500.00
        }
    ]
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())
GET /api/v2/accounts/list

获取当前用户有权限访问的所有账套列表。

响应参数

参数名 类型 说明
accounts Array 账套数组
accounts[].id String 账套ID
accounts[].name String 账套名称
accounts[].status String 账套状态:active(启用)、frozen(冻结)
accounts[].created_at String 创建时间

代码示例

cURL
curl -X GET https://api.zhangxiaoer.com/api/v2/accounts/list \
  -H "Authorization: Bearer {your_api_key}"
响应示例
{
  "accounts": [
    {
      "id": "acc_123456",
      "name": "某某科技有限公司",
      "status": "active",
      "created_at": "2023-06-15T08:30:00Z"
    },
    {
      "id": "acc_789012",
      "name": "某某贸易公司",
      "status": "active",
      "created_at": "2023-08-20T10:00:00Z"
    }
  ]
}
GET /api/v2/report/{type}

获取财务报表数据,支持资产负债表、利润表、现金流量表。

路径参数

参数名 类型 必填 说明
type String 报表类型:balance(资产负债表)、profit(利润表)、cashflow(现金流量表)

查询参数

参数名 类型 必填 说明
account_id String 账套ID
start_date String 开始日期,格式:YYYY-MM-DD
end_date String 结束日期,格式:YYYY-MM-DD

响应参数

参数名 类型 说明
report_data Object 报表数据,根据报表类型返回不同结构
period Object 报表期间,包含start_date和end_date

代码示例

cURL
curl -X GET "https://api.zhangxiaoer.com/api/v2/report/profit?account_id=acc_123456&start_date=2024-01-01&end_date=2024-03-31" \
  -H "Authorization: Bearer {your_api_key}"

错误码说明

接口返回的错误码及其含义如下:

错误码 说明 处理建议
400 参数错误 检查请求参数格式是否正确
401 未授权 检查API Key是否有效或已过期
403 权限不足 确认账号是否具有该接口访问权限
404 资源不存在 检查资源ID是否正确
429 请求过于频繁 降低请求频率,或联系客服提升限额
500 服务器错误 联系技术支持
错误响应示例
{
  "error": {
    "code": 400,
    "message": "参数错误:account_id不能为空",
    "type": "invalid_request_error"
  }
}

SDK下载

我们提供了多语言的SDK,帮助您更快集成账小二API。

Python SDK
v2.1.0 · 支持 Python 3.8+
Java SDK
v2.0.5 · 支持 Java 8+
Node.js SDK
v2.0.8 · 支持 Node.js 14+
PHP SDK
v2.0.3 · 支持 PHP 7.4+
安装说明:SDK支持通过包管理器安装,详情请查看开发文档