nocobase-app-plugin-api-keys
它能做什么
这项技能为 NocoBase 应用添加 API 密钥访问功能,使脚本和其他系统可以调用其 API。适合连接外部工具的开发者。
适用场景
- ✓当脚本或集成程序需要调用你的 NocoBase 应用 API 时。
- ✓当应用发出的 API 密钥被拒绝时。
- ✓当你需要以特定用户身份、并带有该用户角色的 API 密钥时。
技能链接
https://github.com/nocobase/nocobase/blob/v3-develop/packages/plugins/app-plugin-api-keys/skills/nocobase-app-plugin-api-keys/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我为 NocoBase 应用添加 API 密钥访问,让我每晚运行的脚本可以调用它的 API。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会注册 API Keys 插件及其数据库表。每个密钥都与其所有者的会话相匹配,因此脚本会以该用户的角色执行操作。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
API Keys App Plugin
Use this Skill when an App needs non-interactive callers — a script, a cron job,
a third-party integration — to reach its API as a real user, or when a key the
App issued is being rejected. The default configuration resolves a key into its owner’s Session and carries that user’s roles. Applications needing scoped credentials must use a separate configuration with enableSessionForAPIKeys: false and enforce their business rules on the server.
This package is Better Auth's
API Key plugin plus the
apikey migration. apiKey is wrapped
only to supply three defaults, all overridable; its name, options and behaviour
are Better Auth's, and Better Auth's documentation applies unchanged.
Public surfaces
- Server plugin: default export of
@nocobase/app-plugin-api-keys/server. Contributes theapikeytable migration and nothing else. apiKey(options?), plusAPI_KEY_TABLE_NAME,API_KEY_ERROR_CODESand the option types, from the same entry. Use these rather than adding@better-auth/api-keyto the App: the migration here matches the schema this version declares, and taking both from one package keeps them in step.- Client registration factory:
@nocobase/app-plugin-api-keys/client. It contributes no pages. apiKeyClient, re-exported from@better-auth/api-key/clientby the same entry.ApiKeySummaryand the expiry helpers, for the App's own key management page. Such a page readsuseAuthenticationClient().apiKey, typed through the authentication plugin'sAuthClientPluginRegistry, and is declared among the App's own routes.- HTTP API:
POST /api/auth/api-key/create,GET /api/auth/api-key/list,GET /api/auth/api-key/get,POST /api/auth/api-key/update, andPOST /api/auth/api-key/delete. All five act only on the caller's own keys.
The plugin contributes no NocoBase route of its own. The endpoints above are
Better Auth's, mounted by Authentication's /api/auth/* handler. Its server registers one thing: the access check that lets a valid key read the App's API document.
Register it
Both halves are required, and each fails differently on its own.
- Add the default export to the App's
server/plugins.tsandclient/plugins.ts. - Add
apiKey()topluginsinserver/config/auth.ts, andapiKeyClient()topluginsinclient/config/auth.ts. - Run
pnpm nocobase db apply.
Only step 1 leaves the apikey table created and no endpoints mounted. Only
step 2 mounts endpoints against a table that does not exist, and every call
fails at the database.
The three defaults
apiKey() supplies enableSessionForAPIKeys: true, rateLimit: { enabled: false } and requireName: true, so an App normally passes nothing.
enableSessionForAPIKeys is the one that has to be set. Better Auth defaults
it off, and with it off a key authenticates nothing: keys are still
issued, and every request carrying one answers 401. Nothing points at the
configuration, so do not override it without meaning to.
rateLimit is off because Better Auth's own default is 10 requests per key per
day, which is a quota for issuing keys rather than for using them. Pass
{ enabled: true, maxRequests: 1000, timeWindow: 60_000 } for a quota the App
actually wants.
All three are overridable, and passing all three back reproduces Better Auth's own behaviour exactly.
Call the API with a key
curl -H 'x-api-key: <key>' https://example.com/api/orders
An App route needs no change. auth.required() and auth.optional() resolve
the key into the owner's Session, and Authorization then evaluates that user's
roles. Read context.get('auth') exactly as for a cookie Session.
Pass apiKeyHeaders: ['x-api-key', 'authorization-key'] to accept a different
header.
To find out what the App's API offers, read its OpenAPI document with the same key: GET /api/swagger returns the JSON document and GET /api/swagger/docs the Swagger UI page. A signed-in session or a valid key may read it; anything else gets 401 with error.reason API_DOCS_UNAUTHENTICATED. The API key endpoints are listed there under /api/auth/api-key/..., tagged Authentication.
What a key can reach, and what it cannot
A key is its owner, so it reaches the Better Auth endpoints a Session reaches.
Verified against @better-auth/api-key 1.7.1 with only an x-api-key header:
| Endpoint
常见问题
脚本如何发送密钥?
每次请求时,在 x-api-key 请求头中发送密钥。
密钥可以有受限的权限吗?
默认设置下不行,默认设置会携带所有者的角色。技能另外说明了受限密钥的设置方法。
它会添加登录页面吗?
不会。它只添加 API 密钥访问功能。
详情
作者的原始描述
Register the API Keys plugin in a NocoBase App, let scripts and integrations call the App's API with an x-api-key header, or work out why a key is rejected.
repo: nocobase/nocobase
热度
- GitHub 星标
- 25k
- 排名
- #10088
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
nocobase-app-development
25k帮你的智能体为完整的 NocoBase 3 业务应用构建页面、路由、数据、权限和测试。
shadcn
25k帮你的智能体在 Web 项目中查找、添加、修复和美化 shadcn/ui 组件,并使用其官方文档和注册表。
nocobase-app-plugin-development
25k帮你的智能体在现有应用中创建和维护 NocoBase 3 插件,包括服务端、客户端、数据库和注册部分。
nocobase-app-upgrade
25k帮你的智能体把 NocoBase 应用升级到较新的模板版本,引入模板的更改,同时保留你自己的工作。