API 参考

Castimer REST API、嵌入组件、Webhooks 和快速入门指南的完整参考文档。

REST API

认证

所有 API 请求都需要在 Authorization 请求头中携带 Bearer 令牌。从控制面板获取您的 API 密钥。

Authorization: Bearer YOUR_API_KEY

基础 URL 和请求头

基础 URL:https://castimer.com/api/v1

请求头必需
AuthorizationBearer {api_key}
Content-Typeapplication/json是(POST/PATCH)
Acceptapplication/json

接口端点

GET/events

列出认证用户的所有计时器事件。

curl -X GET https://castimer.com/api/v1/events \
  -H "Authorization: Bearer YOUR_API_KEY"
POST/timers

创建新计时器。

curl -X POST https://castimer.com/api/v1/timers \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "会议计时器",
    "type": "countdown",
    "duration": 1800
  }'
GET/timers/:id

通过 ID 获取特定计时器。

curl -X GET https://castimer.com/api/v1/timers/tmr_abc123 \
  -H "Authorization: Bearer YOUR_API_KEY"
PATCH/timers/:id/state

更新计时器状态(启动、暂停、恢复、重置)。

curl -X PATCH https://castimer.com/api/v1/timers/tmr_abc123/state \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "state": "running" }'
DELETE/timers/:id

删除计时器。

curl -X DELETE https://castimer.com/api/v1/timers/tmr_abc123 \
  -H "Authorization: Bearer YOUR_API_KEY"

错误码

状态码含义
400错误请求 — 参数无效
401未授权 — API 密钥无效或缺失
404未找到 — 计时器不存在
422无法处理 — 验证错误
429请求过多 — 超出速率限制
500服务器内部错误

速率限制

套餐请求/分钟请求/天
免费版301,000
Pro12010,000
企业版600无限

SDK 和库

官方和社区 SDK,支持主流语言和框架。

JavaScript/TypeScript
官方GitHub
Python
官方GitHub
PHP
社区GitHub

嵌入组件

Widget URL 格式

Widget 通过单个 URL 提供服务,使用查询参数进行配置:

https://castimer.com/widget?type=countdown&title=My+Timer&duration=300&theme=dark&primary_color=6C63FF&lang=en

参数参考

参数类型默认值说明
typestringcountdown计时器类型:countdown、event、interval
titlestring-显示标题
durationnumber-倒计时时长(秒)
target_datestring-目标日期(ISO8601),仅 event 类型
timezonestringUTC时区,仅 event 类型
worknumber20工作时长(秒),仅 interval 类型
restnumber10休息时长(秒),仅 interval 类型
roundsnumber8轮次数,仅 interval 类型
themestringdark主题:light、dark、auto
primary_colorstring6C63FF主色调(hex 无 #)
langstringen语言:en、zh、ja、es、fr、de、ko
show_title0|11显示标题
show_date0|11显示日期(仅 event 类型)
alarm0|11完成时播放闹钟声音
show_brand0|11显示 Castimer 品牌(Pro)

iframe 集成

使用标准 iframe 元素嵌入组件:

<iframe
  src="https://castimer.com/widget?type=countdown&duration=300&theme=dark"
  style="width:360px;height:360px;border:none;"
  loading="lazy"
  title="倒计时计时器"
></iframe>

style — 设置宽度和高度(最小 100×200,最大 600×600)

loading="lazy" — 推荐用于性能优化

title — 无障碍标签

嵌入生成器工具

使用我们的可视化嵌入生成器 配置组件并即时生成 iframe 代码。无需手动构建 URL。

提示:生成器提供实时预览,您可以在复制代码之前准确查看组件的外观。

Webhooks

在计时器事件发生时获取实时通知。完整文档请参阅Webhooks 指南

概述

Webhooks 让 Castimer 能够在计时器事件发生时实时通知您的服务器——比如启动、暂停或归零。在创建或更新计时器时注册 Webhook URL。

事件类型

事件触发条件
timer.started计时器开始运行
timer.paused计时器被暂停
timer.resumed计时器从暂停状态恢复
timer.completed计时器归零
timer.reset计时器重置为初始时长
interval.round_end间隔计时器的一轮完成
interval.completed间隔计时器所有轮次完成

数据格式

{
  "event": "timer.completed",
  "timestamp": "2026-05-10T14:30:00Z",
  "timer_id": "tmr_abc123xyz",
  "title": "限时促销倒计时",
  "type": "event",
  "data": {
    "state": "completed",
    "duration": 1800,
    "actual_duration": 1803,
    "started_at": "2026-05-10T14:00:00Z",
    "completed_at": "2026-05-10T14:30:00Z"
  },
  "signature": "sha256=a8f3b2c..."
}

签名验证

Castimer 使用 HMAC-SHA256 对每个 Webhook 进行签名。签名包含在 X-Castimer-Signature 请求头中。

const crypto = require('crypto');

function verifyWebhook(payload, signature, secret) {
  const expected = crypto
    .createHmac('sha256', secret)
    .update(JSON.stringify(payload))
    .digest('hex');
  return `sha256=${expected}` === signature;
}

重试机制

如果您的端点返回非 2xx 响应或超时,Castimer 将使用指数退避策略重试:

11立即(首次)
221 分钟
335 分钟
4430 分钟
552 小时

快速开始

创建第一个计时器

  1. 1控制面板获取 API 密钥
  2. 2通过 API 创建计时器:
curl -X POST https://castimer.com/api/v1/timers \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "我的第一个计时器",
    "type": "countdown",
    "duration": 300
  }'
  1. 3使用返回的计时器 ID 来控制它(启动、暂停等)

嵌入组件

  1. 1打开嵌入生成器
  2. 2可视化配置计时器设置
  3. 3复制生成的 iframe 代码并粘贴到您的网站中

设置 Webhooks

  1. 1在您的服务器上创建接收 POST 请求的端点
  2. 2创建计时器时包含 webhook_url
  3. 3验证传入请求的 HMAC-SHA256 签名
curl -X POST https://castimer.com/api/v1/timers \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Webhook 计时器",
    "type": "countdown",
    "duration": 60,
    "webhook_url": "https://yourapp.com/webhooks/castimer"
  }'