api

A set of rules for building web APIs, including URL structure, request and response names, authentication, permissions, errors, and generated TypeScript types.

In plain words
What is it for?
Use it when adding or changing routes, schemas, pagination, service code, authentication, error handling, or generated API types.
Why use it?
It gives a development team one consistent way to design and implement API endpoints. It also prevents hand-written client types from drifting away from the API definition.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/lawofall/agentcore/api
Clone the repo
git clone --depth 1 https://github.com/Lawofall/AgentCore

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 835 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00835
Opus 5 $0.00000 $0.00417
Sonnet 5 $0.00000 $0.00167
Haiku 4.5 $0.00000 $0.00084

Measured 2d ago against content hash a34217ce9452, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

api scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/api.mdc · 51 lines

What it actually says

API 开发规范

路由

  • 所有路由注册在 main.py,统一 prefix="/v1"
  • Router 是薄层:参数解析 → 调用 service → 返回结果
  • 分页参数:page: int = Query(1, ge=1)page_size: int = Query(20, ge=1, le=100)
  • 删除操作返回 {"status": "ok"}
  • RESTful 命名:GET /listPOST /createPUT /{id} 全量、PATCH /{id} 部分、DELETE /{id}

Schema 命名

类型 命名模式
列表项 / 详情 {Resource}Summary / {Resource}Detail
列表响应 {Resource}ListResponsedata + total
创建 / 更新 Create{Resource}Request / Update{Resource}Request
  • 主键统一 id,外键 {entity}_id,更新请求所有字段可选
  • 列表响应包含 datatotalpagepage_size
  • 时间字段序列化为 ISO 8601
  • 前端 TS 类型从 OpenAPI 生成,禁止手写(单一真相源 = schemas.py)。改 schema 后在仓库根跑 pnpm gen:types 一条命令:它先 dump apps/server/openapi.json,再经 openapi-typescript 生成 packages/contract-rest-types/src/api.generated.ts(各端 src/**/types/api.generated.ts 只是三行透传再导出,没有独立生成脚本)。生成物入仓——CI 跑同一命令后 git diff --exit-code 挡漂移,故改完必须一并提交;*.generated.ts 免 lint(biome ignore),无 postinstall 自动重生成。service 文件用 type X = components["schemas"]["X"] 取别名引用。
  • ✅ 已迁移:所有 service 的 REST 类型均用生成类型别名(type X = components["schemas"]["X"]);新增/改动一律走生成类型,勿新增手写 REST 类型。纯 SSE/事件载荷与客户端域模型(camelCase,经 toX 映射)不受此约束。例外:端点无 response_model(如 /readyz/version)时生成类型为无类型字典,保留手写并加注。带默认值的字段在生成类型里是可选(?),读取处按需 ?? 兜底

认证 & 权限

  • 注入:AuthUser(必须登录)/ OptionalUser(可选)→ 见 agentcore.deps
  • 权限在 service 层校验,不在 router 层

错误处理

  • 使用 agentcore.common.errorsnot_found / forbidden / bad_request
  • 统一响应:{"error": {"code": N, "message": "...", "detail": "..."}}

Service 层

  • ID 生成 str(uuid4())
  • ORM → Pydantic 转换放 service 私有方法
  • 写操作先查再改(验证存在性和权限)
  • 软删除检查:if not entity or entity.deleted_at: raise not_found(...)
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 51 lines · 0 tokens per session scan A a34217ce9452

Subscribe to this mod's changes

api is a cursor rule published in the GitHub repository Lawofall/AgentCore (78 stars, last pushed 2d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 835 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.