API v1

借助 Tracefix REST API,您的服务台、工单系统或会计软件可以创建工单、读取工单报告,并在发生变更时接收事件。基础 URL:https://app.tracefixapp.com/api/v1/。请求和响应均为 JSON(UTF-8,字段名采用 snake_case),仅支持 HTTPS。

API 和 webhook 包含在所有付费套餐中,试用期间也可使用。

API 应从服务器端调用:它不发送任何 CORS 头,因此浏览器页面无法直接调用。切勿将密钥放在前端代码中。

目录

认证与权限范围

公司管理员在 设置 → 集成与 API 中创建密钥。完整密钥仅在创建时显示一次:请将其复制到您工具的密钥存储中。Tracefix 只保存其哈希值;页面上显示密钥的开头部分、其权限范围以及最近一次使用时间。密钥以 trf_live_ 开头。

在每个请求的 Authorization 头中发送密钥:

Authorization: Bearer trf_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

每个密钥都有权限范围(scope)。只授予所连接工具实际需要的权限。超出密钥权限范围的请求会收到 403。

权限范围允许的操作
interventions:read列出和读取工单。
interventions:write创建、取消和评论工单。
customers:read列出和读取客户及其站点。
customers:write创建客户。
equipment:read列出设备。
invoices:read列出和读取贵公司 Tracefix 订阅的发票及其红字发票(贷项通知单)。
webhooks:read为将来的端点预留;目前不授予任何权限。

创建密钥时,默认选中所有读取权限范围和 interventions:write。密钥可随时在同一页面撤销;从下一个请求起即被拒绝(401)。在当前格式之前创建的密钥(tfx_…)仍可使用,并拥有所有权限范围;请尽可能用带权限范围的密钥替换它们。

约定

  • 标识符是不透明字符串:由表示类型的前缀、一个下划线和 8 个字符组成,例如 int_3k9x0p2a。请以字符串形式存储,不要尝试解码。类型不符的 id 按未找到处理。
    前缀对象
    int_工单
    cus_客户
    sit_站点
    eqp_设备
    inv_发票
    crn_红字发票(贷项通知单)
    usr_用户(技术人员)
    evt_webhook 事件
    whk_, key_webhook 端点、API 密钥(预留)
  • 日期采用 UTC 的 ISO 8601 格式,例如 2026-11-01T10:00:00Z。在查询参数中,不带时区的日期按 UTC 解析。
  • 金额为以分为单位的整数,并始终附带币种:"amount_ttc": 1800, "currency": "EUR" 表示 18.00 EUR。
  • 空值:文本字段为空时为 "";日期和可选对象为 null。

错误

错误使用标准 HTTP 状态码,响应体采用 RFC 9457 格式,内容类型为 application/problem+json。type 以稳定的错误码结尾,可在代码中据此判断;detail 说明具体情况;对于校验错误,errors 列出出错的字段。

HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json

{
  "type": "https://tracefixapp.com/docs/api/#validation_error",
  "title": "Validation error",
  "status": 422,
  "detail": "title is required.",
  "errors": [{"field": "title", "message": "title is required."}]
}
状态码错误码触发情形
400invalid_json, invalid_parameter请求体不是 JSON 对象,或某个查询参数或请求头无效(limit、cursor、updated_since、Idempotency-Key)。
401unauthorized密钥缺失、无效或已撤销。响应带有 WWW-Authenticate: Bearer。
402subscription_inactive, plan_without_api订阅未激活,或套餐不包含 API。
403account_suspended, insufficient_scope账户已被暂停,或密钥缺少该请求所需的权限范围。
404not_foundid 未知或格式错误,或该 id 属于其他公司。
405method_not_allowed该路径不支持此方法。Allow 头列出允许的方法。
409idempotency_in_progress使用相同 Idempotency-Key 的请求仍在处理中。请稍后重试。
422validation_error, idempotency_key_reused某个字段缺失或无效,或该 Idempotency-Key 已用于另一个请求。
429rate_limited已达到速率限制。请等待 Retry-After 中给出的秒数。
500internal_error我方发生意外错误。请稍后重试;如果问题持续,请联系支持。

