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

# Aggregate Earned Premium

> Returns earned premium per day summed across all matching policies. No
per-policy or per-transaction breakdown — just a daily time series and
a grand total.

Use `segmentScope` to control which policies are included based on
segment date matching:
- `"all"` — every policy
- `{"asOf":"YYYY-MM-DD"}` — policies with segments active on that date
- `{"fromDate":"YYYY-MM-DD","toDate":"YYYY-MM-DD"}` — policies with
  segments overlapping the date range

Use `filters` to narrow results by field values in policy data.

The earned premium date range is a half-open interval `[startDate, endDate)`
and must not exceed 366 days.

**Required permission:** `company.policy:read`




## OpenAPI

````yaml /openapi/generated-external-api.yaml get /api/v1/external/companies/{companyId}/policies/aggregate/earned-premium
openapi: 3.0.3
info:
  title: AI Insurance External API
  description: External API for AI Insurance platform
  version: 1.0.0
  contact:
    email: support@aiinsurance.io
servers:
  - url: https://app.aiinsurance.io
    description: Production
security:
  - ApiKeyAuth: []
paths:
  /api/v1/external/companies/{companyId}/policies/aggregate/earned-premium:
    get:
      tags:
        - Field Model Earned Premium
      summary: Aggregate Earned Premium
      description: >
        Returns earned premium per day summed across all matching policies. No

        per-policy or per-transaction breakdown — just a daily time series and

        a grand total.


        Use `segmentScope` to control which policies are included based on

        segment date matching:

        - `"all"` — every policy

        - `{"asOf":"YYYY-MM-DD"}` — policies with segments active on that date

        - `{"fromDate":"YYYY-MM-DD","toDate":"YYYY-MM-DD"}` — policies with
          segments overlapping the date range

        Use `filters` to narrow results by field values in policy data.


        The earned premium date range is a half-open interval `[startDate,
        endDate)`

        and must not exceed 366 days.


        **Required permission:** `company.policy:read`
      operationId: aggregateEarnedPremium
      parameters:
        - $ref: '#/components/parameters/companyId'
        - name: startDate
          in: query
          required: true
          schema:
            type: string
            format: date
          description: |
            Start of the earned premium window (inclusive). ISO 8601 date
            (`YYYY-MM-DD`). Must be before or equal to `endDate`.
        - name: endDate
          in: query
          required: true
          schema:
            type: string
            format: date
          description: |
            End of the earned premium window (exclusive). ISO 8601 date
            (`YYYY-MM-DD`). The range `[startDate, endDate)` must not exceed
            366 days.
        - name: segmentScope
          in: query
          required: true
          schema:
            type: string
          description: |
            JSON-encoded segment scope filter. Controls which policies are
            included based on segment date matching. Values:
            - `"all"` — every policy
            - `{"asOf":"YYYY-MM-DD"}` — point-in-time
            - `{"fromDate":"YYYY-MM-DD","toDate":"YYYY-MM-DD"}` — date range
          examples:
            all:
              summary: All policies
              value: '"all"'
            asOf:
              summary: Point-in-time
              value: '{"asOf":"2025-06-15"}'
            range:
              summary: Date range
              value: '{"fromDate":"2025-01-01","toDate":"2025-06-30"}'
        - name: filters
          in: query
          schema:
            type: string
          description: >
            JSON-encoded array of field filters. Each filter targets a field in

            the segment's `fieldModelV1Data` and supports type-specific
            operators

            (text, number, boolean, date, currency, optionSet, address).
          examples:
            textFilter:
              summary: Text field filter
              value: >-
                [{"fieldReferenceId":"policyNumber","fieldType":"text","operator":"equals","value":"POL-001"}]
            numberFilter:
              summary: Number field filter
              value: >-
                [{"fieldReferenceId":"annualPremium","fieldType":"number","operator":"greaterThan","value":50000}]
        - name: timeTravelBackToDate
          in: query
          schema:
            type: string
            format: date-time
          description: |
            Only include transactions created before this timestamp (ISO 8601).
            Useful for auditing historical earned premium state.
        - name: limit
          in: query
          schema:
            type: integer
            minimum: 1
            maximum: 10000
            default: 10000
          description: |
            Maximum number of policies to aggregate (default and max: 10,000).
      responses:
        '200':
          description: Aggregated earned premium across all matching policies
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EarnedPremiumAggregateResponse'
              examples:
                quarterlyAggregate:
                  summary: Q1 2025 aggregate
                  value:
                    earnedPremiumTotal: 1250000
                    policyCount: 47
                    days:
                      - date: '2025-01-01'
                        earnedPremium: 13888.89
                      - date: '2025-01-02'
                        earnedPremium: 13888.89
                      - date: '2025-01-03'
                        earnedPremium: 14102.74
                emptyResult:
                  summary: No matching policies
                  value:
                    earnedPremiumTotal: 0
                    policyCount: 0
                    days: []
        '400':
          description: Invalid request parameters
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                missingDates:
                  summary: Missing required date parameters
                  value:
                    error:
                      code: INVALID_REQUEST
                      message: startDate and endDate must be valid ISO dates
                dateRangeTooLarge:
                  summary: Date range exceeds 366 days
                  value:
                    error:
                      code: INVALID_REQUEST
                      message: Date range must not exceed 366 days
                limitExceeded:
                  summary: Limit exceeds maximum
                  value:
                    error:
                      code: INVALID_REQUEST
                      message: limit must not exceed 10000
                invalidSegmentScope:
                  summary: Invalid JSON in segmentScope
                  value:
                    error:
                      code: INVALID_REQUEST
                      message: segmentScope must be valid JSON
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '500':
          $ref: '#/components/responses/InternalServerError'
