Use Cases

OpenAPI/Swagger API 실전 가이드: Claude Code 활용 가이드

openapi/swagger api practical guide: Claude Code 활용. 실용적인 팁과 코드 예시를 포함합니다.

OpenAPI/Swagger定義をClaude Code로 효율화하기

API설계ファーストのアプローチでは、구현前にOpenAPI仕様を定義します。Claude Code를 활용하면 包括的なAPI仕様書の생성から클라이언트コード생성まで一気に進められます。

OpenAPI仕様の생성

RESTful APIの定義

> ブ로그APIのOpenAPI 3.1仕様를 생성해줘。
> 글のCRUD、タグ관리、인증を含めて。
openapi: 3.1.0
info:
  title: Blog API
  version: 1.0.0
  description: ブログ管理用REST API

servers:
  - url: https://api.example.com/v1
    description: 本番環境

paths:
  /posts:
    get:
      summary: 記事一覧の取得
      operationId: listPosts
      tags: [posts]
      parameters:
        - $ref: '#/components/parameters/PageParam'
        - $ref: '#/components/parameters/LimitParam'
        - name: tag
          in: query
          schema:
            type: string
          description: タグでフィルター
      responses:
        '200':
          description: 記事一覧
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Post'
                  meta:
                    $ref: '#/components/schemas/Pagination'
    post:
      summary: 記事の作成
      operationId: createPost
      tags: [posts]
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreatePostRequest'
      responses:
        '201':
          description: 作成成功
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Post'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'

components:
  schemas:
    Post:
      type: object
      required: [id, title, content, createdAt]
      properties:
        id:
          type: string
          format: uuid
        title:
          type: string
          maxLength: 200
        content:
          type: string
        tags:
          type: array
          items:
            type: string
        createdAt:
          type: string
          format: date-time
        updatedAt:
          type: string
          format: date-time

    CreatePostRequest:
      type: object
      required: [title, content]
      properties:
        title:
          type: string
          minLength: 1
          maxLength: 200
        content:
          type: string
          minLength: 1
        tags:
          type: array
          items:
            type: string

    Pagination:
      type: object
      properties:
        currentPage:
          type: integer
        totalPages:
          type: integer
        totalItems:
          type: integer

  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

コード생성

TypeScript타입と클라이언트の自動생성

> OpenAPI定義からTypeScriptの타입定義とAPI클라이언트を생성して。
> openapi-typescriptを使って。
// 생성された타입를 사용한API클라이언트
import type { paths } from './generated/api';
import createClient from 'openapi-fetch';

const client = createClient<paths>({
  baseUrl: 'https://api.example.com/v1',
});

// 타입安全なAPI呼び出し
const { data, error } = await client.GET('/posts', {
  params: {
    query: { page: 1, limit: 10, tag: 'typescript' },
  },
});

유효성 검사통합

OpenAPI定義をランタイム유효성 검사に활용することで、仕様と구현の乖離を防げます。

> OpenAPI定義からZod스키마を생성して。
> 요청・응답の両方を유효성 검사して。

모크서버

OpenAPI定義から모크서버を立ち上げ、프론트엔드개발を並行して進められます。Claude Code에「Prismで모크서버を起動する설정を作って」と依頼すれば、即座に利用가능합니다。

정리

Claude Code를 활용하면 OpenAPI仕様の생성からコード생성、유효성 검사통합まで一貫したAPI개발워크플로우を구축할 수 있습니다。API개발가이드API테스트자동화도 함께 참고하세요.

OpenAPI의 상세 정보는OpenAPI InitiativeおよびSwagger공식 문서를 참고하세요.

#Claude Code #OpenAPI #Swagger #API design #REST