Getting Started
Docs Home | Next: Decorator Basics
This guide walks you through building a small Users API with bun-openapi. By the end you will have a running Bun server with automatic request validation, a generated OpenAPI 3.0 spec, and live Swagger UI.
Prerequisites: Bun >= 1.0 installed.
Install
Install bun-openapi and one schema adapter. This guide uses class-validator, but you can swap it for TypeBox, Zod, or Valibot later.
1) Define a Model
Start by defining the data shapes your API will use. These classes serve double duty — they are TypeScript types for your code and the source for OpenAPI schema definitions.
TIP: With
class-validator, you can add validation constraints directly on the DTO fields. These constraints are enforced at runtime and also appear in the generated OpenAPI spec.
2) Create a Controller
Controllers define your routes. The @Route decorator sets the base path, and HTTP-verb decorators (@Get, @Post, ...) register individual endpoints. Parameter decorators like @Body bind and validate incoming data automatically.
Let's break down what's happening:
@Route("/users")sets/usersas the base path for all methods in this class.@Get()combined with the base route creates aGET /usersendpoint.@Post()createsPOST /users.@Summary(...)becomes the endpoint summary in generated OpenAPI docs.@Returns(201, User, ...)tells bun-openapi what the success response looks like — both for documentation and optional response validation.@Body(CreateUser)validates the incoming JSON body against theCreateUserschema before the method executes.
TIP: If validation fails (e.g. a missing
namefield), the framework automatically returns a 400 error with details. You don't need to write any validation logic yourself.
3) Start the Server
Wire everything together with createApp() and pass the result to Bun.serve:
createApp() does several things at startup:
- Reads decorator metadata from your controllers.
- Builds a Bun route map with validation, guards, and interceptors wired in.
- Generates an OpenAPI 3.0 document from the collected metadata.
- Optionally serves Swagger UI and the raw spec.
4) Open Generated Docs
Start your server and visit:
- Swagger UI: http://localhost:3000/docs/swagger/
- OpenAPI JSON: http://localhost:3000/docs/swagger/openapi.json
You should see your Users endpoints listed with their request/response schemas — all generated from the decorators you wrote. Try sending a POST /users with an invalid body to see validation in action.
What's Next?
Now that you have a running API, explore deeper topics:
- Decorator Basics — understand the full decorator model and how layers compose.
- Request Binding — learn about
@Param,@Query,@Header, and scalar vs. whole-object binding. - Validation and Schemas — swap adapters, add constraints, and understand how validation integrates with OpenAPI.
- Error Handling — customize error responses with
errorFormatterand built-in exceptions. - Dependency Injection — organize code with providers and service classes.
- Guards and Security — add authentication and authorization to your endpoints.
- Descriptions and Metadata — enrich your OpenAPI docs with summaries, descriptions, and operation IDs.
Runnable Example
See examples/02_crud/server.ts for a complete working setup.