分页与筛选

列表采用游标分页,按从新到旧排序。参数:

  • limit:每页条数,默认 50,最多 200(更大的值会被降为 200);
  • cursor:上一页的 next_cursor 值。

列表响应的形式为 {"data": [...], "next_cursor": "..."}。在最后一页,next_cursor 为 null。请将游标视为不透明字符串。

curl https://app.tracefixapp.com/api/v1/interventions?limit=100 \
  -H "Authorization: Bearer $TRACEFIX_KEY"

{"data": [ ... ], "next_cursor": "aW50XzNrOXgwcDJh"}

curl "https://app.tracefixapp.com/api/v1/interventions?limit=100&cursor=aW50XzNrOXgwcDJh" \
  -H "Authorization: Bearer $TRACEFIX_KEY"

筛选条件可与分页参数组合使用。updated_since(ISO 8601)返回在该日期时间或之后发生变更的记录,所有列表均支持;对于发票(开具后不再变更),它作用于开具日期。其他筛选条件在下文各端点中给出。若要保持副本同步,请记录上次运行的时间,并在下次运行时将其作为 updated_since 发送。

幂等性

每个 POST 都接受 Idempotency-Key 头(1 到 255 个字符,例如您生成的 UUID)。如果网络错误导致您不确定请求是否已成功,请使用相同的密钥再次发送:

  • 24 小时内使用相同密钥发送相同请求:再次返回第一次的响应,并带有 Idempotent-Replayed: true 头,不会重复创建任何内容;
  • 相同密钥但请求不同(路径或请求体不同):422 idempotency_key_reused;
  • 第一个请求仍在处理中:409 idempotency_in_progress;
  • 第一个请求以服务器错误(5xx)结束:该密钥不会被保留,您可以使用它重试。

密钥按公司保留 24 小时。每个新操作请使用新的密钥。

速率限制

限制按密钥计算,以一分钟为时间窗口。每个已认证的响应都带有以下头:RateLimit-Limit(窗口内允许的请求数)、RateLimit-Remaining(剩余请求数)和 RateLimit-Reset(距窗口重置的秒数)。超出限制时,响应为 429,并带有以秒为单位的 Retry-After 头。

版本管理

版本号位于路径中:/api/v1。在 v1 内,变更仅限于兼容的新增内容:新端点、新的可选参数、响应中的新字段、新事件。您的代码应忽略不认识的字段。不兼容的变更将以新版本发布,并至少提前 6 个月公告;届时旧版本的响应将带有 Deprecation 和 Sunset 头。

端点

方法与路径权限范围
GET /interventionsinterventions:read
POST /interventionsinterventions:write
GET /interventions/{id}interventions:read
POST /interventions/{id}/cancelinterventions:write
POST /interventions/{id}/commentsinterventions:write
GET /customerscustomers:read
POST /customerscustomers:write
GET /customers/{id}customers:read
GET /equipmentequipment:read
GET /invoicesinvoices:read
GET /invoices/{id}invoices:read
GET /openapi.json无(公开)

路径相对于 https://app.tracefixapp.com/api/v1。API 不提供更新(PATCH)或删除操作。

列出工单

GET /interventions · 权限范围 interventions:read

参数:limit、cursor、updated_since、status(new、scheduled、in_progress、completed、cancelled)、external_ref(您的外部工单编号)。

curl "https://app.tracefixapp.com/api/v1/interventions?external_ref=4821" \
  -H "Authorization: Bearer $TRACEFIX_KEY"

响应为工单对象列表:

