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

# Run aggregations

> Computes sums, averages, and counts over matching records, with
optional grouping. Maximum 10 aggregation steps per call.




## OpenAPI

````yaml /api-reference/ai-collection/swagger.yml post /v1/collections/{collection_id}/data/aggregate
openapi: 3.0.3
info:
  version: 1.0.0
  title: Collections API
  description: |
    Public REST API for the Prisme.ai Collections workspace: named collections
    of structured records with typed schemas, MongoDB-style querying,
    aggregations, and an MCP (JSON-RPC 2.0) endpoint for AI agents.

    Records are isolated per owner (user or agent); a collection can be shared
    with additional agents through its `agent_ids` list. Every write emits an
    audit event.
  contact:
    name: Prisme.ai
    url: https://prisme.ai
servers:
  - url: https://{host}/v2/workspaces/slug:ai-collection-v3/webhooks
    description: Prisme.ai workspace webhooks
    variables:
      host:
        default: api.studio.prisme.ai
        description: API host (override for self-hosted or sandbox)
security:
  - BearerAuth: []
  - OrgApiKeyAuth: []
tags:
  - name: Collections
    description: Create, list, read, update, and delete collections (metadata and schema).
  - name: Records
    description: Insert, query, read, update, upsert, and delete records in a collection.
  - name: Analytics
    description: Count, distinct values, and aggregations (sum, avg, count with grouping).
  - name: MCP
    description: >-
      JSON-RPC 2.0 endpoint exposing the `data_*` tools to AI agents and MCP
      clients.
paths:
  /v1/collections/{collection_id}/data/aggregate:
    parameters:
      - $ref: '#/components/parameters/CollectionId'
    post:
      tags:
        - Analytics
      summary: Run aggregations
      description: |
        Computes sums, averages, and counts over matching records, with
        optional grouping. Maximum 10 aggregation steps per call.
      operationId: aggregateRecords
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - steps
              properties:
                filter:
                  type: object
                  description: MongoDB-style filter applied before aggregating.
                group_by:
                  type: string
                  description: Field to group results by.
                  example: category
                steps:
                  type: array
                  maxItems: 10
                  items:
                    type: object
                    required:
                      - type
                      - output_field
                    properties:
                      type:
                        type: string
                        enum:
                          - sum
                          - avg
                          - count
                      input_field:
                        type: string
                        description: Numeric field to aggregate (unused for `count`).
                      output_field:
                        type: string
            example:
              filter:
                status: completed
              group_by: category
              steps:
                - type: sum
                  input_field: amount
                  output_field: total
                - type: avg
                  input_field: amount
                  output_field: avg_amount
      responses:
        '200':
          description: Aggregation results (one row per group).
          content:
            application/json:
              schema:
                type: object
                properties:
                  results:
                    type: array
                    items:
                      type: object
                      additionalProperties: true
        '400':
          $ref: '#/components/responses/BadRequest'
components:
  parameters:
    CollectionId:
      name: collection_id
      in: path
      required: true
      schema:
        type: string
        format: uuid
      description: Collection identifier returned at creation.
  responses:
    BadRequest:
      description: Invalid arguments (bad filter operator, batch too large, name invalid…).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Error:
      type: object
      properties:
        error:
          type: string
          example: InvalidArguments
        message:
          type: string
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: 'User session JWT. Send as `Authorization: Bearer <token>`.'
    OrgApiKeyAuth:
      type: apiKey
      in: header
      name: x-prismeai-api-key
      description: Organization-scoped API key.

````