Skip to main content

API 入门指南

与 ActRight API 集成所需的一切:身份验证、端点、webhook 和错误处理。

环境生产环境https://api.actright.com

身份验证

所有 API 请求都需要在 Authorization 请求头中提供 Bearer 令牌。有两种身份验证方式:

方式 A:API 密钥

在控制台 → API 密钥页面创建 API 密钥。密钥以 gsk_ 为前缀,可限定为 read、read_write 或 full 访问权限。

API 密钥身份验证
curl https://api.actright.com/v1/petitions \
  -H "Authorization: Bearer gsk_your_api_key_here"

方式 B:OAuth 2.0 令牌

对于代表用户操作的应用,请注册一个 OAuth 应用,并使用授权码流程获取 JWT 访问令牌。

OAuth 令牌身份验证
curl https://api.actright.com/v1/me \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."

安全提示:切勿将 API 密钥提交到版本控制。请使用环境变量或密钥管理器。具有 full 权限范围的 API 密钥可执行破坏性操作。

快速开始

列出请愿

请求
curl https://api.actright.com/v1/petitions?limit=5 \
  -H "Authorization: Bearer gsk_your_key"
响应
{
  "data": [
    {
      "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
      "title": "Save the Local Park",
      "slug": "save-the-local-park",
      "signatureCount": 1248,
      "signatureGoal": 5000,
      "status": "active"
    }
  ],
  "meta": { "total": 42, "limit": 5, "hasMore": true }
}

签署请愿

请求
curl -X POST https://api.actright.com/v1/petitions/save-the-local-park/signatures \
  -H "Authorization: Bearer gsk_your_key" \
  -H "Content-Type: application/json" \
  -d '{
    "comment": "I support this cause!",
    "isAnonymous": false
  }'
响应 (201 Created)
{
  "data": {
    "id": "a1b2c3d4-5678-90ab-cdef-1234567890ab",
    "petitionId": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
    "comment": "I support this cause!",
    "isAnonymous": false,
    "createdAt": "2026-02-10T12:00:00Z"
  }
}

创建请愿

请求
curl -X POST https://api.actright.com/v1/petitions \
  -H "Authorization: Bearer gsk_your_key" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Improve Public Transit",
    "description": "<p>We need better bus routes in our city.</p>",
    "categoryId": "uuid-of-category",
    "signatureGoal": 1000,
    "tags": ["transit", "infrastructure"]
  }'
响应 (201 Created)
{
  "data": {
    "id": "new-petition-uuid",
    "title": "Improve Public Transit",
    "slug": "improve-public-transit",
    "status": "draft",
    "createdAt": "2026-02-10T12:00:00Z"
  }
}

请愿创建后处于 draft 状态。使用 POST /v1/petitions/:slug/publish 使其公开可见。

速率限制

API 请求按 API 密钥或访问令牌进行速率限制,以确保所有开发者公平使用。

50
持续请求数/秒
100
突发上限
429
超限时的状态码

速率限制请求头

每个响应都包含这些请求头,方便你跟踪使用情况:

请求头说明
X-RateLimit-Limit当前时间窗口内允许的最大请求数
X-RateLimit-Remaining当前窗口内剩余的请求数
X-RateLimit-Reset窗口重置时的 Unix 时间戳
429 Too Many Requests 响应
{
  "type": "https://api.groundswell.io/errors/rate-limited",
  "title": "Rate limit exceeded",
  "status": 429,
  "detail": "You have exceeded 50 requests per second. Retry after 1.2s.",
  "instance": "/v1/petitions"
}

API 版本管理

ActRight API 采用基于 URL 的版本管理。所有端点都以版本号为前缀(例如 /v1/)。

当前版本

当前 API 版本为 v1。每个响应都包含一个版本请求头:

版本响应头
X-API-Version: 1

版本管理策略

方面策略
方案URL 路径前缀(/v1/、/v2/ 等)
弃用每个主版本在下一版本发布后仍支持 12 个月
破坏性变更通过开发者更新日志和弃用请求头进行通知
增量变更可以向现有版本添加新字段和端点,而无需升级版本号

提示:始终将你的集成固定到特定版本(例如 /v1/)。当新版本发布时,先针对它测试你的集成,然后再迁移。

嵌入组件

使用 iframe 或 oEmbed 协议将任意 ActRight 请愿嵌入你的网站。该组件完全自适应并支持主题定制。

iframe 嵌入

复制并粘贴以下 HTML 以嵌入请愿:

