API 快速入门
几分钟内完成你的第一个 Mobyform API 调用。
API 快速入门
本指南将引导你完成第一个 Mobyform API 调用。几分钟内,你将学会列出表单、创建新表单、读取提交数据和设置 Webhook。
需要 Pro 及以上套餐
第一步:生成 API 密钥
- 登录 Mobyform
- 进入 「设置」 → 「开发者」
- 点击 「生成 API 密钥」
- 为密钥起一个描述性名称(如 "后端集成")
- 立即复制密钥 — 之后将不再显示
请妥善保管 API 密钥。不要将其暴露在前端代码、公共仓库或客户端应用中。
第二步:发起第一个请求
使用 API 密钥列出你的表单。所有请求使用基础 URL https://api.mobyform.com,并需要 Authorization 请求头。
cURL
curl -X GET https://api.mobyform.com/api/forms \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json"JavaScript / Node.js
const response = await fetch('https://api.mobyform.com/api/forms', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
});
const data = await response.json();
console.log(data);Python
import requests
response = requests.get(
'https://api.mobyform.com/api/forms',
headers={
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
}
)
data = response.json()
print(data)响应示例
{
"data": [
{
"key": "abc123",
"title": "客户反馈",
"status": "published",
"createdAt": "2024-06-01T08:00:00Z",
"submissions": 142
},
{
"key": "def456",
"title": "活动报名",
"status": "draft",
"createdAt": "2024-06-10T14:30:00Z",
"submissions": 0
}
],
"total": 2,
"page": 1,
"size": 20
}第三步:通过 API 创建表单
发送 POST 请求并附带表单结构来创建新表单。
cURL
curl -X POST https://api.mobyform.com/api/forms \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"title": "联系表单",
"fields": [
{
"type": "input",
"label": "姓名",
"required": true
},
{
"type": "email",
"label": "邮箱地址",
"required": true
},
{
"type": "textarea",
"label": "留言内容",
"required": true,
"placeholder": "我们能为您做什么?"
}
]
}'JavaScript / Node.js
const response = await fetch('https://api.mobyform.com/api/forms', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: '联系表单',
fields: [
{ type: 'input', label: '姓名', required: true },
{ type: 'email', label: '邮箱地址', required: true },
{ type: 'textarea', label: '留言内容', required: true, placeholder: '我们能为您做什么?' },
],
}),
});
const form = await response.json();
console.log('创建的表单:', form.key);Python
import requests
import json
response = requests.post(
'https://api.mobyform.com/api/forms',
headers={
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
data=json.dumps({
'title': '联系表单',
'fields': [
{'type': 'input', 'label': '姓名', 'required': True},
{'type': 'email', 'label': '邮箱地址', 'required': True},
{'type': 'textarea', 'label': '留言内容', 'required': True, 'placeholder': '我们能为您做什么?'},
],
})
)
form = response.json()
print('创建的表单:', form['key'])响应示例
{
"key": "ghi789",
"title": "联系表单",
"status": "draft",
"createdAt": "2024-06-15T10:00:00Z",
"fields": [
{ "id": "field_1", "type": "input", "label": "姓名", "required": true },
{ "id": "field_2", "type": "email", "label": "邮箱地址", "required": true },
{ "id": "field_3", "type": "textarea", "label": "留言内容", "required": true }
]
}第四步:读取表单提交数据
获取指定表单的提交数据,支持分页。
cURL
curl -X GET "https://api.mobyform.com/api/forms/abc123/data?page=1&size=10" \
-H "Authorization: Bearer YOUR_API_KEY"JavaScript / Node.js
const formKey = 'abc123';
const response = await fetch(
`https://api.mobyform.com/api/forms/${formKey}/data?page=1&size=10`,
{
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
},
}
);
const submissions = await response.json();
console.log(`共计: ${submissions.total} 条提交`);
for (const item of submissions.data) {
console.log(item.id, item.fields);
}Python
import requests
form_key = 'abc123'
response = requests.get(
f'https://api.mobyform.com/api/forms/{form_key}/data',
headers={'Authorization': 'Bearer YOUR_API_KEY'},
params={'page': 1, 'size': 10}
)
submissions = response.json()
print(f"共计: {submissions['total']} 条提交")
for item in submissions['data']:
print(item['id'], item['fields'])响应示例
{
"data": [
{
"id": "data_001",
"fields": {
"姓名": "张三",
"邮箱地址": "zhangsan@example.com",
"留言内容": "我想了解一下定价方案。"
},
"createdAt": "2024-06-15T12:00:00Z"
}
],
"total": 1,
"page": 1,
"size": 10
}分页参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
page | number | 1 | 页码 |
size | number | 20 | 每页数量(最大 100) |
sort | string | createdAt | 排序字段 |
order | string | desc | 排序方向(asc 或 desc) |
第五步:设置 Webhook
Webhook 在表单事件发生时实时通知你的服务器,无需轮询 API。
注册 Webhook
curl -X POST https://api.mobyform.com/api/webhooks \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://your-server.com/webhook/mobyform",
"events": ["form_data_add", "form_data_update", "form_data_delete"],
"headers": {
"X-Webhook-Secret": "your-secret-value"
}
}'Webhook 事件
| 事件 | 触发时机 |
|---|---|
form_data_add | 收到新的提交 |
form_data_update | 提交数据被修改 |
form_data_delete | 提交数据被删除 |
处理 Webhook 事件
以下是一个简单的 Node.js/Express 处理传入 Webhook 事件的示例:
const express = require('express');
const app = express();
app.use(express.json());
app.post('/webhook/mobyform', (req, res) => {
const secret = req.headers['x-webhook-secret'];
if (secret !== 'your-secret-value') {
return res.status(401).send('Unauthorized');
}
const { event, formKey, data } = req.body;
switch (event) {
case 'form_data_add':
console.log(`表单 ${formKey} 收到新提交:`, data.fields);
break;
case 'form_data_update':
console.log(`表单 ${formKey} 的提交 ${data.id} 已更新`);
break;
case 'form_data_delete':
console.log(`表单 ${formKey} 的提交 ${data.id} 已删除`);
break;
}
res.status(200).send('OK');
});
app.listen(3000);错误处理
请求出错时,API 返回标准错误响应:
{
"error": {
"code": "INVALID_REQUEST",
"message": "错误描述信息"
}
}常见错误码
| HTTP 状态码 | 错误码 | 含义 | 解决方法 |
|---|---|---|---|
400 | INVALID_REQUEST | 请求体格式错误或缺少必填字段 | 检查 JSON 结构和必填参数 |
401 | UNAUTHORIZED | API 密钥缺失或无效 | 检查 Authorization 请求头和密钥 |
403 | FORBIDDEN | API 密钥权限不足 | 在设置中检查密钥的权限范围 |
404 | NOT_FOUND | 表单或提交数据不存在 | 确认表单 key 或数据 ID |
429 | RATE_LIMITED | 请求过于频繁 | 降低请求频率;参见下方速率限制 |
500 | INTERNAL_ERROR | 服务器内部错误 | 稍后重试;持续出现请联系支持 |
速率限制
| 套餐 | 月度限制 |
|---|---|
| Pro | 1,000 次请求 |
| Business | 10,000 次请求 |
| Enterprise | 自定义 |
超过限制后,API 返回 429 Too Many Requests。响应中包含 Retry-After 头,指示何时可以再次请求。