代码示例

curl、JavaScript 和 Python 代码片段

阅读时间 6 分钟
打印本章节

最常用 API 操作的复制粘贴示例。把你的 API 密钥放进 FMK(以 fmk_ 开头)。代码请原样保留:字段名、请求头名称和枚举值都属于协议,永远不要翻译。

curl

  • 验证密钥: curl -H "Authorization: Bearer $FMK" https://freemaint.com/api/v1/public-api/whoami
  • 列出工单: curl -X POST -H "Authorization: Bearer $FMK" -H "Content-Type: application/json" -d '{"query":"{ workOrders(limit:5){ id title status } }"}' https://freemaint.com/api/v1/graphql
  • 消耗一个库存单位: curl -X POST -H "Authorization: Bearer $FMK" -H "Content-Type: application/json" -d '{"query":"mutation { adjustStock(id:812, quantity:-1, reason:\"ERP sync\"){ id quantity } }"}' https://freemaint.com/api/v1/graphql

JavaScript (fetch)

使用 fetch,在 Node.js 或浏览器中: const r = await fetch('https://freemaint.com/api/v1/graphql', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.FMK}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ query: '{ workOrders(limit:5){ id title } }' }) }); const data = await r.json();

Python (requests)

使用 requests 库: import requests; r = requests.post('https://freemaint.com/api/v1/graphql', headers={'Authorization': f'Bearer {FMK}'}, json={'query': '{ workOrders(limit:5){ id title } }'}); print(r.json())

Python:条形码自助终端

扫描条码,查找备件,然后减去一个单位: import requests; H={'Authorization': f'Bearer {FMK}'}; U='https://freemaint.com/api/v1/graphql'; code=input('Scan: '); part=requests.post(U,headers=H,json={'query':'query($c:String!){ part(barcode:$c){ id name description quantity minQuantity maxStockLevel unitOfMeasure imageUrl area location{name} vendor{name website} } }','variables':{'c':code}}).json()['data']['part']; requests.post(U,headers=H,json={'query':'mutation($id:Int!){ adjustStock(id:$id,quantity:-1,reason:"Kiosk"){ id quantity } }','variables':{'id':int(part['id'])}})

提示

请务必在响应中检查 X-RateLimit-Remaining — 您的集成应在耗尽每日上限之前向您发出警告。

本页对您有帮助吗?