openapi: 3.1.0
info:
  title: Webmatik API
  description: |
    Run AI-powered website audits programmatically. Same engine that powers webmatik.ai.

    API access requires a Starter or Growth plan. Get an API key at https://webmatik.ai/account.
  version: 1.0.0
  contact:
    name: Webmatik Support
    url: https://webmatik.ai/contact
  license:
    name: Proprietary
servers:
  - url: https://webmatik.ai/api/v1
    description: Production
security:
  - ApiKeyAuth: []
paths:
  /audit:
    post:
      summary: Start a new audit
      description: |
        Initiates a website audit. Returns immediately with an audit ID.
        Use GET /audit/{id} to poll for results, or pass `webhookUrl` to receive a callback.
      operationId: startAudit
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [url]
              properties:
                url:
                  type: string
                  description: URL to audit. Protocol is optional.
                  example: stripe.com
                webhookUrl:
                  type: string
                  format: uri
                  description: HTTPS URL that will receive a POST when the audit completes.
                  example: https://your-app.com/webhooks/webmatik
      responses:
        '202':
          description: Audit started
          content:
            application/json:
              schema:
                type: object
                properties:
                  auditId: { type: string, format: uuid }
                  status: { type: string, enum: [processing] }
                  redirectTo: { type: string }
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/RateLimited'

  /audit/{id}:
    get:
      summary: Get audit result
      description: |
        Returns the current status and result of an audit.
        Poll every 5-10 seconds until status is "completed" or "failed".
      operationId: getAudit
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: Audit result or current status
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/AuditCompleted'
                  - $ref: '#/components/schemas/AuditProcessing'
                  - $ref: '#/components/schemas/AuditFailed'
        '401': { $ref: '#/components/responses/Unauthorized' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404':
          description: Audit not found
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
      description: API key with prefix `wmk_`. Generate at https://webmatik.ai/account.

  schemas:
    Error:
      type: object
      properties:
        error: { type: string }

    AuditProcessing:
      type: object
      properties:
        status: { type: string, enum: [processing] }
        auditId: { type: string, format: uuid }
        url: { type: string }
        geoStatus: { type: string, enum: [pending, complete] }
        visionStatus: { type: string, enum: [pending, complete] }
        partialScore: { type: number }
        message: { type: string }

    AuditFailed:
      type: object
      properties:
        status: { type: string, enum: [failed] }
        auditId: { type: string, format: uuid }

    AuditCompleted:
      type: object
      properties:
        status: { type: string, enum: [completed] }
        auditId: { type: string, format: uuid }
        url: { type: string }
        score: { type: number, description: "Growth Score 0-100" }
        grade: { type: string, enum: [Poor, Fair, Good, Great, Excellent] }
        siteType: { type: string }
        language: { type: string, description: "ISO 639-1 language code" }
        reportUrl: { type: string }
        timestamp: { type: string, format: date-time }
        summary:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/CategorySummary'
        actionPlan:
          $ref: '#/components/schemas/ActionPlan'
        seo:
          type: object
          properties:
            estimatedTraffic: { type: integer }
            totalKeywords: { type: integer }
            top3Keywords: { type: integer }
            top10Keywords: { type: integer }
            topKeywords:
              type: array
              items: { $ref: '#/components/schemas/Keyword' }
        geoVisibility:
          type: object
          properties:
            score: { type: number }
            promptsChecked: { type: integer }
            mentionedIn: { type: integer }
            citedIn: { type: integer }
            providers: { type: array, items: { type: object } }
            queries: { type: array, items: { type: object } }
        innerPages:
          type: array
          items: { $ref: '#/components/schemas/InnerPage' }

    CategorySummary:
      type: object
      properties:
        score: { type: number }
        passed: { type: integer }
        issues: { type: integer }
        topIssue: { type: string, nullable: true }
        allIssues:
          type: array
          items:
            type: object
            properties:
              name: { type: string }
              details: { type: string }
              current: { type: string, nullable: true }
              recommended: { type: string, nullable: true }

    ActionPlan:
      type: object
      properties:
        total: { type: integer }
        critical: { type: array, items: { $ref: '#/components/schemas/Recommendation' } }
        important: { type: array, items: { $ref: '#/components/schemas/Recommendation' } }
        niceToHave: { type: array, items: { $ref: '#/components/schemas/Recommendation' } }

    Recommendation:
      type: object
      properties:
        category: { type: string }
        title: { type: string }
        description: { type: string }
        howToFix: { type: string, nullable: true }
        effort: { type: string, enum: [quick, medium, complex], nullable: true }
        impact: { type: string, enum: [high, medium, low], nullable: true }
        businessImpact: { type: string, nullable: true }
        scoreImpact: { type: string, nullable: true }
        affectedPages: { type: array, items: { type: string }, nullable: true }

    Keyword:
      type: object
      properties:
        keyword: { type: string }
        position: { type: integer }
        searchVolume: { type: integer }
        intent: { type: string }
        url: { type: string }

    InnerPage:
      type: object
      properties:
        url: { type: string }
        score: { type: number }
        issueCount: { type: integer }
        topIssues: { type: array, items: { type: string } }

  responses:
    BadRequest:
      description: Bad Request — invalid parameters
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    Unauthorized:
      description: Unauthorized — missing or invalid API key
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    Forbidden:
      description: Forbidden — API access requires Starter or Growth plan
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    RateLimited:
      description: Too Many Requests — monthly audit limit reached
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
