openapi: 3.1.0
info:
  title: SonmiAI API
  version: 1.0.0
  description: |
    Run SEO audits, poll results, and download PDF reports. Authenticate with an API key
    created in Dashboard → API. Send `Authorization: Bearer ap_...`.
servers:
  - url: https://app.sonmiai.com/api/v1
    description: Production
  - url: http://localhost:3000/api/v1
    description: Local
security:
  - bearerAuth: []
paths:
  /sites:
    get:
      summary: List sites
      operationId: listSites
      responses:
        "200":
          description: Sites with the latest audit
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: "#/components/schemas/Site"
    post:
      summary: Add a site
      operationId: createSite
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [url]
              properties:
                url:
                  type: string
                  example: https://example.com
                label:
                  type: string
                runAudit:
                  type: boolean
                  default: true
      responses:
        "201":
          description: Site created
        "402":
          description: Plan limit reached
  /audits:
    get:
      summary: List audits
      operationId: listAudits
      parameters:
        - in: query
          name: siteId
          schema:
            type: string
      responses:
        "200":
          description: Recent audits
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: "#/components/schemas/AuditSummary"
    post:
      summary: Start an audit
      operationId: createAudit
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [siteId]
              properties:
                siteId:
                  type: string
      responses:
        "202":
          description: Audit queued
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: object
                    properties:
                      id:
                        type: string
                      siteId:
                        type: string
                      status:
                        type: string
                      pollUrl:
                        type: string
        "402":
          description: Monthly page budget exceeded
        "409":
          description: Concurrent audit limit reached
  /audits/{id}:
    get:
      summary: Get an audit
      operationId: getAudit
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: string
      responses:
        "200":
          description: Audit result with issues
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    $ref: "#/components/schemas/Audit"
        "404":
          description: Not found
  /audits/{id}/report:
    get:
      summary: Download the PDF report
      operationId: getAuditReport
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: string
        - in: query
          name: download
          schema:
            type: string
            enum: ["1"]
      responses:
        "200":
          description: PDF bytes
          content:
            application/pdf:
              schema:
                type: string
                format: binary
        "404":
          description: Report not ready
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API key
  schemas:
    Site:
      type: object
      properties:
        id:
          type: string
        url:
          type: string
        label:
          type: string
          nullable: true
        schedule:
          type: string
          nullable: true
          enum: [DAILY, WEEKLY, MONTHLY]
        nextAuditAt:
          type: string
          format: date-time
          nullable: true
        latestAudit:
          $ref: "#/components/schemas/AuditSummary"
    AuditSummary:
      type: object
      properties:
        id:
          type: string
        status:
          type: string
          enum: [QUEUED, RUNNING, COMPLETE, FAILED]
        score:
          type: integer
          nullable: true
        pagesAudited:
          type: integer
        startedAt:
          type: string
          format: date-time
        completedAt:
          type: string
          format: date-time
          nullable: true
        trigger:
          type: string
        error:
          type: string
          nullable: true
    Audit:
      allOf:
        - $ref: "#/components/schemas/AuditSummary"
        - type: object
          properties:
            summary:
              type: string
              nullable: true
            stats:
              type: object
              additionalProperties: true
            gsc:
              type: object
              additionalProperties: true
              nullable: true
            site:
              type: object
              additionalProperties: true
            issues:
              type: array
              items:
                type: object
                additionalProperties: true
            reportUrl:
              type: string
              nullable: true
