home / skills / blockmatic-icebox / basilic-old / openapi-ts-v0

openapi-ts-v0 skill

/.cursor/skills/openapi-ts-v0

This skill generates type-safe TypeScript API clients from OpenAPI specs using @hey-api/openapi-ts with Zod validation.

npx playbooks add skill blockmatic-icebox/basilic-old --skill openapi-ts-v0

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
2.6 KB
---
name: Hey API Codegen
description: |
  Generate TypeScript clients from OpenAPI specs using @hey-api/openapi-ts. Type-safe API clients with Zod schemas.
  
  Use when: generating TypeScript clients from OpenAPI specifications for frontend or API consumers.
---

# Skill: openapi-ts

## Scope

- Applies to: Generating TypeScript clients from OpenAPI 3.0 specifications using `@hey-api/openapi-ts`
- Does NOT cover: Writing OpenAPI specs, API server implementation (see [fastify](@cursor/skills/fastify-v5/SKILL.md))

## Assumptions

- `@hey-api/openapi-ts` v0+
- OpenAPI 3.0 specification format
- TypeScript v5+ with strict mode
- Zod for runtime validation (when using Zod schemas)

## Principles

- Generate clients from OpenAPI specs (single source of truth)
- Use generated Zod schemas for runtime validation
- Client methods are fully typed from OpenAPI spec
- Error responses are typed from OpenAPI error schemas
- Use `createClient` factory for client instantiation
- Configure output format and schema type in config file

## Constraints

### MUST

- Use `openapi-ts.config.ts` for configuration
- Generate clients before using in code
- Handle `response.error` for typed error responses

### SHOULD

- Use Zod schemas (`schemas.type: 'zod'`) for runtime validation
- Use Prettier formatting (`output.format: 'prettier'`)
- Generate TypeScript enums (`types.enums: 'typescript'`)

### AVOID

- Manually editing generated code
- Using generated clients without error handling
- Mixing generated and manual client code

## Interactions

- Consumes OpenAPI specs generated by [fastify](@cursor/skills/fastify-v5/SKILL.md)
- Works with [nextjs](@cursor/skills/nextjs-v16/SKILL.md) for API client usage
- Integrates with TanStack Query (see [React Query Integration](references/react-query-integration.md))

## Patterns

### Configuration Pattern

```typescript
// openapi-ts.config.ts
import { defineConfig } from '@hey-api/openapi-ts'

export default defineConfig({
  input: './openapi.json',
  output: {
    path: './src/gen',
    format: 'prettier',
  },
  types: {
    enums: 'typescript',
  },
  schemas: {
    type: 'zod',
  },
})
```

### Client Usage Pattern

```typescript
import { createClient } from './gen/client'

const client = createClient({
  baseUrl: 'https://api.example.com',
})

const response = await client.GET('/users/{id}', {
  params: { path: { id: '123' } },
})

if (response.error) {
  // Handle typed error
  return
}

// response.data is typed from OpenAPI spec
```

See [Config Template](templates/openapi-ts.config.ts) for complete example.

## References

- [React Query Integration](references/react-query-integration.md) - Using generated clients with TanStack Query

Overview

This skill generates TypeScript API clients from OpenAPI 3.0 specifications using @hey-api/openapi-ts. It produces fully typed client methods and optional Zod schemas for runtime validation, enabling type-safe API consumption in frontend or consumer code. The output is configured and generated before use, keeping the OpenAPI spec as the single source of truth.

How this skill works

You configure generation in openapi-ts.config.ts and run the generator to emit a TypeScript client and types into a chosen output path. The generated client includes methods typed from OpenAPI operations, typed error responses, and optional Zod validators when schemas.type is set to 'zod'. Use createClient to instantiate a client and inspect response.error for typed error handling.

When to use it

  • When you need a type-safe TypeScript client for a REST API defined in an OpenAPI 3.0 spec.
  • When you want runtime validation of responses using Zod schemas.
  • When you want to avoid manual client code and keep API types sync'd with the spec.
  • When integrating generated clients into frontend apps (Next.js, React) or API consumers.
  • When you need typed error handling driven by OpenAPI error schemas.

Best practices

  • Keep an openapi-ts.config.ts file and include generation in your build or codegen step.
  • Enable Zod schemas (schemas.type: 'zod') for runtime validation of responses.
  • Format generated output with Prettier (output.format: 'prettier') to keep diffs clean.
  • Generate TypeScript enums (types.enums: 'typescript') to preserve enum semantics.
  • Never manually edit generated files; treat them as ephemeral and regenerate from the spec.
  • Always check response.error and handle typed error payloads instead of assuming success.

Example use cases

  • Frontend Next.js app consuming a backend OpenAPI spec and using generated clients in server or client components.
  • React apps integrated with TanStack Query using generated methods as query/mutation functions.
  • A TypeScript SDK for third-party consumers where runtime validation with Zod is required.
  • Automated codegen in CI that regenerates clients when the OpenAPI spec changes.
  • Migrating from handwritten fetch wrappers to a single source of truth generated client.

FAQ

Do I have to use Zod schemas?

No. Zod is recommended for runtime validation, but you can configure schemas.type differently or omit them if you only need static types.

Where should I put the config file?

Place openapi-ts.config.ts at the project root and reference the input spec path and desired output.path so the generator can run consistently in builds or CI.