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

# Get Earned Premium (Single Policy)

> Returns earned premium for a single policy over a date range. Each
transaction on the policy produces a daily contribution (a fixed daily
rate over its effective range), and the earned premium on any given day
is the sum of all active contributions.

Use the `detail` parameter to control response granularity:
- `transactions` (default) — transaction-level totals only
- `days` — adds a daily time series of net earned premium
- `daysAndTransactions` — adds a daily time series with per-transaction
  contributions on each day

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

Use `timeTravelBackToDate` to see earned premium as it would have been
calculated at a prior point in time — only transactions created before
that timestamp are included.

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




## OpenAPI

````yaml /openapi/generated-external-api.yaml get /api/v1/external/companies/{companyId}/policies/{policyId}/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/{policyId}/earned-premium:
    get:
      tags:
        - Field Model Earned Premium
      summary: Get Earned Premium (Single Policy)
      description: |
        Returns earned premium for a single policy over a date range. Each
        transaction on the policy produces a daily contribution (a fixed daily
        rate over its effective range), and the earned premium on any given day
        is the sum of all active contributions.

        Use the `detail` parameter to control response granularity:
        - `transactions` (default) — transaction-level totals only
        - `days` — adds a daily time series of net earned premium
        - `daysAndTransactions` — adds a daily time series with per-transaction
          contributions on each day

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

        Use `timeTravelBackToDate` to see earned premium as it would have been
        calculated at a prior point in time — only transactions created before
        that timestamp are included.

        **Required permission:** `company.policy:read`
      operationId: getEarnedPremium
      parameters:
        - $ref: '#/components/parameters/companyId'
        - $ref: '#/components/parameters/policyIdPath'
        - 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: detail
          in: query
          schema:
            type: string
            enum:
              - transactions
              - days
              - daysAndTransactions
            default: transactions
          description: |
            Response granularity:
            - `transactions` — `transactionTotals` only (default)
            - `days` — adds `days` array with net earned premium per day
            - `daysAndTransactions` — adds `days` array with per-transaction
              `contributions` on each day
        - 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.
      responses:
        '200':
          description: Earned premium for the policy
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EarnedPremiumPolicyResponse'
              examples:
                transactionsDetail:
                  summary: Default detail level (transactions)
                  value:
                    policyId: 550e8400-e29b-41d4-a716-446655440001
                    policyNumber: POL-2025-001
                    earnedPremiumTotal: 42465.75
                    transactionTotals:
                      - transactionId: 660e8400-e29b-41d4-a716-446655440001
                        policyVersion: 1
                        action: NEW_BUSINESS
                        totalContribution: 42465.75
                daysDetail:
                  summary: Daily time series (days)
                  value:
                    policyId: 550e8400-e29b-41d4-a716-446655440001
                    policyNumber: POL-2025-001
                    earnedPremiumTotal: 465.76
                    transactionTotals:
                      - transactionId: 660e8400-e29b-41d4-a716-446655440001
                        policyVersion: 1
                        action: NEW_BUSINESS
                        totalContribution: 465.76
                    days:
                      - date: '2025-01-01'
                        earnedPremium: 232.88
                      - date: '2025-01-02'
                        earnedPremium: 232.88
                daysAndTransactionsDetail:
                  summary: Daily time series with transaction breakdown
                  value:
                    policyId: 550e8400-e29b-41d4-a716-446655440001
                    policyNumber: POL-2025-001
                    earnedPremiumTotal: 698.63
                    transactionTotals:
                      - transactionId: 660e8400-e29b-41d4-a716-446655440001
                        policyVersion: 1
                        action: NEW_BUSINESS
                        totalContribution: 465.76
                      - transactionId: 660e8400-e29b-41d4-a716-446655440002
                        policyVersion: 2
                        action: ENDORSE
                        totalContribution: 232.87
                    days:
                      - date: '2025-06-01'
                        earnedPremium: 232.88
                        contributions:
                          - transactionId: 660e8400-e29b-41d4-a716-446655440001
                            policyVersion: 1
                            action: NEW_BUSINESS
                            dailyContribution: 232.88
                      - date: '2025-06-02'
                        earnedPremium: 465.75
                        contributions:
                          - transactionId: 660e8400-e29b-41d4-a716-446655440001
                            policyVersion: 1
                            action: NEW_BUSINESS
                            dailyContribution: 232.88
                          - transactionId: 660e8400-e29b-41d4-a716-446655440002
                            policyVersion: 2
                            action: ENDORSE
                            dailyContribution: 232.87
        '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
                startAfterEnd:
                  summary: Start date after end date
                  value:
                    error:
                      code: INVALID_REQUEST
                      message: startDate must be before or equal to endDate
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          description: Policy not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                policyNotFound:
                  summary: Policy not found
                  value:
                    error:
                      code: NOT_FOUND
                      message: Policy 550e8400-e29b-41d4-a716-446655440001 not found
        '500':
          $ref: '#/components/responses/InternalServerError'
