Api design

From qauth-labs/qauth · View interactive version

Api design is an AI agent skill from the GitHub repository qauth-labs/qauth. Designs and reviews HTTP APIs with resource-oriented REST, schema validation, and consistent error responses. Use when adding or changing auth-server routes, request/response schemas, or when the user asks about API design, versioning, or OpenAPI.

How to use this skill

Skill file

API Design (QAuth)

Standards-aligned HTTP API design for QAuth auth-server. APIs are resource-oriented, schema-validated with Zod, and return consistent success and error payloads. Use this skill when defining new endpoints, schemas, or reviewing API contracts.

When to Use This Skill

Quick Reference

| Concern | QAuth pattern |

| ---------------- | ---------------------------------------------------------------------------------------------- |

| Validation | Zod schemas in app/schemas/; fastify-type-provider-zod on routes |

| Route schema | schema: { body?, querystring?, params?, response: { 200: schema } } |

| Success | 200/201/204 with typed body; no extra envelope unless needed |

| Errors | RFC 9457 problem+json (code, trace_id); { error, error_description? } on OAuth endpoints |

| OAuth tokens | RFC 6749: snake_case (access_token, refresh_token, expires_in, token_type) |

| Other JSON | camelCase for app-specific fields (e.g. emailVerified, realmId) |

| Versioning | Not in use yet; prefer path /v1/ or header when introduced |

Route Structure (QAuth)

  1. Schemas first

Define Zod schemas in app/schemas/ (e.g. auth.ts, oauth.ts, common.ts). Export schema and inferred type (z.infer<typeof schema>). Use z.email(), z.uuid(), z.url() (Zod v4 standalone validators).

  1. Register schema on route

Use fastify.withTypeProvider<ZodTypeProvider>().get|post|...(path, { schema, config }, handler). Set schema.body, schema.querystring, schema.params, and schema.response (e.g. response: { 200: responseSchema }). Fastify validates and types request/response.

  1. Rate limiting

Use config.rateLimit with max, timeWindow, and optional keyGenerator (e.g. by request.ip). Values come from env (e.g. env.LOGIN_RATE_LIMIT).

  1. Errors

Throw domain errors from @qauth-labs/shared-errors. Error handler maps them to HTTP status and the standard error JSON shape. For validation, rely on Fastify validation (schema); it produces 400 with details.

  1. Security

Keep handlers stateless. Use constant-time checks and minimum response time where needed (e.g. login). Do not log secrets or tokens.

HTTP Methods and Status Codes

| Method | Use |

| ------ | -------------------------------------------------- |

| GET | Read resource or list; idempotent, cacheable |

| POST | Create resource or action (login, token, register) |

| PUT | Replace resource (idempotent) |

| PATCH | Partial update |

| DELETE | Remove resource |

| Status | Use |

| ------ | ----------------------------------------------------------------- |

| 200 | Success with body |

| 201 | Created (e.g. register) |

| 204 | Success, no body |

| 400 | Bad request / validation error |

| 401 | Unauthorized (missing or invalid auth) |

| 403 | Forbidden (valid auth, insufficient permission) |

| 404 | Not found |

| 422 | Unprocessable entity (semantic validation) |

| 429 | Too many requests (rate limit; set Retry-After when applicable) |

| 500 | Internal server error |

| 503 | Service unavailable (e.g. health unhealthy) |

Error Response Shape

Two formats, chosen by the route's config.errorFormat (see plugins/error-handler.ts, ADR-019 Decision 7, #513):

Use custom error classes from @qauth-labs/shared-errors; api-conventions/error-classes.ts maps each class to its code. Declare a route's by-design error statuses with 'x-qauth-errors': [400, 401] in its schema; the OpenAPI document references Problem or OAuthError for them. The codes are listed on the docs site's reference/problem-types.md.

Naming Conventions

Checklist for New Endpoints

Additional Resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.