components:
  parameters:
    companyId:
      name: companyId
      in: path
      required: true
      schema:
        type: string
        format: uuid
      description: Company identifier
  schemas:
    EarnedPremiumAggregateResponse:
      type: object
      description: >
        Earned premium aggregated across all matching policies. Returns a daily
        time series and a grand total — no per-policy or per-transaction
        breakdown.
      required:
        - earnedPremiumTotal
        - policyCount
        - days
      properties:
        earnedPremiumTotal:
          type: number
          description: Grand total earned premium across all matching policies
        policyCount:
          type: integer
          description: Number of policies included in the aggregation
        days:
          type: array
          description: >
            Daily earned premium time series. Contains one entry per day in the
            half-open interval [startDate, endDate).
          items:
            $ref: '#/components/schemas/EarnedPremiumAggregateDay'
    ErrorResponse:
      type: object
      description: Standard error response for all external API endpoints
      required:
        - error
      properties:
        error:
          type: object
          required:
            - code
            - message
          properties:
            code:
              type: string
              description: Machine-readable error code
              example: VALIDATION_ERROR
            message:
              type: string
              description: Human-readable error message
              example: 'submissionId: Required field is missing'
            details:
              type: array
              description: Additional details for validation errors (field-level errors)
              items:
                type: object
                properties:
                  field:
                    type: string
                    description: The field that caused the error
                    example: submissionId
                  message:
                    type: string
                    description: Description of the field error
                    example: Required field is missing
    EarnedPremiumAggregateDay:
      type: object
      description: Aggregate earned premium on a single day across all matching policies.
      required:
        - date
        - earnedPremium
      properties:
        date:
          type: string
          format: date
          description: The calendar date (YYYY-MM-DD)
        earnedPremium:
          type: number
          description: |
            Total earned premium across all matching policies on this day
  responses:
    Unauthorized:
      description: Unauthorized - Invalid or missing API key
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            missingApiKey:
              summary: Missing API key
              value:
                error:
                  code: UNAUTHORIZED
                  message: Authorization header is required
            bearerTokenNotAllowed:
              summary: Bearer token used instead of API key
              value:
                error:
                  code: UNAUTHORIZED
                  message: External API endpoints require API key authentication
    Forbidden:
      description: Forbidden - Insufficient permissions
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            insufficientPermissions:
              summary: Insufficient permissions
              value:
                error:
                  code: FORBIDDEN
                  message: Insufficient permissions to perform this action
    InternalServerError:
      description: Internal Server Error - Unexpected error occurred
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            internalError:
              summary: Unexpected server error
              value:
                error:
                  code: INTERNAL_ERROR
                  message: An unexpected error occurred. Please try again later.
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: >-
        API key authentication. Include your API key in the Authorization
        header.

````