Swagger
Swagger
An API documentation tool. OpenAPI spec. Automatic documentation generation.
Swagger
An API documentation tool. OpenAPI spec. Automatic documentation generation.
Swagger is a set of tools for designing, documenting, and testing RESTful APIs. It has now been standardized under the name OpenAPI Specification (OAS), and by defining the structure of an API in YAML or JSON, documentation and client SDKs can be generated automatically.
The Swagger/OpenAPI ecosystem consists of three main parts: Swagger Editor (writing specs), Swagger UI (interactive documentation), and Swagger Codegen (code generation). In the API-First design approach, the OpenAPI spec is defined before any code is written, and server stubs and client SDKs are automatically generated from it. Frontend and backend teams can develop in parallel, which greatly improves productivity.
# openapi.yaml
openapi: 3.0.3
info:
title: E-Commerce API
description: μ μμκ±°λ νλ«νΌ REST API
version: 1.0.0
contact:
name: API Support
email: api@example.com
servers:
- url: https://api.example.com/v1
description: Production
- url: https://staging-api.example.com/v1
description: Staging
paths:
/products:
get:
summary: μν λͺ©λ‘ μ‘°ν
tags:
- Products
parameters:
- name: category
in: query
schema:
type: string
description: μΉ΄ν
κ³ λ¦¬ νν°
- name: limit
in: query
schema:
type: integer
default: 20
maximum: 100
responses:
'200':
description: μ±κ³΅
content:
application/json:
schema:
type: object
properties:
data:
type: array
items:
$ref: '#/components/schemas/Product'
pagination:
$ref: '#/components/schemas/Pagination'
/products/{productId}:
get:
summary: μν μμΈ μ‘°ν
tags:
- Products
parameters:
- name: productId
in: path
required: true
schema:
type: string
format: uuid
responses:
'200':
description: μ±κ³΅
content:
application/json:
schema:
$ref: '#/components/schemas/Product'
'404':
$ref: '#/components/responses/NotFound'
components:
schemas:
Product:
type: object
required:
- id
- name
- price
properties:
id:
type: string
format: uuid
name:
type: string
minLength: 1
maxLength: 200
price:
type: number
format: float
minimum: 0
description:
type: string
category:
type: string
createdAt:
type: string
format: date-time
Pagination:
type: object
properties:
total:
type: integer
page:
type: integer
limit:
type: integer
responses:
NotFound:
description: 리μμ€λ₯Ό μ°Ύμ μ μμ
content:
application/json:
schema:
type: object
properties:
error:
type: string
message:
type: string
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: []
// app.js
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const app = express();
const swaggerDocument = YAML.load('./openapi.yaml');
// Swagger UI μ€μ
const swaggerOptions = {
customCss: '.swagger-ui .topbar { display: none }',
customSiteTitle: "E-Commerce API Docs",
swaggerOptions: {
persistAuthorization: true,
displayRequestDuration: true,
}
};
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, swaggerOptions));
// API μλν¬μΈνΈ
app.get('/products', (req, res) => {
// OpenAPI μ€νμ μ μλ λλ‘ μλ΅
res.json({
data: [...],
pagination: { total: 100, page: 1, limit: 20 }
});
});
app.listen(3000, () => {
console.log('API μλ² μ€ν μ€: http://localhost:3000');
console.log('API λ¬Έμ: http://localhost:3000/api-docs');
});