openapi: 3.1.0
info:
  title: ConsentOS Consent Capture API
  version: 1.0.0
  description: >-
    Record consent using an organisation-scoped API key minted in the app's Settings.
    The consent capture API is available on Growth and above. See
    https://consentos.in/pricing/ for plan detail.
    Request and response shapes verified from app source on 15 October 2026.
    All example payloads use synthetic values.
servers:
  - url: https://app.consentos.in
security:
  - bearerAuth: []
tags:
  - name: Consent
    description: Consent capture.
paths:
  /api/v1/consent:
    post:
      tags: [Consent]
      operationId: createConsent
      summary: Record consent
      description: >-
        Requires Growth or above and an active trial term or activated plan.
        Rate limits are 60 grants per minute per key, then 120 per minute across
        all keys of the organisation. A replay with the same Idempotency-Key
        returns the original 201 body with Idempotent-Replayed: true.
        If the signing or ledger outcome is unknown, retry the 503 response
        with the same Idempotency-Key.
      parameters:
        - name: Idempotency-Key
          in: header
          required: false
          description: >-
            Optional key of 1 to 255 printable ASCII characters with no spaces.
            Scoped to the organisation for 24 hours. Reusing a key for a different
            body returns 422.
          schema:
            type: string
            minLength: 1
            maxLength: 255
            pattern: '^[!-~]+$'
          example: example-consent-0001
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ConsentCapture'
            example:
              principal_email: example@example.com
              purpose_code: marketing
              expires_at: '2099-01-01T00:00:00Z'
              mechanism: api_call
      responses:
        '201':
          description: Consent recorded. An idempotent replay returns the original body.
          headers:
            Idempotent-Replayed:
              description: Present with value true on an idempotent replay.
              schema:
                type: boolean
                const: true
              example: true
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ConsentRecord'
              example:
                id: '00000000-0000-0000-0000-000000000000'
                artifact_hash: '0000000000000000000000000000000000000000000000000000000000000000'
                granted_at: '2026-10-15T00:00:00Z'
        '400':
          description: >-
            Invalid request. An invalid expires_at produces a field error named
            expires_at; it must be an ISO 8601 date-time in the future.
            An invalid Idempotency-Key returns the error shown below.
          content:
            application/json:
              schema:
                type: object
              examples:
                invalidIdempotencyKey:
                  value:
                    error: Idempotency-Key must be 1 to 255 printable ASCII characters with no spaces
        '401':
          description: Missing or malformed Authorization header.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                error: Unauthorized
        '403':
          description: Revoked API key, unavailable plan feature, or ended trial term.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                revokedKey:
                  value:
                    error: Forbidden
                lowerPlan:
                  value:
                    error: This feature is not available on your plan.
                trialEnded:
                  value:
                    error: Trial term ended. This workspace is read-only until a plan is activated.
        '422':
          description: Idempotency-Key was already used for a different request body.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                error: This Idempotency-Key was already used for a different consent request
        '429':
          description: Per-key or organisation rate limit exceeded.
          headers:
            Retry-After:
              description: Number of seconds to wait before retrying.
              schema:
                type: integer
              example: 60
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                error: Too many requests
        '500':
          description: Failed to record the consent event or create the consent artifact.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                eventFailure:
                  value:
                    error: Failed to record consent event
                artifactFailure:
                  value:
                    error: Failed to create consent artifact
        '503':
          description: >-
            Key service not provisioned, or signing or ledger outcome unknown.
            When the signing or ledger outcome is unknown, retry with the same
            Idempotency-Key. The example is for the unprovisioned key service.
          content:
            application/json:
              schema:
                type: object
              examples:
                keyServiceUnavailable:
                  value:
                    error: Service unavailable
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        Organisation-scoped API key minted in the app's Settings.
        Send Authorization: Bearer <api key>. Keep the key private and outside source control.
  schemas:
    ConsentCapture:
      type: object
      required: [principal_email, purpose_code]
      properties:
        principal_email:
          type: string
          format: email
          example: example@example.com
        purpose_code:
          type: string
          enum:
            - marketing
            - analytics
            - service_delivery
            - legal_compliance
            - research
            - personalisation
            - third_party_sharing
            - profiling
        expires_at:
          type: string
          format: date-time
          description: Optional ISO 8601 date-time that must be in the future.
          example: '2099-01-01T00:00:00Z'
        mechanism:
          type: string
          const: api_call
          description: Fixed to api_call and may be omitted.
    ConsentRecord:
      type: object
      required: [id, artifact_hash, granted_at]
      properties:
        id:
          type: string
          format: uuid
          example: '00000000-0000-0000-0000-000000000000'
        artifact_hash:
          type: string
          description: Hexadecimal SHA-256 hash of the consent artifact.
          pattern: '^[0-9a-fA-F]{64}$'
        granted_at:
          type: string
          format: date-time
    ApiError:
      type: object
      required: [error]
      properties:
        error:
          type: string