{
  "data": [
    {
      "id": "int_3k9x0p2a",
      "number": "INT-2026-00142",
      "title": "AC unit in fault, server room",
      "status": "completed",
      "kind": "corrective",
      "priority": "high",
      "external_ref": "4821",
      "customer": {"id": "cus_7m2q8w1z", "name": "Northside Clinic"},
      "site": {"id": "sit_4h6t0b9c", "name": "Building B"},
      "equipment": [{"id": "eqp_9r1s5d3f", "name": "Rooftop unit 2", "serial_number": "SN-99812"}],
      "technicians": [{"id": "usr_2n8v6k4j", "name": "Sam Taylor", "email": "sam@company.example"}],
      "scheduled_start": "2026-11-03T08:00:00Z",
      "scheduled_end": "2026-11-03T10:00:00Z",
      "started_at": "2026-11-03T08:12:00Z",
      "finished_at": "2026-11-03T09:40:00Z",
      "work_done": "Replaced the condensate pump, alarm cleared.",
      "follow_up": "",
      "failed_checks": ["Leak check"],
      "custom_fields": {"Purchase order": "PO-4471", "Out of hours": "yes"},
      "signed": true,
      "signer_name": "J. Lewis",
      "report_url": "https://app.tracefixapp.com/p/r/xxxxxxxx/",
      "app_url": "https://app.tracefixapp.com/app/interventions/xxxx/",
      "created_at": "2026-11-02T14:05:00Z",
      "updated_at": "2026-11-03T09:41:00Z"
    }
  ],
  "next_cursor": null
}

report_url 是已签名报告的链接,仅在工单完成后才设置(否则为 null)。kind 取值为 corrective、preventive、installation、inspection、other 之一;priority 取值为 low、normal、high、urgent 之一。

创建工单

POST /interventions · 权限范围 interventions:write · 接受 Idempotency-Key

字段说明
title必填,最多 255 个字符。
customer必填。对于现有客户使用 {"id": "cus_…"}(或仅以字符串形式传 id);否则使用 {"email", "name", "phone"}:先按电子邮件、再按名称查找客户,若无匹配则创建(此时 name 为必填)。
description请求内容的文本。
external_ref, external_url您的外部工单编号(最多 120 个字符)及其链接。如果已有一个未结工单具有相同的 external_ref,则返回该工单,状态码为 200,而不会创建重复工单。
site_id该客户的一个站点(sit_…)。
equipment_ids, equipment_serial设备 id 列表(eqp_…),和/或一个序列号(若没有设备具有该序列号则忽略)。未提供 site_id 时,使用第一台设备所在的站点。
prioritylow、normal(默认)、high、urgent。
kindcorrective(默认)、preventive、installation、inspection、other。
scheduled_startISO 8601 日期时间。
requester提出请求者的 {"name", "email", "phone"}。
custom_fields您自己的字段(设置 → 工单字段),以标签或 id 为键:文本、数字、日期(YYYY-MM-DD)、是/否(true/false)或列表中的某个选项。不适用于该工单类型的字段会被忽略;未知字段返回 422。
curl https://app.tracefixapp.com/api/v1/interventions \
  -H "Authorization: Bearer $TRACEFIX_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 5f0c8e8e-2a41-4d7b-9a51-0e6f7c3b2d19" \
  -d '{
    "title": "AC unit in fault, server room",
    "description": "Customer reports alarm E5 since this morning.",
    "external_ref": "4821",
    "external_url": "https://helpdesk.example.com/tickets/4821",
    "customer": {"email": "facilities@clinic.example", "name": "Northside Clinic"},
    "equipment_serial": "SN-99812",
    "priority": "high",
    "requester": {"name": "J. Lewis", "email": "j.lewis@clinic.example"},
    "custom_fields": {"Purchase order": "PO-4471", "Out of hours": true}
  }'

响应 201 Created,附带新的工单对象(格式同上,"status": "new")。未知的 customer.id、site_id 或设备 id 返回 404。

读取工单

GET /interventions/{id} · 权限范围 interventions:read

curl https://app.tracefixapp.com/api/v1/interventions/int_3k9x0p2a \
  -H "Authorization: Bearer $TRACEFIX_KEY"

响应 200,附带工单对象。

取消工单

POST /interventions/{id}/cancel · 权限范围 interventions:write · 接受 Idempotency-Key

可选请求体:reason(最多 500 个字符)。已完成或已取消的工单将原样返回。

curl https://app.tracefixapp.com/api/v1/interventions/int_3k9x0p2a/cancel \
  -H "Authorization: Bearer $TRACEFIX_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reason": "Ticket closed by the customer."}'

响应 200,附带工单对象("status": "cancelled")。

评论工单

POST /interventions/{id}/comments · 权限范围 interventions:write · 接受 Idempotency-Key