HTML
<iframe
  src="https://actright.com/embed/p/{slug}"
  width="100%"
  height="400"
  frameborder="0"
></iframe>

将 {slug} 替换为请愿的 slug(例如 save-the-local-park)。

oEmbed 端点

在 CMS 平台和富预览工具中使用 oEmbed:

oEmbed 请求
GET https://api.actright.com/v1/oembed?url=https://actright.com/p/{slug}&format=json

主题支持

嵌入组件默认使用浅色模式。追加 ?theme=dark 以使用深色模式:

深色模式嵌入
<iframe
  src="https://actright.com/embed/p/{slug}?theme=dark"
  width="100%"
  height="400"
  frameborder="0"
></iframe>

自适应行为

该组件通过 postMessage 自动调整高度。要启用自动调整大小,请在你的页面上添加此监听器:

自动调整大小监听器
window.addEventListener("message", (event) => {
  if (event.data?.type === "actright:resize") {
    const iframe = document.querySelector("iframe[src^='https://actright.com/embed/']");
    if (iframe) iframe.style.height = event.data.height + "px";
  }
});

WordPress:对于 WordPress 网站,请使用官方 ActRight 插件,实现一键嵌入体验。查看 WordPress 文档

Webhook 事件

在控制台 → Webhooks页面或通过 API 注册 webhook 端点。ActRight 会针对每个已订阅的事件向你的 URL 发送 POST 请求。

事件类型

事件说明
petition.created已创建新请愿(草稿)。
petition.updated请愿的标题、描述或设置已更新。
petition.published请愿已发布,现已公开可见。
petition.closed请愿被其创建者或管理员关闭。
petition.victory请愿被标记为已胜利。
signature.created有人签署了请愿。
signature.milestone达到了签名里程碑(例如 100、500、1000 个签名)。
signature.removed签名被签署者或管理员移除。
update.created请愿创建者发布了动态更新。
payment.completed一笔付款(推广或加速)已完成。
response.submitted决策者提交了正式回应。
response.published决策者的回应已发布。

投递请求头

每次 webhook 投递都包含这些请求头:

请求头说明
X-ActRight-SignatureHMAC-SHA256 签名:sha256={hex}
X-ActRight-Event事件类型(例如 signature.created)
X-ActRight-Delivery-Id用于去重的唯一投递 UUID
X-ActRight-Timestamp事件的 ISO 8601 时间戳

验证签名

Node.js 验证示例
import crypto from "crypto";

function verifyWebhook(payload, signature, secret) {
  const expected = "sha256=" +
    crypto.createHmac("sha256", secret)
      .update(payload)
      .digest("hex");
  return crypto.timingSafeEqual(
    Buffer.from(signature),
    Buffer.from(expected)
  );
}

重试策略:投递失败最多重试 5 次,采用指数退避。你的端点必须在 10 秒内返回 2xx 状态才会被视为成功。

错误格式

所有错误响应都遵循 RFC 7807 Problem Details 规范。每个错误都包含机器可读的 type URI、人类可读的 title 以及 HTTP status 代码。

字段类型说明
typestring标识错误类型的 URI
titlestring简短的人类可读摘要
statusnumberHTTP 状态码
detailstring错误的详细说明
instancestring生成该错误的请求路径
errorsarray?字段级验证错误(422 响应)

验证错误 (422)

{
  "type": "https://api.groundswell.io/errors/validation-failed",
  "title": "Validation failed",
  "status": 422,
  "detail": "One or more fields are invalid.",
  "instance": "/v1/petitions",
  "errors": [
    { "field": "title", "message": "Required", "code": "invalid_type" },
    { "field": "description", "message": "String must contain at least 10 character(s)", "code": "too_small" }
  ]
}

未找到 (404)

{
  "type": "https://api.groundswell.io/errors/not-found",
  "title": "Not found",
  "status": 404,
  "detail": "Petition with slug 'nonexistent' was not found.",
  "instance": "/v1/petitions/nonexistent"
}

身份验证错误 (401)

{
  "type": "https://api.groundswell.io/errors/unauthorized",
  "title": "Unauthorized",
  "status": 401,
  "detail": "Missing or invalid Bearer token.",
  "instance": "/v1/petitions"
}

API 演练场

直接在本页试用 API 端点。输入你的 API 密钥,选择一个端点并发送请求。

API Playground

实时
https://api.actright.com/v1/petitions

准备好开始构建了吗?

获取 API 密钥,几分钟内即可开始发起请求。