diff --git a/developer-resources/different-ways-to-bill-with-ubb.mdx b/developer-resources/different-ways-to-bill-with-ubb.mdx new file mode 100644 index 0000000..3c14879 --- /dev/null +++ b/developer-resources/different-ways-to-bill-with-ubb.mdx @@ -0,0 +1,624 @@ +--- +title: Different ways to bill with Usage Based Billing. +description: This guide demonstrates different ways a business can bill the customers using Usage Based Billing. +icon: "credit-card" +--- + +## Table of contents +- Billing on the basis of API calls. +- Billing on the basis of Token usage. +- Billing on the basis of runtime (Execution Time) + +## Billing on the basis of number of API calls + +In this section you'll see how the business can charge on the basis of the number of API calls made by their customers. + +### Creating a Meter + + + API Call Meter + + + + +Set up the fundamental details for your meter. + + +Choose a clear, descriptive name that identifies what this meter tracks. + +Example: "API Calls" + + + +Provide a detailed explanation of what this meter measures. + +Example: "Counts each POST /v1/orders request made by the customer" + + + +Specify the event identifier that will trigger this meter. + +Examples: "api.call" + + + +The event name must match exactly what you send in your usage events. Event names are case-sensitive. + + + + +Define how the meter calculates usage from your events. + + +Select how events should be aggregated: +Select the Count aggregation to calculate the number of API calls. + + + + + +Define the unit label for display purposes in reports and billing. + +Example: "calls" + + + + +Set up criteria to control which events are included in the meter. + + +**Enable Event Filtering** + +Toggle **Enable Event Filtering** to activate conditional event processing. + +**Choose Filter Logic** + +Select how multiple conditions are evaluated + + + +All conditions must be true for an event to be counted. Use this when you need events to meet multiple strict criteria simultaneously. + +**Example:** Count API calls where `user_tier = "premium"` AND `endpoint = "/v1/orders"` + + + +At least one condition must be true for an event to be counted. Use this when you want to include events that meet any of several criteria. + +**Example:** Count events where `method = "POST"` OR `method = "PUT"` OR `method = "DELETE"` + + + +**Setting Up Filter Conditions** + + + +Click **Add condition** to create a new filter rule. + + + +Specify the property name from your event metadata. + + + +Choose from available operators: +- `equals` - Exact match +- `not equals` - Exclusion filter +- `greater than` - Numeric comparison +- `greater than or equals` - Numeric comparison (inclusive) +- `less than` - Numeric comparison +- `less than or equals` - Numeric comparison (inclusive) +- `contains` - String contains substring +- `does not contain` - String exclusion filter + + + +Set the target value for comparison. + + + +Use **Add Group** to create additional condition groups for complex logic. + + + + + +Filtered properties must be included in your event metadata for the conditions to work properly. Events missing required properties will be excluded from counting. + + + + +Review your meter configuration and click on **Create Meter**. + + +Your meter is now ready to receive and aggregate usage events. + + + + + +### Ingest an Event + +Ingest event to your configured meter using Events API: + + +```javascript Node.js +const response = await fetch('https://test.dodopayments.com/events/ingest', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.DODO_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + events: [ + { + event_id: "api_call_1234", + customer_id: "cust_id_123", + event_name: "api.call", + timestamp: new Date().toISOString(), + } + ] + }) +}); +``` + +```python Python +import requests +from datetime import datetime + +response = requests.post( + 'https://test.dodopayments.com/events/ingest', + headers={ + 'Authorization': f'Bearer {api_key}', + 'Content-Type': 'application/json' + }, + json={ + 'events': [ + { + 'event_id': 'api_call_1234', + 'customer_id': 'cust_id_123', + 'event_name': 'api.call', + 'timestamp': datetime.now().isoformat(), + } + ] + } +) +``` + +```curl cURL +curl -X POST 'https://test.dodopayments.com/events/ingest' \ + -H 'Authorization: Bearer YOUR_API_KEY' \ + -H 'Content-Type: application/json' \ + -d '{ + "events": [ + { + "event_id": "api_call_1234", + "customer_id": "cust_id_123", + "event_name": "api.call", + "timestamp": "2024-01-15T10:30:00Z" + } + ] + }' +``` + + + +## Billing on the basis of Token Usage + +In this section you'll see how the business can charge on the basis of Token usage by their customers. + +### Creating a Meter + + + Token Usage Meter + + + + +Set up the fundamental details for your meter. + + +Choose a clear, descriptive name that identifies what this meter tracks. + +Example: "Tokens" + + + +Provide a detailed explanation of what this meter measures. + +Example: "Counts the token usage by the customers" + + + +Specify the event identifier that will trigger this meter. + +Example: "token" + + + +The event name must match exactly what you send in your usage events. Event names are case-sensitive. + + + + +Define how the meter calculates usage from your events. + + +Select how events should be aggregated: +Select the Sum aggregation to calculate token usage. + + + + +The property name from event metadata to aggregate over. Here, aggregate it over `token` field. + + +This field is required when using Sum, Max, or Last aggregation types. + + + + +Define the unit label for display purposes in reports and billing. + +Example: "token" + + + + +Set up criteria to control which events are included in the meter. + + +**Enable Event Filtering** + +Toggle **Enable Event Filtering** to activate conditional event processing. + +**Choose Filter Logic** + +Select how multiple conditions are evaluated + + + +All conditions must be true for an event to be counted. Use this when you need events to meet multiple strict criteria simultaneously. + +**Example:** Count Token usage on `endpoint = "/v1/ai"` **AND** `user_plan = "pro"` + + + +At least one condition must be true for an event to be counted. Use this when you want to include events that meet any of several criteria. + +**Example:** Count Token usage on `endpoint = "/v1/ai"` **OR** `endpoint = "/v1/chat"` + + + +**Setting Up Filter Conditions** + + + +Click **Add condition** to create a new filter rule. + + + +Specify the property name from your event metadata. + + + +Choose from available operators: +- `equals` - Exact match +- `not equals` - Exclusion filter +- `greater than` - Numeric comparison +- `greater than or equals` - Numeric comparison (inclusive) +- `less than` - Numeric comparison +- `less than or equals` - Numeric comparison (inclusive) +- `contains` - String contains substring +- `does not contain` - String exclusion filter + + + +Set the target value for comparison. + + + +Use **Add Group** to create additional condition groups for complex logic. + + + + + +Filtered properties must be included in your event metadata for the conditions to work properly. Events missing required properties will be excluded from counting. + + + + +Review your meter configuration and click on **Create Meter**. + + +Your meter is now ready to receive and aggregate usage events. + + + + + + +### Ingest an Event + +Ingest event to your configured meter using Events API: + + +```javascript Node.js +const response = await fetch('https://test.dodopayments.com/events/ingest', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.DODO_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + events: [ + { + event_id: "token_usage_1234", + customer_id: "cust_id_123", + event_name: "token", + timestamp: new Date().toISOString(), + metadata: { + tokens: 100 + } + } + ] + }) +}); +``` + +```python Python +import requests +from datetime import datetime + +response = requests.post( + 'https://test.dodopayments.com/events/ingest', + headers={ + 'Authorization': f'Bearer {api_key}', + 'Content-Type': 'application/json' + }, + json={ + 'events': [ + { + 'event_id': 'token_usage_1234', + 'customer_id': 'cust_id_123', + 'event_name': 'token', + 'timestamp': datetime.now().isoformat(), + 'metadata': { + 'tokens': 100 + } + } + ] + } +) +``` + +```curl cURL +curl -X POST 'https://test.dodopayments.com/events/ingest' \ + -H 'Authorization: Bearer YOUR_API_KEY' \ + -H 'Content-Type: application/json' \ + -d '{ + "events": [ + { + "event_id": "token_usage_1234", + "customer_id": "cust_id_123", + "event_name": "token", + "timestamp": "2025-10-8T10:30:00Z", + metadata: { + tokens: 100 + } + } + ] + }' +``` + + + +## Billing on the basis of Runtime (Execution Time) + +In this section you'll see how the business can charge on the basis of runtime by their customers. + +### Creating a Meter + + + Execution Time Meter + + + + +Set up the fundamental details for your meter. + + +Choose a clear, descriptive name that identifies what this meter tracks. + +Example: "Compute Hours" + + + +Provide a detailed explanation of what this meter measures. + +Example: "Count the total compute hours by the customer" + + + +Specify the event identifier that will trigger this meter. + +Example: "compute.hour" + + + +The event name must match exactly what you send in your usage events. Event names are case-sensitive. + + + + +Define how the meter calculates usage from your events. + + +Select how events should be aggregated: +Select the Sum aggregation to calculate runtime. + + + + +The property name from event metadata to aggregate over. Here, aggregate it over `hour` field. + + +This field is required when using Sum, Max, or Last aggregation types. + + + + +Define the unit label for display purposes in reports and billing. + +Example: "hours" + + + + +Set up criteria to control which events are included in the meter. + + +**Enable Event Filtering** + +Toggle **Enable Event Filtering** to activate conditional event processing. + +**Choose Filter Logic** + +Select how multiple conditions are evaluated + + + +All conditions must be true for an event to be counted. Use this when you need events to meet multiple strict criteria simultaneously. + +**Example:** Count compute hours where `execution_time > 60` **AND** `model_type = "advanced"` + + + +At least one condition must be true for an event to be counted. Use this when you want to include events that meet any of several criteria. + +**Example:** Count compute hours where `hours > 60` **OR** `model_type = "advanced"` + + + +**Setting Up Filter Conditions** + + + +Click **Add condition** to create a new filter rule. + + + +Specify the property name from your event metadata. + + + +Choose from available operators: +- `equals` - Exact match +- `not equals` - Exclusion filter +- `greater than` - Numeric comparison +- `greater than or equals` - Numeric comparison (inclusive) +- `less than` - Numeric comparison +- `less than or equals` - Numeric comparison (inclusive) +- `contains` - String contains substring +- `does not contain` - String exclusion filter + + + +Set the target value for comparison. + + + +Use **Add Group** to create additional condition groups for complex logic. + + + + + +Filtered properties must be included in your event metadata for the conditions to work properly. Events missing required properties will be excluded from counting. + + + + +Review your meter configuration and click on **Create Meter**. + + +Your meter is now ready to receive and aggregate usage events. + + + + + +### Ingest an Event + +Ingest event to your configured meter using Events API: + + +```javascript Node.js +const response = await fetch('https://test.dodopayments.com/events/ingest', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.DODO_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + events: [ + { + event_id: "compute_hour_1234", + customer_id: "cust_id_123", + event_name: "compute.hour", + timestamp: new Date().toISOString(), + metadata: { + hour: 60 + } + } + ] + }) +}); +``` + +```python Python +import requests +from datetime import datetime + +response = requests.post( + 'https://test.dodopayments.com/events/ingest', + headers={ + 'Authorization': f'Bearer {api_key}', + 'Content-Type': 'application/json' + }, + json={ + 'events': [ + { + 'event_id': 'compute_hour_1234', + 'customer_id': 'cust_id_123', + 'event_name': 'compute.hour', + 'timestamp': datetime.now().isoformat(), + 'metadata': { + 'hour': 60 + } + } + ] + } +) +``` + +```curl cURL +curl -X POST 'https://test.dodopayments.com/events/ingest' \ + -H 'Authorization: Bearer YOUR_API_KEY' \ + -H 'Content-Type: application/json' \ + -d '{ + "events": [ + { + "event_id": "compute_hour_1234", + "customer_id": "cust_id_123", + "event_name": "compute.hour", + "timestamp": "2025-10-8T10:30:00Z", + metadata: { + hour: 60 + } + } + ] + }' +``` + \ No newline at end of file diff --git a/docs.json b/docs.json index 7f9f6ca..43dfe30 100644 --- a/docs.json +++ b/docs.json @@ -125,6 +125,7 @@ "developer-resources/usage-based-billing-guide", "developer-resources/integration-guide", "developer-resources/subscription-integration-guide", + "developer-resources/different-ways-to-bill-with-ubb", "developer-resources/subscription-upgrade-downgrade", "developer-resources/integration-tutorial", "developer-resources/overlay-checkout", diff --git a/images/different-ways-to-bill-with-ubb/api-call-meter.png b/images/different-ways-to-bill-with-ubb/api-call-meter.png new file mode 100644 index 0000000..d125fdd Binary files /dev/null and b/images/different-ways-to-bill-with-ubb/api-call-meter.png differ diff --git a/images/different-ways-to-bill-with-ubb/execution-time-meter.png b/images/different-ways-to-bill-with-ubb/execution-time-meter.png new file mode 100644 index 0000000..d36a768 Binary files /dev/null and b/images/different-ways-to-bill-with-ubb/execution-time-meter.png differ diff --git a/images/different-ways-to-bill-with-ubb/token-usage-meter.png b/images/different-ways-to-bill-with-ubb/token-usage-meter.png new file mode 100644 index 0000000..1d7ea50 Binary files /dev/null and b/images/different-ways-to-bill-with-ubb/token-usage-meter.png differ