@dodopayments/ingestion-blueprints npm package에 trackAPICall()로 포함되어 있으며, 호출당 하나의 event를 전송합니다. 또한 요청량이 많은 경우를 위해 event를 queue하는 createBatch()도 포함되어 있습니다.
사용 사례
API Gateway Blueprint는 다음 시나리오에 적합합니다:API-as-a-Service
API platform에서 고객별 호출 수를 추적하고 호출 횟수에 따라 요금을 청구합니다.
Rate Limiting
사용량 기반 rate limit을 설정할 수 있도록 각 고객의 호출량을 기록합니다. 이 blueprint는 사용량을 기록하지만 limit을 적용하지는 않습니다.
Performance Monitoring
각 event에 response time과 status code를 기록하여 error rate를 billing data와 함께 확인할 수 있도록 합니다.
Multi-Tenant SaaS
서로 다른 endpoint에서 고객이 사용한 API에 대해 요금을 청구합니다.
모든 event에는 요금을 청구할 고객의 Dodo Payments customer ID가 필요하며, 이 ID는
cus_로 시작합니다. 고객을 생성할 때 user record와 함께 저장한 다음 customerId로 전달하세요.빠른 시작
API 호출을 추적하려면 package를 설치하고, meter를 생성한 다음, 각 호출에 대해 event를 전송하세요.1
Install the SDK
Dodo Payments Ingestion Blueprints package를 설치합니다:
2
Get Your API Keys
Dodo Payments dashboard의 Developer → API Keys에서 Dodo Payments API key를 생성하고
DODO_PAYMENTS_API_KEY environment variable에 저장하세요. 개발 중에는 test mode key를 사용하세요. test mode key는 test_mode에서만 작동합니다.3
Create a Meter
Dodo Payments dashboard에서 Products → Meters로 이동한 다음 Create Meter를 클릭하세요. 다음 field를 설정합니다:
- Meter Name:
API Calls와 같은 설명이 포함된 이름입니다. - Event Name:
api_call또는 직접 선택한 이름입니다. 코드의eventName와 정확히 일치해야 합니다(case-sensitive). - Aggregation Type: 호출 횟수에 따라 요금을 청구하려면 Count를 선택합니다.
- Measurement Unit: invoice에 표시할 단위입니다. 예:
calls.
endpoint, method 또는 status_code와 같은 metadata key에 조건을 추가하세요.4
Track API Calls
API key와 event name을 사용해
Ingestion instance 하나를 생성한 다음 pattern을 선택하세요. 호출당 하나의 event를 전송하거나, 대량 요청을 위한 batch를 사용하거나, 모든 request를 추적하는 Express.js middleware를 사용할 수 있습니다. middleware에서 req.user는 authentication middleware에서 가져오며, 해당 id는 Dodo Payments customer ID여야 합니다. sign-in한 user가 없는 request는 어떤 customer와도 일치하지 않는 customer ID anonymous와 함께 전송됩니다.Configuration
Ingestion Configuration
다음 option을new Ingestion()에 전달하세요:
string
필수
Dashboard에서 가져온 Dodo Payments API key입니다.
string
Environment mode:
test_mode 또는 live_mode입니다. 기본값은 test_mode입니다. 반면 Dodo Payments SDK의 기본값은 live_mode이므로 production에서는 live_mode를 명시적으로 설정하세요.string
필수
meter의 Event Name과 일치하는 event name입니다(case-sensitive). 이 instance가 전송하는 모든 event에서 이 이름을 사용합니다.
API 호출 추적 Option
다음 option을trackAPICall() 및 batch.add()에 전달하세요:
string
필수
호출 요금을 청구할 Dodo Payments customer ID입니다. 예:
cus_123.object
endpoint, method, status code, response time과 같은 API 호출에 대한 선택적 metadata입니다. 각 value는 string, number 또는 boolean이어야 합니다. API는 nested object, array 및
null value를 거부합니다.Batch Configuration
createBatch(ingestion, options)는 event를 memory에 queue하고 세 가지 method가 포함된 object를 반환합니다. add()는 event를 queue하고, flush()는 queue된 event를 전송하며, cleanup()는 event를 전송하고 timer를 중지합니다. flush는 event마다 하나의 ingest request를 parallel하게 전송합니다.
number
즉시 flush를 실행하는 queue된 event 수입니다. 기본값:
100.number
가장 최근의
add() 이후 batch가 flush될 때까지 기다리는 milliseconds입니다. add()가 호출될 때마다 timer가 다시 시작됩니다. 기본값: 5000(5초).모범 사례
batch는 flush될 때까지 event를 memory에 보관하며, 전송에 실패한 event를 retry하지 않습니다. automatic flush는console.error로 error를 log합니다. flush() 또는 cleanup()를 호출하면 해당 error가 throw됩니다.