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

# Time Range Blueprint

> Track resource consumption based on elapsed time for compute, serverless functions, containers, and runtime billing.

## Use Cases

Explore common scenarios supported by the Time Range Blueprint:

<CardGroup cols={2}>
  <Card title="Serverless Functions" icon="function">
    Bill based on function execution time and memory usage.
  </Card>

  <Card title="Container Runtime" icon="container-storage">
    Track container running time for usage-based billing.
  </Card>

  <Card title="Compute Instances" icon="server">
    Monitor VM runtime and charge by the minute or hour.
  </Card>

  <Card title="Background Jobs" icon="briefcase">
    Track processing time for data exports, reports, and batch jobs.
  </Card>
</CardGroup>

<Info>
  Perfect for billing based on compute time, function execution duration, container runtime, or any time-based usage.
</Info>

## Quick Start

Track resource usage by time duration:

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

  <Step title="Get Your API Keys">
    * **Dodo Payments API Key**: Get it from [Dodo Payments Dashboard](https://app.dodopayments.com/developer/api-keys)
  </Step>

  <Step title="Create a Meter">
    Create a meter in your [Dodo Payments Dashboard](https://app.dodopayments.com/):

    * **Event Name**: `time_range_usage` (or your preferred name)
    * **Aggregation Type**: `sum` to track total duration
    * **Over Property**: `durationSeconds`, `durationMinutes`, or `durationMs`
  </Step>

  <Step title="Track Time Usage">
    <CodeGroup>
      ```javascript Serverless Functions theme={null}
      import { Ingestion, trackTimeRange } from '@dodopayments/ingestion-blueprints';

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

      // Track function execution time
      const startTime = Date.now();

      // Execute your function (example: image processing)
      const result = await yourImageProcessingLogic();

      const durationMs = Date.now() - startTime;

      await trackTimeRange(ingestion, {
        customerId: 'customer_123',
        durationMs: durationMs
      });
      ```

      ```javascript Container Runtime theme={null}
      import { Ingestion, trackTimeRange } from '@dodopayments/ingestion-blueprints';

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

      // Track container runtime in seconds
      await trackTimeRange(ingestion, {
        customerId: 'customer_456',
        durationSeconds: 120
      });
      ```

      ```javascript VM Instance Runtime theme={null}
      import { Ingestion, trackTimeRange } from '@dodopayments/ingestion-blueprints';

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

      // Track VM runtime in minutes
      await trackTimeRange(ingestion, {
        customerId: 'customer_789',
        durationMinutes: 60
      });
      ```
    </CodeGroup>
  </Step>
</Steps>

## Configuration

### Ingestion Configuration

<ParamField path="apiKey" type="string" required>
  Your Dodo Payments API key from the dashboard.
</ParamField>

<ParamField path="environment" type="string" required>
  Environment mode: `test_mode` or `live_mode`.
</ParamField>

<ParamField path="eventName" type="string" required>
  Event name that matches your meter configuration.
</ParamField>

### Track Time Range Options

<ParamField path="customerId" type="string" required>
  The customer ID for billing attribution.
</ParamField>

<ParamField path="durationMs" type="number">
  Duration in milliseconds. Use for sub-second precision.
</ParamField>

<ParamField path="durationSeconds" type="number">
  Duration in seconds. Most common for function execution and short tasks.
</ParamField>

<ParamField path="durationMinutes" type="number">
  Duration in minutes. Useful for longer-running resources like VMs.
</ParamField>

<ParamField path="metadata" type="object">
  Optional metadata about the resource like CPU, memory, region, etc.
</ParamField>

## Best Practices

<Tip>
  **Choose the Right Unit**: Use milliseconds for short operations, seconds for functions, and minutes for longer-running resources.
</Tip>

<Warning>
  **Accurate Timing**: Use `Date.now()` or `performance.now()` for accurate time tracking, especially for serverless functions.
</Warning>
