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

# Test a draft SQL metric query

> Run a draft SQL Float metric query against a fixed sample `SimulationDataFrames`
artifact and return the value it would produce. This is org-agnostic pure compute —
it never touches your organization's data — so it needs no metric to exist yet and
makes no changes.

Use `GET /v1/metrics/sql-schema` to discover the tables, columns, and sample rows
your query runs against. The query must return the columns `start_offset_milliseconds`
and `value`.

Routine authoring failures (invalid SQL, no matching rows) return `200` with a
populated `error` field rather than an HTTP error; only a malformed request body is a
`400`.




## OpenAPI

````yaml /api-reference/v1/metrics-v1.yaml post /metrics/sql:test
openapi: 3.0.3
info:
  title: Coval Metrics API
  version: 1.0.0
  description: |
    Manage custom evaluation metrics for agent testing and monitoring.
  contact:
    name: Coval API Support
    email: support@coval.dev
    url: https://docs.coval.ai
  license:
    name: Proprietary
    url: https://coval.dev/terms
servers:
  - url: https://api.coval.dev/v1
    description: Production API
security:
  - ApiKeyAuth: []
tags:
  - name: Metrics
    description: CRUD operations for custom evaluation metrics
paths:
  /metrics/sql:test:
    post:
      tags:
        - Metrics
      summary: Test a draft SQL metric query
      description: >
        Run a draft SQL Float metric query against a fixed sample
        `SimulationDataFrames`

        artifact and return the value it would produce. This is org-agnostic
        pure compute —

        it never touches your organization's data — so it needs no metric to
        exist yet and

        makes no changes.


        Use `GET /v1/metrics/sql-schema` to discover the tables, columns, and
        sample rows

        your query runs against. The query must return the columns
        `start_offset_milliseconds`

        and `value`.


        Routine authoring failures (invalid SQL, no matching rows) return `200`
        with a

        populated `error` field rather than an HTTP error; only a malformed
        request body is a

        `400`.
      operationId: testSqlMetric
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TestSqlMetricRequest'
      responses:
        '200':
          description: Query evaluated (check `error` for authoring failures)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TestSqlMetricResponse'
        '400':
          description: Invalid request body
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          description: Permission denied
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          $ref: '#/components/responses/InternalError'
        '503':
          $ref: '#/components/responses/ServiceUnavailable'
components:
  schemas:
    TestSqlMetricRequest:
      type: object
      required:
        - sql_query
      properties:
        sql_query:
          type: string
          description: >
            The draft SQL Float metric query to evaluate. Must return the
            columns

            `start_offset_milliseconds` and `value`.
        aggregation_method:
          type: string
          nullable: true
          description: >-
            How per-row values are aggregated into a single metric value (e.g.
            MEAN, SUM).
        unit:
          type: string
          nullable: true
          description: Optional result unit.
      additionalProperties: false
    TestSqlMetricResponse:
      type: object
      properties:
        value:
          type: number
          nullable: true
          description: >-
            The aggregated metric value, or null when the query produced no
            value.
        unit:
          type: string
          nullable: true
        aggregation_method:
          type: string
          nullable: true
        row_count:
          type: integer
          description: Number of rows the query returned against the sample data.
        subvalues:
          type: array
          items:
            $ref: '#/components/schemas/SqlMetricTestSubvalue'
        error:
          type: string
          nullable: true
          description: >
            Set when the query could not be evaluated (invalid SQL, missing
            columns, or no

            matching rows). Present on a 200 response — routine authoring
            failures are not HTTP errors.
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          type: object
          required:
            - code
            - message
          properties:
            code:
              type: string
              enum:
                - INVALID_ARGUMENT
                - UNAUTHENTICATED
                - PERMISSION_DENIED
                - NOT_FOUND
                - ALREADY_EXISTS
                - INTERNAL
            message:
              type: string
            details:
              type: array
              items:
                type: object
                properties:
                  field:
                    type: string
                    nullable: true
                  description:
                    type: string
    SqlMetricTestSubvalue:
      type: object
      required:
        - start_offset_seconds
        - end_offset_seconds
        - value
      properties:
        start_offset_seconds:
          type: number
        end_offset_seconds:
          type: number
        value:
          type: number
  responses:
    Unauthorized:
      description: Authentication failed
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: UNAUTHENTICATED
              message: Authentication failed
              details:
                - field: X-API-Key
                  description: Invalid or missing API key
    InternalError:
      description: Internal server error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: INTERNAL
              message: Internal server error
    ServiceUnavailable:
      description: Service temporarily unavailable
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: INTERNAL
              message: Service temporarily unavailable
              details:
                - description: Database routing is temporarily unavailable. Please retry.
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
      description: API key for authentication

````