Tutorials
Getting Started
Build your first OpenAPI spec with spac in 5 minutes
Install
npm install @spec-spac/spac @sinclair/typeboxpnpm add @spec-spac/spac @sinclair/typeboxbun add @spec-spac/spac @sinclair/typeboxCreate 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 apiopenapi: 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: stringRun it
npx tsx api.tspnpm dlx tsx api.tsbunx tsx api.tsThis outputs a complete OpenAPI 3.1.2 JSON document with:
- All routes under
/pets Petschema hoisted tocomponents.schemaswith$refpointers- Tags, summaries, and parameter definitions
Next steps
- Defining Routes — learn about route configuration options
- API Reference — full type reference for the
Apiclass