> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dodopayments.com/llms.txt
> Use this file to discover all available pages before exploring further.

# API Gateway Blueprint

> API 호출 및 게이트웨이 수준 사용량을 추적하여 청구합니다. 고용량 요청 추적이 필요한 API-as-a-service 플랫폼에 적합합니다.

## 사용 사례

API Gateway Blueprint에서 지원하는 일반적인 시나리오를 살펴보세요:

<CardGroup cols={2}>
  <Card title="API-as-a-Service" icon="server">
    API 플랫폼에서 고객별 사용량을 추적하고 호출 수를 기준으로 요금 청구.
  </Card>

  <Card title="Rate Limiting" icon="gauge">
    API 사용 패턴을 모니터링하고 사용량 기반 속도 제한을 구현.
  </Card>

  <Card title="Performance Monitoring" icon="chart-line">
    청구 데이터와 함께 응답 시간 및 오류율을 추적.
  </Card>

  <Card title="Multi-Tenant SaaS" icon="users">
    다양한 엔드포인트에서 API 소비를 기준으로 고객에게 청구.
  </Card>
</CardGroup>

<Info>
  API 엔드포인트 사용량 추적, 속도 제한, 사용량 기반 API 청구 구현에 적합.
</Info>

## 빠른 시작

고용량 시나리오를 위한 자동 배치로 게이트웨이 수준에서 API 호출을 추적합니다:

<Steps>
  <Step title="Install the SDK">
    ```bash theme={null}
    npm install @dodopayments/ingestion-blueprints
    ```
  </Step>

  <Step title="Get Your API Keys">
    * **Dodo Payments API 키**: [Dodo Payments Dashboard](https://app.dodopayments.com/developer/api-keys)에서 가져옵니다
  </Step>

  <Step title="Create a Meter">
    다음에서 미터를 생성하세요: [Dodo Payments Dashboard](https://app.dodopayments.com/)

    * **이벤트 이름**: `api_call` (또는 선호하는 이름)
    * **집계 유형**: `count` (호출 수 추적용)
    * 응답 시간, 상태 코드 등과 같은 메타데이터를 추적하는 경우 추가 속성을 구성합니다.
  </Step>

  <Step title="Track API Calls">
    <CodeGroup>
      ```javascript Single API Call theme={null}
      import { Ingestion, trackAPICall } from '@dodopayments/ingestion-blueprints';

      const ingestion = new Ingestion({
        apiKey: process.env.DODO_PAYMENTS_API_KEY,
        environment: 'test_mode',
        eventName: 'api_call'
      });

      // Track a single API call
      await trackAPICall(ingestion, {
        customerId: 'customer_123',
        metadata: {
          endpoint: '/api/v1/users',
          method: 'GET',
          status_code: 200,
          response_time_ms: 45
        }
      });
      ```

      ```javascript High-Volume with Batching theme={null}
      import { Ingestion, createBatch } from '@dodopayments/ingestion-blueprints';

      const ingestion = new Ingestion({
        apiKey: process.env.DODO_PAYMENTS_API_KEY,
        environment: 'live_mode',
        eventName: 'api_call'
      });

      // Create batch for high-volume tracking
      const batch = createBatch(ingestion, {
        maxSize: 100,      // Flush after 100 events
        flushInterval: 5000 // Or flush every 5 seconds
      });

      // Add API calls to batch
      batch.add({
        customerId: 'customer_123',
        metadata: {
          endpoint: '/api/v1/products',
          method: 'GET',
          status_code: 200
        }
      });

      // Clean up when done
      await batch.cleanup();
      ```

      ```javascript Express.js Middleware theme={null}
      import express from 'express';
      import { Ingestion, createBatch } from '@dodopayments/ingestion-blueprints';

      const app = express();

      const ingestion = new Ingestion({
        apiKey: process.env.DODO_PAYMENTS_API_KEY,
        environment: 'live_mode',
        eventName: 'api_call'
      });

      const batch = createBatch(ingestion, {
        maxSize: 50,
        flushInterval: 10000
      });

      // Middleware to track all API calls
      app.use((req, res, next) => {
        const startTime = Date.now();
        
        res.on('finish', () => {
          const responseTime = Date.now() - startTime;
          
          batch.add({
            customerId: req.user?.id || 'anonymous',
            metadata: {
              endpoint: req.path,
              method: req.method,
              status_code: res.statusCode,
              response_time_ms: responseTime
            }
          });
        });
        
        next();
      });

      // Cleanup on shutdown
      process.on('SIGTERM', async () => {
        await batch.cleanup();
        process.exit(0);
      });
      ```
    </CodeGroup>
  </Step>
</Steps>

## 구성

### 수집 구성

<ParamField path="apiKey" type="string" required>
  대시보드에서 가져온 Dodo Payments API 키입니다.
</ParamField>

<ParamField path="environment" type="string" required>
  환경 모드: `test_mode` 또는 `live_mode`.
</ParamField>

<ParamField path="eventName" type="string" required>
  미터 구성과 일치하는 이벤트 이름입니다.
</ParamField>

### API 호출 추적 옵션

<ParamField path="customerId" type="string" required>
  청구 귀속을 위한 고객 ID입니다.
</ParamField>

<ParamField path="metadata" type="object">
  API 호출에 대한 선택적 메타데이터(엔드포인트, 메서드, 상태 코드, 응답 시간 등).
</ParamField>

### 배치 구성

<ParamField path="maxSize" type="number">
  자동 플러시 전 최대 이벤트 수. 기본값: `100`.
</ParamField>

<ParamField path="flushInterval" type="number">
  자동 플러시 간격(밀리초). 기본값: `5000` (5초).
</ParamField>

## 모범 사례

<Tip>
  **대용량에는 배칭 사용**: 초당 10건 이상의 요청을 처리하는 애플리케이션은 `createBatch()`를 사용하여 오버헤드를 줄이고 성능을 개선하세요.
</Tip>

<Warning>
  **항상 배치 정리**: 애플리케이션 종료 시 `batch.cleanup()`를 호출하여 대기 중인 이벤트를 플러시하고 데이터 손실을 방지하세요.
</Warning>
