spac
Tutorials

Getting Started

Build your first OpenAPI spec with spac in 5 minutes

Install

npm install @spec-spac/spac @sinclair/typebox
pnpm add @spec-spac/spac @sinclair/typebox
bun add @spec-spac/spac @sinclair/typebox

Create your first spec

Create a file called api.ts:

import { Api, named } from '@spec-spac/spac'
import { Type } from '@sinclair/typebox'
const Pet = named('Pet', Type.Object({
  id: Type.String({ format: 'uuid' }),
  name: Type.String(),
  tag: Type.Optional(Type.String()),
}))
const api = new Api('3.1', 'Petstore', {
  version: '1.0.0',
  description: 'A sample pet store API',
})
api.group('/pets', g => {
  g.get('/').response(Type.Array(Pet)).summary('List all pets').tag('pets')
  g.post('/').body(Pet).response(Pet).summary('Create a pet').tag('pets')
  g.get('/:id')
    .params(Type.Object({ id: Type.String() }))
    .response(Pet)
    .summary('Get a pet by ID')
    .tag('pets')
})
export default api
openapi: 3.1.2
info:
  title: Petstore
  version: 1.0.0
  description: A sample pet store API
jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema
paths:
  /pets:
    get:
      tags:
        - pets
      summary: List all pets
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Pet"
    post:
      tags:
        - pets
      summary: Create a pet
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
  /pets/:id:
    get:
      tags:
        - pets
      summary: Get a pet by ID
      parameters:
        - name: id
          in: path
          schema:
            type: string
          required: true
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          format: uuid
          type: string
        name:
          type: string
        tag:
          type: string

Run it

npx tsx api.ts
pnpm dlx tsx api.ts
bunx tsx api.ts

This outputs a complete OpenAPI 3.1.2 JSON document with:

  • All routes under /pets
  • Pet schema hoisted to components.schemas with $ref pointers
  • Tags, summaries, and parameter definitions

Next steps

On this page