v2-api-conventions

Enforces response envelope, error, pagination, and validation contracts for /api/v2 endpoints.

29.5k|3.8k|Updated Jan 5, 2025
One-click install
npx skills add https://github.com/simstudioai/sim --skill v2-api-conventions
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: v2-api-conventions
Source: https://github.com/simstudioai/sim/tree/main/.agents/skills/v2-api-conventions
Command: npx skills add https://github.com/simstudioai/sim --skill v2-api-conventions

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It prevents the recurring classes of API contract bugs on the Sim v2 surface: inconsistent response envelopes, caller-reachable 500s, silently dropped query parameters, and pagination cursors that skip or repeat rows.

Core Features & Use Cases

  • Envelope and status-code rules: Mandates the exact {data} / {data, nextCursor} / {error:{code,message,details?}} shapes and defines precise semantics for 400, 403, 404, 409, 413, 429, and 500 responses.
  • Pagination contract: Requires every collection returning nextCursor to accept and apply limit + cursor, with keyset or offset cursor codecs bound to sort and filter stamps.
  • Strict validation: Enforces .strict() query and body schemas, mandatory query declarations, and shared v2PaginationFields helpers so no caller input can reach SQL untyped.
  • Use Case: When adding a new route under apps/sim/app/api/v2/, follow the contract-first workflow (contract, use case, route, OpenAPI description) and run the built-in checklist to ship a conformant endpoint.

Quick Start

Audit the route at apps/sim/app/api/v2/workspaces against the v2 API conventions and list every contract violation you find.

Frequently Asked Questions about v2-api-conventions

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add a new endpoint to the Sim v2 API?

Define the route contract first in lib/api/contracts/v2/<domain>.ts, then write the application use case, then build the route with defineV2JsonRoute declaring auth, rate limit, error policy, and present. Finally add the OpenAPI description and run bun run generate:openapi.

What response format does the Sim v2 API use?

Every v2 response uses one of three shapes: {data} for single resources, {data, nextCursor} for collections, and {error:{code,message,details?}} for failures. Bodies are built only by the v2Data, v2CursorList, and v2Error helpers, never by hand with NextResponse.json.

How does cursor pagination work in the v2 API?

Collections returning nextCursor must accept limit and cursor via v2PaginationFields. Keyset cursors are the default and stamp the sort and filters into the token; offset cursors are only for lists that cannot use one ordered SQL read. Keyset sorts must end in a unique id column.

Why does the v2 API return 404 instead of 403 for some resources?

Cross-tenant access failures are deliberately concealed as 404 so callers cannot confirm a resource exists in another workspace. A 403 is reserved for authenticated same-tenant callers with insufficient rights, and actionable 403s name their cause in error.details.code.

Why must query schemas be declared even for endpoints with no parameters?

An omitted query declaration means the query string is never validated, so unknown params are silently ignored. Every contract must declare query: noInputSchema when it takes no params, making strict rejection of undeclared parameters the default.

When should a v2 GET route set headSafe to false?

Set headSafe: false whenever a GET has side effects such as writing audit events or opening outbound connections, because Next.js aliases HEAD onto GET. The route then answers HEAD with a bodiless 200 before parsing or executing.