请求体:message,必填,最多 5,000 个字符。评论会显示在工单的历史记录中。

curl https://app.tracefixapp.com/api/v1/interventions/int_3k9x0p2a/comments \
  -H "Authorization: Bearer $TRACEFIX_KEY" \
  -H "Content-Type: application/json" \
  -d '{"message": "Access code for the plant room: ask reception."}'

{"ok": true}

响应 201 Created。

列出客户

GET /customers · 权限范围 customers:read

参数:limit、cursor、updated_since、email(精确地址,不区分大小写)、q(名称的一部分)。

curl "https://app.tracefixapp.com/api/v1/customers?email=facilities@clinic.example" \
  -H "Authorization: Bearer $TRACEFIX_KEY"

{
  "data": [
    {
      "id": "cus_7m2q8w1z",
      "name": "Northside Clinic",
      "email": "facilities@clinic.example",
      "phone": "+44 20 0000 0000",
      "sites": [{"id": "sit_4h6t0b9c", "name": "Building B", "city": "London"}],
      "created_at": "2026-09-14T08:30:00Z"
    }
  ],
  "next_cursor": null
}

创建客户

POST /customers · 权限范围 customers:write · 接受 Idempotency-Key

请求体:name(必填,最多 200 个字符)、email、phone、address。

curl https://app.tracefixapp.com/api/v1/customers \
  -H "Authorization: Bearer $TRACEFIX_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "Riverside Offices", "email": "maintenance@riverside.example", "phone": "+44 20 0000 0001"}'

响应 201 Created,附带客户对象("sites": [])。

读取客户

GET /customers/{id} · 权限范围 customers:read

curl https://app.tracefixapp.com/api/v1/customers/cus_7m2q8w1z \
  -H "Authorization: Bearer $TRACEFIX_KEY"

响应 200,附带客户对象。

列出设备

GET /equipment · 权限范围 equipment:read

参数:limit、cursor、updated_since、serial(精确序列号)、site_id(sit_…)。

curl "https://app.tracefixapp.com/api/v1/equipment?serial=SN-99812" \
  -H "Authorization: Bearer $TRACEFIX_KEY"

{
  "data": [
    {
      "id": "eqp_9r1s5d3f",
      "name": "Rooftop unit 2",
      "serial_number": "SN-99812",
      "category": "hvac",
      "site": {"id": "sit_4h6t0b9c", "name": "Building B"},
      "created_at": "2026-09-14T08:35:00Z",
      "updated_at": "2026-10-20T15:02:00Z"
    }
  ],
  "next_cursor": null
}

列出发票

GET /invoices · 权限范围 invoices:read

这些是贵公司 Tracefix 订阅的发票及其红字发票,例如可用于导入您的会计软件。它们不是开给您自己客户的发票。PDF 仍可在应用的“计费”页面获取。

参数:limit、cursor、updated_since(在该日期时间或之后开具的发票)。

curl "https://app.tracefixapp.com/api/v1/invoices?updated_since=2026-10-01T00:00:00Z" \
  -H "Authorization: Bearer $TRACEFIX_KEY"

{
  "data": [
    {
      "id": "inv_5p0w7e2m",
      "number": "2026-0042",
      "issued_at": "2026-10-15T09:00:00Z",
      "description": "Pro plan, monthly",
      "period_start": "2026-10-15T09:00:00Z",
      "period_end": "2026-11-15T09:00:00Z",
      "amount_ht": 4900,
      "vat_rate": "20.00",
      "vat_amount": 980,
      "amount_ttc": 5880,
      "currency": "EUR",
      "vat_mention": "",
      "credit_notes": [
        {"id": "crn_8c3x1y6q", "number": "AV-2026-0003", "issued_at": "2026-10-20T11:00:00Z", "amount_ttc": 1200}
      ]
    }
  ],
  "next_cursor": null
}

amount_ht 为不含增值税的金额,amount_ttc 为含增值税的金额,均以分为单位。vat_rate 是以字符串表示的百分比。vat_mention 包含发票上印制的法定增值税说明(如有)。

读取发票