components:
  parameters:
    companyId:
      name: companyId
      in: path
      required: true
      schema:
        type: string
        format: uuid
      description: Company identifier
    policyIdPath:
      name: policyId
      in: path
      required: true
      schema:
        type: string
        format: uuid
      description: Policy identifier
  schemas:
    EarnedPremiumPolicyResponse:
      type: object
      description: >
        Earned premium for a single policy over the requested date range. Shape
        varies by detail level: - `transactions` — `transactionTotals` only
        (default) - `days` — adds `days` array (without per-transaction
        contributions) - `daysAndTransactions` — adds `days` array with
        `contributions` per day
      required:
        - policyId
        - policyNumber
        - earnedPremiumTotal
        - transactionTotals
      properties:
        policyId:
          type: string
          format: uuid
          description: The policy ID
        policyNumber:
          type: string
          description: Human-readable policy number
        earnedPremiumTotal:
          type: number
          description: >
            Total earned premium across all transactions for the requested date
            range
        transactionTotals:
          type: array
          description: >
            Each transaction's total earned premium contribution over the
            requested date range. Always present.
          items:
            $ref: '#/components/schemas/EarnedPremiumTransactionRangeTotal'
        days:
          type: array
          description: >
            Daily earned premium time series. Only present when `detail=days` or
            `detail=daysAndTransactions`. Contains one entry per day in the
            half-open interval [startDate, endDate).
          items:
            $ref: '#/components/schemas/EarnedPremiumOneDayTotal'
    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
    EarnedPremiumTransactionRangeTotal:
      type: object
      description: >
        One transaction's total earned premium contribution over the requested
        date range. Present at every detail level.
      required:
        - transactionId
        - policyVersion
        - action
        - totalContribution
      properties:
        transactionId:
          type: string
          format: uuid
          description: The transaction that produced this contribution
        policyVersion:
          type: integer
          description: Policy version number at the time of this transaction
        action:
          type: string
          enum:
            - NEW_BUSINESS
            - ENDORSE
            - CANCEL
            - REINSTATE
            - RENEW
          description: The transaction action type
        totalContribution:
          type: number
          description: >
            Net earned premium contributed by this transaction over the
            requested date range. Positive for
            NEW_BUSINESS/ENDORSE/REINSTATE/RENEW, negative for CANCEL.
    EarnedPremiumOneDayTotal:
      type: object
      description: >
        Net earned premium on a single day. Present when `detail=days` or
        `detail=daysAndTransactions`.
      required:
        - date
        - earnedPremium
      properties:
        date:
          type: string
          format: date
          description: The calendar date (YYYY-MM-DD)
        earnedPremium:
          type: number
          description: >
            Net earned premium on this day — the sum of all active transaction
            contributions whose effective range includes this date.
        contributions:
          type: array
          description: >
            Per-transaction breakdown of daily contributions. Only present when
            `detail=daysAndTransactions`.
          items:
            $ref: '#/components/schemas/EarnedPremiumTransactionOneDayContribution'
    EarnedPremiumTransactionOneDayContribution:
      type: object
      description: >
        One transaction's earned premium contribution on a single day. Present
        when `detail=daysAndTransactions`.
      required:
        - transactionId
        - policyVersion
        - action
        - dailyContribution
      properties:
        transactionId:
          type: string
          format: uuid
          description: The transaction that produced this contribution
        policyVersion:
          type: integer
          description: Policy version number at the time of this transaction
        action:
          type: string
          enum:
            - NEW_BUSINESS
            - ENDORSE
            - CANCEL
            - REINSTATE
            - RENEW
          description: The transaction action type
        dailyContribution:
          type: number
          description: >
            This transaction's daily earned premium rate. The transaction
            spreads its premium change evenly across its effective date range.
  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.

````