> ## 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

> Verfolgen Sie API-Aufrufe und die Nutzung auf Gateway-Ebene zur Abrechnung. Perfekt für API-as-a-Service-Plattformen mit hochvolumiger Anrufverfolgung.

## Anwendungsfälle

Erforschen Sie gängige Szenarien, die vom API Gateway Blueprint unterstützt werden:

<CardGroup cols={2}>
  <Card title="API-as-a-Service" icon="server">
    Verfolge die Nutzung pro Kunde für API-Plattformen und berechne basierend auf der Anzahl der Aufrufe.
  </Card>

  <Card title="Rate Limiting" icon="gauge">
    Überwache API-Nutzungsmuster und implementiere nutzungsbasierte Drosselung.
  </Card>

  <Card title="Performance Monitoring" icon="chart-line">
    Verfolge Antwortzeiten und Fehlerraten zusammen mit Abrechnungsdaten.
  </Card>

  <Card title="Multi-Tenant SaaS" icon="users">
    Berechne Kunden basierend auf ihrem API-Verbrauch über verschiedene Endpunkte hinweg.
  </Card>
</CardGroup>

<Info>
  Ideal zum Verfolgen der Nutzung von API-Endpunkten, der Drosselung und der Implementierung nutzungsbasierter API-Abrechnung.
</Info>

## Schnellstart

Verfolgen Sie API-Aufrufe auf Gateway-Ebene mit automatischer Batch-Verarbeitung für hochvolumige Szenarien:

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

  <Step title="Get Your API Keys">
    * **Dodo Payments API-Schlüssel**: Hol ihn dir vom [Dodo Payments Dashboard](https://app.dodopayments.com/developer/api-keys)
  </Step>

  <Step title="Create a Meter">
    Erstelle einen Zähler in deinem [Dodo Payments Dashboard](https://app.dodopayments.com/):

    * **Event Name**: `api_call` (oder deinen bevorzugten Namen)
    * **Aggregation Type**: `count` zum Nachverfolgen der Anzahl der Aufrufe
    * Konfiguriere zusätzliche Eigenschaften, wenn du Metadaten wie Antwortzeiten, Statuscodes usw. verfolgst.
  </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>

## Konfiguration

### Ingestion-Konfiguration

<ParamField path="apiKey" type="string" required>
  Dein Dodo Payments API-Schlüssel aus dem Dashboard.
</ParamField>

<ParamField path="environment" type="string" required>
  Umgebungsmodus: `test_mode` oder `live_mode`.
</ParamField>

<ParamField path="eventName" type="string" required>
  Ereignisname, der mit deiner Meter-Konfiguration übereinstimmt.
</ParamField>

### Optionen zum Verfolgen von API-Aufrufen

<ParamField path="customerId" type="string" required>
  Die Kunden-ID für die Abrechnungszuordnung.
</ParamField>

<ParamField path="metadata" type="object">
  Optionale Metadaten zum API-Aufruf wie Endpunkt, Methode, Statuscode, Antwortzeit usw.
</ParamField>

### Batch-Konfiguration

<ParamField path="maxSize" type="number">
  Maximale Anzahl von Ereignissen vor dem Auto-Flush. Standard: `100`.
</ParamField>

<ParamField path="flushInterval" type="number">
  Intervall für den Auto-Flush in Millisekunden. Standard: `5000` (5 Sekunden).
</ParamField>

## Best Practices

<Tip>
  **Verwende Batching bei hohem Volumen**: Für Anwendungen, die mehr als 10 Anfragen pro Sekunde verarbeiten, verwende `createBatch()`, um Overhead zu reduzieren und die Leistung zu verbessern.
</Tip>

<Warning>
  **Räume Batches immer auf**: Rufe `batch.cleanup()` beim Herunterfahren der Anwendung auf, um ausstehende Ereignisse zu übertragen und Datenverlust zu verhindern.
</Warning>