GET /invoices/{id} · 权限范围 invoices:read

curl https://app.tracefixapp.com/api/v1/invoices/inv_5p0w7e2m \
  -H "Authorization: Bearer $TRACEFIX_KEY"

响应 200,附带发票对象。

Webhook

webhook 会在事件发生时向您的工具发送 HTTPS POST 请求,例如在工单完成时更新或关闭外部系统中的对应工单。公司管理员在 设置 → 集成与 API 中添加端点:一个可从互联网访问的 https:// URL(私有和本地地址会被拒绝),以及要接收的事件(不选择任何事件即表示接收所有事件)。每个端点都有自己的签名密钥,显示在同一页面上。发送测试 按钮会发送一个 ping 事件。

事件发送时机data
intervention.created工单被创建。工单
intervention.scheduled工单被排期。工单
intervention.started技术人员开始处理。工单
intervention.completed工单完成(报告可通过 report_url 获取)。工单
intervention.cancelled工单被取消。工单
invoice.issued为贵公司开具了一张 Tracefix 订阅发票。发票
credit_note.issued开具了一张红字发票。红字发票

请求体是一个封装结构,其 data 包含与 API 格式相同的对象:

POST /hooks/tracefix HTTP/1.1
Content-Type: application/json
User-Agent: Tracefix-Webhooks/1.0
X-Tracefix-Event: intervention.completed
X-Signature: t=1793700000,v1=3f1c9a…e9a2

{
  "id": "evt_6d9f2a7h",
  "type": "intervention.completed",
  "created_at": "2026-11-03T09:41:00Z",
  "data": {"id": "int_3k9x0p2a", "number": "INT-2026-00142", "status": "completed", ...}
}

credit_note.issued 事件携带的 data 如下:

{
  "id": "crn_8c3x1y6q",
  "number": "AV-2026-0003",
  "issued_at": "2026-10-20T11:00:00Z",
  "description": "Refund, Pro plan",
  "amount_ht": 1000, "vat_rate": "20.00", "vat_amount": 200, "amount_ttc": 1200,
  "currency": "EUR", "vat_mention": "",
  "invoice": {"id": "inv_5p0w7e2m", "number": "2026-0042"}
}

使用事件 id 来忽略已处理过的投递。

验证签名

每个请求都带有 X-Signature: t=<unix time>,v1=<hex>(相同的值也会通过 X-Tracefix-Signature 发送)。该十六进制值是使用端点签名密钥对字符串 "<t>.<raw body>" 计算的 HMAC-SHA256。请基于接收到的原始请求体计算,在进行任何 JSON 解析之前,并拒绝与您的时钟相差超过 5 分钟的 t。

import hashlib
import hmac
import time

def verify(secret: str, raw_body: bytes, header: str, tolerance: int = 300) -> bool:
    try:
        parts = dict(p.split("=", 1) for p in header.split(","))
        t = int(parts["t"])
    except (KeyError, ValueError):
        return False
    if abs(time.time() - t) > tolerance:
        return False
    expected = hmac.new(secret.encode(), str(t).encode() + b"." + raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, parts.get("v1", ""))

响应与重试

请在几秒内返回任意 2xx 状态码(请求在 8 秒后超时);不会跟随重定向。其他任何结果均视为失败,投递将在 1、5、15、30、60、120、240、480 分钟后以及之后的 720 分钟后重试:总共 10 次尝试,历时约 28 小时,之后放弃。设置 → 集成与 API 中的 最近投递记录 表显示每次投递的事件、结果、尝试次数和下一次尝试时间。

OpenAPI 描述

API 的机器可读 OpenAPI 3.1 描述(包括 webhook)无需密钥即可在 https://app.tracefixapp.com/api/v1/openapi.json 获取。您可以将其导入 API 客户端或代码生成器。

调用日志

每次 API 调用都会记录并保留 30 天。公司管理员可在 设置 → 集成与 API 中查看最近的调用:日期、密钥名称、方法和路径、状态、耗时和 IP 地址。密钥、签名密钥以及请求或响应体从不记录;在查询字符串中,仅保留 limit、status、updated_since 和 site_id 的值,其他值均被屏蔽。