Bmob REST API 是 跨语言、跨平台 的通用接入方式:任何能发 HTTP 请求的环境都能用,不依赖任何 SDK。典型用法包括 Python/Go/PHP/C#/Rust/Ruby/Java 后端、数据迁移脚本、Bash one-liner、Deno Edge Functions、Serverless 函数等。
1. URL 形态固定: 所有接口在 https://api.codenow.cn/1/(Bmob 控制台 → 应用 → 设置 → 配置 中显示的 API 域名,一般为 api.codenow.cn)。版本号 /1/ 必须保留。
示例:https://api.codenow.cn/1/classes/Token — 对 Token 表做 CRUD 时路径即 /1/classes/Token。
POST /1/classes/<TableName> # 新增
GET /1/classes/<TableName>/<objectId> # 查单条
GET /1/classes/<TableName> # 查列表 / 条件查询
PUT /1/classes/<TableName>/<objectId> # 更新
DELETE /1/classes/<TableName>/<objectId> # 删除
POST /1/batch # 批量(≤ 50 条)
GET /1/cloudQuery # BQL 查询
POST /1/users # 注册
POST /1/login # 登录
POST /2/files/<fileName> # 上传文件
GET /1/timestamp # 服务器时间
完整快速参考见 references/url-cheatsheet.md。
2. 两种鉴权方式,均可用(与 hydrogen-js-sdk 3.0+ 的两种 Bmob.initialize 对应):
X-Bmob-Application-Id + X-Bmob-REST-API-Key。适合服务端、内网脚本、与 SDK 方式 B 共用同一套 Key 的项目。详见 shared/auth-headers.md。> 2.x 文档曾暗示前端「禁止」Application ID + REST API Key;当前两种鉴权在全端均可正常使用。公开 bundle 中 REST API Key 仍可被抓包,故公开端仍推荐优先 SDK 方式 A 或加密授权,而非硬性禁止方式 B。
3. POST/PUT 必须 Content-Type: application/json,body 用 JSON。Bmob REST 不支持 form-urlencoded。
4. GET 的 query 必须 URL encode:where / order / limit / skip / include / keys / count 都通过 query string 传,含 { / : 等字符必须 encode。
5. 默认查询返回 10 条,最大 1000。需要更多用 skip + limit 分页或 count=1。
bmob-database-javascript SDK(3.0+ 支持 Secret Key 或 Application ID 两种初始化);若必须手写 REST,公开端优先加密授权或自建 BFF。X-Bmob-Master-Key 头部使用,不要给前端。bmob-acl-and-roles(P1);REST 创建对象时 body 里加 "ACL": {...} 即可。/1/batch 单次最多 50 个子请求。公共头部(所有请求都要带,下方示例不再重复):
X-Bmob-Application-Id: <your-application-id>
X-Bmob-REST-API-Key: <your-rest-api-key>
Content-Type: application/json
curl -X POST 'https://api.codenow.cn/1/classes/GameScore' \
-H "X-Bmob-Application-Id: <id>" \
-H "X-Bmob-REST-API-Key: <key>" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}'
成功 201 Created + body:
{ "createdAt": "2011-08-20 02:06:57", "objectId": "e1kXT22L" }
curl -X GET 'https://api.codenow.cn/1/classes/GameScore/e1kXT22L' \
-H "X-Bmob-Application-Id: <id>" \
-H "X-Bmob-REST-API-Key: <key>"
curl -X GET 'https://api.codenow.cn/1/classes/GameScore' \
-H "X-Bmob-Application-Id: <id>" \
-H "X-Bmob-REST-API-Key: <key>" \
-G \
--data-urlencode 'where={"score":{"$gt":100}}' \
--data-urlencode 'limit=20' \
--data-urlencode 'skip=0' \
--data-urlencode 'order=-createdAt' \
--data-urlencode 'count=1'
返回:
{
"results": [ /* ... */ ],
"count": 123
}
完整 where 运算符列表($gt / $lt / $in / $nin / $exists / $regex / $inQuery / $or …)见 references/query-where-syntax.md。
curl -X PUT 'https://api.codenow.cn/1/classes/GameScore/e1kXT22L' \
-H "X-Bmob-Application-Id: <id>" \
-H "X-Bmob-REST-API-Key: <key>" \
-H "Content-Type: application/json" \
-d '{"score":73453}'
curl -X DELETE 'https://api.codenow.cn/1/classes/GameScore/e1kXT22L' \
-H "X-Bmob-Application-Id: <id>" \
-H "X-Bmob-REST-API-Key: <key>"
curl -X PUT 'https://api.codenow.cn/1/classes/Post/abc' \
-H "Content-Type: application/json" \
...
-d '{"likes":{"__op":"Increment","amount":1}}'
amount 支持负数。
curl -X POST 'https://api.codenow.cn/1/batch' \
-H "Content-Type: application/json" \
...
-d '{
"requests": [
{ "method": "POST", "path": "/1/classes/GameScore", "body": { "score": 1 } },
{ "method": "PUT", "path": "/1/classes/GameScore/aaa", "body": { "score": 9 } },
{ "method": "DELETE", "path": "/1/classes/GameScore/bbb" }
]
}'
每个子请求独立成功/失败,不会回滚。
REST 通过 __type 字段标识特殊类型:
{
// Pointer:一对多
"author": { "__type": "Pointer", "className": "_User", "objectId": "abc" },
// Date:日期(注意服务器存的格式)
"publishedAt": { "__type": "Date", "iso": "2024-08-21 18:02:52" },
// File:上传后返回的结构原样存
"cover": { "__type": "File", "group": "group1", "filename": "1.jpg", "url": "M00/01/14/x.jpg" },
// GeoPoint:地理位置
"location": { "__type": "GeoPoint", "latitude": 23.05, "longitude": 113.40 },
// Relation:多对多(实际操作通过 __op AddRelation / RemoveRelation)
"likedBy": { "__type": "Relation", "className": "_User" }
}
# 注册
curl -X POST 'https://api.codenow.cn/1/users' \
-H "Content-Type: application/json" \
... \
-d '{"username":"hello","password":"pwd123","email":"x@y.com"}'
# 登录(必须用 GET,参数走 query)
curl -X GET 'https://api.codenow.cn/1/login' \
-H "Content-Type: application/json" \
... \
-G \
--data-urlencode 'username=hello' \
--data-urlencode 'password=pwd123'
# 拿当前用户(带上 X-Bmob-Session-Token)
curl -X GET 'https://api.codenow.cn/1/users/<objectId>' \
-H "X-Bmob-Session-Token: <session-token>" \
...
完整用户系统接口见 references/users.md。
走独立的 /2/files/ 端点,body 是文件二进制:
curl -X POST 'https://api.codenow.cn/2/files/cover.jpg' \
-H "X-Bmob-Application-Id: <id>" \
-H "X-Bmob-REST-API-Key: <key>" \
-H "Content-Type: image/jpeg" \
--data-binary @/path/to/cover.jpg
浏览器 / 小程序直接调用 REST(不经 SDK)时,推荐加密授权;Application ID + REST API Key 简易授权同样可用(与 SDK 3.0 方式 B 一致),但 REST API Key 更易被抓包。签名规则:
sign = md5(url + timeStamp + SecurityCode + noncestr + body + SDKVersion)
完整 6 个头部、签名拼接顺序、Node 参考实现见 shared/md5-sign-algo.md。
| 语言 | 示例 |
|---|---|
| --- | --- |
| Python(requests) | references/lang-snippets/python.md |
| Go(net/http) | references/lang-snippets/go.md |
| PHP(Guzzle) | references/lang-snippets/php.md |
| C#(HttpClient) | references/lang-snippets/csharp.md |
如果用户配置了 Bmob MCP,generate_code 工具能直接生成 curl,覆盖 13 种 type(添加 / 删除 / 更新 / 条件查询 / 注册 / 登录 / SMS / 云函数 / 文件上传等)。优先调用它而不是手写 curl——这样能确保 URL pattern / Header / where 语法都是当前服务器最新版本。
| HTTP | code | 含义 |
|---|---|---|
| --- | --- | --- |
| 401 | — | App ID 或 REST API Key 错;或加密授权签名错 |
| 400 | 101 | 对象不存在 / 用户名密码不正确 |
| 400 | 105 | 字段名非法或是保留字段(objectId/createdAt/updatedAt/ACL) |
| 400 | 106 | Pointer 格式不对 |
| 400 | 107 | JSON 格式错 / Content-Type 不是 application/json |
| 400 | 117 | 纬度 / 经度越界 |
| 400 | 122 | 用户权限验证失败(ACL) |
| 400 | 202 | username 已被使用 |
| 400 | 209 | mobilePhoneNumber 已被使用 |
| 400 | 211 | 用户未登录 / 登录已过期 |
| 400 | 401 | 唯一索引重复值 |
| 400 | 402 | where 超字节限制 |
| 400 | 10076 | QPS 超限 |
| 500 | — | 服务端临时故障,稍后重试 |
完整错误码见 bmob-error-codes。
bmob-bql(P1)generate_code 工具:bmob-mcpbmob-error-codes共 1 个版本