API 参考

接口都是 JSON,地址以 https://xabcloud.com 为根。

约定

除 GET /api/catalog 外,每个请求都带密钥;请求体是 JSON 时带 Content-Type:

curl https://xabcloud.com/api/instances \
  -H "Authorization: Bearer xabc_…"

出错时的回答,message 是可以直接转述给用户的中文:

{"error": {"message": "…", "status": 409}}

金额的单位是分。

套餐

接口说明
GET /api/catalog在售的套餐 plans、地域 regions、系统镜像 images、时长上限 terms,以及到期后数据保留的天数 grace_days

price_month_fen 是包月价,price_year_fen 是包年价。

订单

接口说明
POST /api/orders下单,开通或续费
GET /api/orders订单记录
GET /api/orders/{out_trade_no}一张订单
GET /api/orders/{out_trade_no}/qr.txt用方块字符画的付款码,原样打印在终端里;浅色背景的终端加 ?invert=1
GET /api/orders/{out_trade_no}/qr.svg付款码图片

开通一台主机:

{"kind": "new", "plan": "starter", "region": "us-west-2",
 "period": "month", "quantity": 1, "label": "博客",
 "ssh_public_key": "ssh-ed25519 AAAA…"}

给一台主机续费:

{"kind": "renew", "instance": "xc-…", "period": "month", "quantity": 1}

period 是 month 或 year,quantity 是几个月(1–11)或几年(1–3)。label 是可选的备注名,image 可省略。

回答里 order.out_trade_no 是订单号,order.amount_fen 是应付金额。payment.code_url 是微信支付链接,payment.pay_page 是给用户在浏览器里打开的付款页,payment.expires_at 是订单的失效时间。

order.status含义
pending等待付款
paid已付款,order.instance 是主机名
closed过期未付,需要重新下单
refunded已退款

主机

接口说明
GET /api/instances这个账户的全部主机
GET /api/instances/{name}一台主机和它的操作记录 events
PATCH /api/instances/{name}改备注名,{"label": "新备注名"}
POST /api/instances/{name}/reboot重启
PUT /api/instances/{name}/ports公网端口,整体替换
DELETE /api/instances/{name}?confirm={name}释放主机,需要密钥有这项权限

付款后主机的 instance.status 依次是 queued、creating、booting,约两分钟后到 running。这时 instance.ssh_command 是登录命令,instance.ipv4 是公网地址,instance.paid_until 是到期时间。

instance.status含义
queued、creating、booting开通中
running运行中
suspending、suspended到期停机,数据保留到 instance.release_at
resuming续费后正在重新启动
deleting、deleted释放中、已释放
failed开通遇到问题,平台已收到通知,已付的时长不受影响

公网端口

{"ports": [{"protocol": "tcp", "from": 22, "to": 22},
           {"protocol": "tcp", "from": 8000, "to": 8100}]}

protocol 是 tcp 或 udp。列表里没有的端口一律关闭,去掉 22 就无法 SSH。

账户

接口说明
GET /api/me当前账户,limits.max_instances 是主机数量上限;超出时下单回答 409

一次完整的开通

# 1. 看套餐
curl https://xabcloud.com/api/catalog

# 2. 下单,回答里有订单号
curl https://xabcloud.com/api/orders \
  -H "Authorization: Bearer xabc_…" \
  -H "Content-Type: application/json" \
  -d '{"kind":"new","plan":"starter","region":"us-west-2",
       "period":"month","quantity":1,
       "ssh_public_key":"ssh-ed25519 AAAA…"}'

# 3. 把付款码打印出来,请用户用微信扫
curl https://xabcloud.com/api/orders/订单号/qr.txt \
  -H "Authorization: Bearer xabc_…"

# 4. 每隔几秒查一次,直到 order.status 是 paid
curl https://xabcloud.com/api/orders/订单号 \
  -H "Authorization: Bearer xabc_…"

# 5. 每隔几秒查一次,直到 instance.status 是 running
curl https://xabcloud.com/api/instances/主机名 \
  -H "Authorization: Bearer xabc_…"