openapi: 3.1.0
info:
  title: Slidesfly HTTP API
  version: 0.1.0
  summary: Publish and manage HTML decks (agent / CLI BFF)
  description: |
    Machine-readable surface for Slidesfly agents. Prefer MCP (`@slidesfly/mcp`) when
    available; this OpenAPI documents the same HTTP BFF the CLI and MCP call.

    **Envelope:** success `{ ok: true, data, warnings? }` · failure
    `{ ok: false, error: { code, message, details?, hint?, trace_id? } }`.

    **Domains:** write APIs only on the app domain (`slidesfly.com`). Content domain
    (`slidesfly.xyz`) is reader-only.

    **Idempotency:** optional `Idempotency-Key` on publish create/update/restore. Same key +
    same body → cached response (24h). Same key + different body → `IDEMPOTENCY_CONFLICT`.
  contact:
    url: https://slidesfly.com
  license:
    name: MIT

servers:
  - url: https://slidesfly.com
    description: Production app domain
  - url: http://localhost:3000
    description: Local Next.js (set SLIDESFLY_API_URL)

tags:
  - name: Publish
  - name: Decks
  - name: Claim
  - name: CLI Auth

paths:
  /api/decks/anonymous:
    post:
      tags: [Publish]
      operationId: publishAnonymous
      summary: Publish an anonymous HTML deck
      description: |
        No auth. Returns `claim_token` once — store locally; never log it in chat.
        Rate limit: 10/min per IP. Daily anon quota applies.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file:
                  type: string
                  format: binary
                  description: Single-file `.html` / `.htm`
                title:
                  type: string
      responses:
        '200':
          description: Published
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnonPublishSuccess'
        '402':
          $ref: '#/components/responses/QuotaExceeded'
        '409':
          $ref: '#/components/responses/IdempotencyConflict'
        '422':
          $ref: '#/components/responses/InvalidHtml'
        '429':
          $ref: '#/components/responses/RateLimited'
        '451':
          $ref: '#/components/responses/MaliciousContent'

  /api/anon/{id}/update:
    post:
      tags: [Publish]
      operationId: updateAnonymousDeck
      summary: Upload a new version of a local anonymous deck
      description: |
        Authenticate with `X-Slidesfly-Claim-Token`. This credential-bearing route
        rejects every query parameter so claim tokens cannot enter access logs,
        history, or referrers under an alternate query name.
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: '#/components/parameters/AnonClaimToken'
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file:
                  type: string
                  format: binary
      responses:
        '200':
          description: Updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OwnedUpdateSuccess'
        '403':
          $ref: '#/components/responses/Forbidden'
        '422':
          $ref: '#/components/responses/InvalidHtml'
        '451':
          $ref: '#/components/responses/MaliciousContent'

  /api/anon/{id}/delete:
    post:
      tags: [Decks]
      operationId: deleteAnonymousDeck
      summary: Delete a local anonymous deck
      description: |
        Authenticate with `X-Slidesfly-Claim-Token`. This credential-bearing route
        rejects every query parameter so claim tokens cannot enter access logs,
        history, or referrers under an alternate query name.
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: '#/components/parameters/AnonClaimToken'
      responses:
        '200':
          description: Deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OkData'
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/anon/{id}/visibility:
    post:
      tags: [Decks]
      operationId: changeAnonymousDeckVisibility
      summary: Request an anonymous-deck visibility change
      description: |
        This endpoint deliberately always returns `ANONYMOUS_LIMITED`; claim the deck
        before changing visibility. No claim token is required or transmitted.
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [visibility]
              properties:
                visibility:
                  type: string
                  enum: [public, unlisted, private]
      responses:
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/decks:
    get:
      tags: [Decks]
      operationId: listOwnedDecks
      summary: List owned decks
      description: Bearer API key (`sk_…`) or session cookie.
      security:
        - bearerApiKey: []
        - sessionCookie: []
      responses:
        '200':
          description: Owned deck list
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListDecksSuccess'
        '401':
          $ref: '#/components/responses/AuthError'
    post:
      tags: [Publish]
      operationId: publishOwned
      summary: Publish an owned deck
      description: 'Requires `Authorization: Bearer sk_…`. Supports `.html` or Pro `.zip`.'
      security:
        - bearerApiKey: []
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file:
                  type: string
                  format: binary
                title:
                  type: string
                visibility:
                  type: string
                  enum: [public, unlisted, private]
      responses:
        '200':
          description: Published
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OwnedPublishSuccess'
        '401':
          $ref: '#/components/responses/AuthError'
        '402':
          $ref: '#/components/responses/QuotaExceeded'
        '409':
          $ref: '#/components/responses/IdempotencyConflict'
        '422':
          $ref: '#/components/responses/InvalidHtml'
        '429':
          $ref: '#/components/responses/RateLimited'
        '451':
          $ref: '#/components/responses/MaliciousContent'

  /api/decks/{id}/update:
    post:
      tags: [Publish]
      operationId: updateOwnedDeck
      summary: Upload a new version of an owned deck
      security:
        - bearerApiKey: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file:
                  type: string
                  format: binary
                title:
                  type: string
      responses:
        '200':
          description: Updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OwnedUpdateSuccess'
        '401':
          $ref: '#/components/responses/AuthError'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          $ref: '#/components/responses/IdempotencyConflict'

  /api/decks/{id}/versions:
    get:
      tags: [Decks]
      operationId: listOwnedVersions
      summary: List versions of an owned deck
      security:
        - bearerApiKey: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Version list (newest first); `is_current` marks MAX(version)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListVersionsSuccess'
        '401':
          $ref: '#/components/responses/AuthError'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'

  /api/decks/{id}/restore:
    post:
      tags: [Publish]
      operationId: restoreOwnedVersion
      summary: Restore an owned deck to a previous version
      description: |
        Copies R2 objects from the source version prefix into a new MAX+1 version
        (serving always uses the highest version integer). Works for single-file and
        multi-file decks. No-ops if the target is already current. Rejects quarantined
        and content-purged decks.
      security:
        - bearerApiKey: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [version]
              properties:
                version:
                  type: integer
                  minimum: 1
                  description: Source version to restore
      responses:
        '200':
          description: Restored (or already current)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RestoreOwnedSuccess'
        '401':
          $ref: '#/components/responses/AuthError'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          $ref: '#/components/responses/IdempotencyConflict'
        '410':
          description: Content purged after expiry
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorBody'

  /api/claim:
    post:
      tags: [Claim]
      operationId: claimAnonymousDecks
      summary: Claim anonymous decks onto the logged-in account
      description: |
        Send claim tokens only in the JSON body. This credential-bearing route
        rejects every query parameter.
      security:
        - bearerApiKey: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: array
              items:
                type: object
                required: [deck_id, claim_token]
                properties:
                  deck_id:
                    type: string
                  claim_token:
                    type: string
      responses:
        '200':
          description: Claim result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ClaimSuccess'
        '401':
          $ref: '#/components/responses/AuthError'

  /api/cli/auth/exchange:
    post:
      tags: [CLI Auth]
      operationId: exchangeCliAuth
      summary: Exchange PKCE authorization code for an API key
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [code, code_verifier]
              properties:
                code:
                  type: string
                code_verifier:
                  type: string
      responses:
        '200':
          description: API key issued
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKeyExchangeSuccess'

  /api/cli/auth/device/register:
    post:
      tags: [CLI Auth]
      operationId: registerDeviceAuth
      summary: Register a device-code PKCE challenge
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [state, code_challenge]
              properties:
                state:
                  type: string
                code_challenge:
                  type: string
                code_challenge_method:
                  type: string
                  enum: [S256]
                  default: S256
      responses:
        '200':
          description: Registered
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OkData'

  /api/cli/auth/exchange-code:
    post:
      tags: [CLI Auth]
      operationId: exchangeDeviceCode
      summary: Exchange device user_code for an API key
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [user_code, state, code_verifier]
              properties:
                user_code:
                  type: string
                state:
                  type: string
                code_verifier:
                  type: string
      responses:
        '200':
          description: API key issued
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKeyExchangeSuccess'

components:
  securitySchemes:
    bearerApiKey:
      type: http
      scheme: bearer
      description: Slidesfly API key (`sk_…`) from login / account page
    sessionCookie:
      type: apiKey
      in: cookie
      name: sb-access-token
      description: Supabase session cookie (dashboard)

  parameters:
    AnonClaimToken:
      name: X-Slidesfly-Claim-Token
      in: header
      required: true
      schema:
        type: string
        minLength: 1
      description: |
        Local anonymous-deck ownership credential. Never place this value in a URL,
        command output, chat transcript, or application log.
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      schema:
        type: string
        maxLength: 128
        pattern: '^[\x21-\x7E]+$'
      description: |
        Optional. Printable ASCII, ≤128 chars (UUID recommended). Scoped per
        route + actor (server-keyed, domain-separated IP HMAC for anon;
        user/key for owned). TTL 24h.

  schemas:
    ApiWarning:
      type: object
      required: [code, message]
      properties:
        code:
          type: string
        severity:
          type: string
          enum: [info, warning]
        message:
          type: string
        recommended_action:
          type: string

    ErrorCode:
      type: string
      enum:
        - INVALID_HTML
        - MALICIOUS_CONTENT
        - QUOTA_EXCEEDED
        - RATE_LIMITED
        - AUTH_REQUIRED
        - AUTH_INVALID
        - FORBIDDEN
        - DECK_NOT_FOUND
        - EXPIRED
        - PASSWORD_REQUIRED
        - ANONYMOUS_LIMITED
        - DECK_NOT_OWNED
        - BILLING_NOT_ENABLED
        - ACCOUNT_DELETION_SCHEDULED
        - IDEMPOTENCY_CONFLICT
        - INTERNAL_ERROR
        - SERVICE_UNAVAILABLE

    ApiErrorBody:
      type: object
      required: [ok, error]
      properties:
        ok:
          type: boolean
          const: false
        error:
          type: object
          required: [code, message]
          properties:
            code:
              $ref: '#/components/schemas/ErrorCode'
            message:
              type: string
            details: {}
            hint:
              type: string
            trace_id:
              type: string

    AnonPublishSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required:
            - deck_id
            - version_id
            - url
            - claim_token
            - title
            - visibility
            - size_bytes
            - format
            - anonymous
          properties:
            deck_id:
              type: string
            version_id:
              type: string
            url:
              type: string
              format: uri
            claim_token:
              type: string
            title:
              type: string
            visibility:
              const: unlisted
            size_bytes:
              type: integer
            format:
              const: single_file
            anonymous:
              const: true
        warnings:
          type: array
          items:
            $ref: '#/components/schemas/ApiWarning'

    OwnedPublishSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required:
            - deck_id
            - version_id
            - url
            - title
            - visibility
            - size_bytes
            - format
            - anonymous
          properties:
            deck_id:
              type: string
            version_id:
              type: string
            url:
              type: string
            title:
              type: string
            visibility:
              type: string
              enum: [public, unlisted, private]
            size_bytes:
              type: integer
            format:
              type: string
              enum: [single_file, multi_file]
            file_count:
              type: integer
            anonymous:
              const: false

    OwnedUpdateSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required: [deck_id, version_id, version, size_bytes, sha256, title]
          properties:
            deck_id:
              type: string
            version_id:
              type: string
            version:
              type: integer
            size_bytes:
              type: integer
            sha256:
              type: string
            title:
              type: string

    ListVersionsSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required: [deck_id, versions]
          properties:
            deck_id:
              type: string
            versions:
              type: array
              items:
                type: object
                required: [version, version_id, created_at, size_bytes, sha256, is_current]
                properties:
                  version:
                    type: integer
                  version_id:
                    type: string
                  created_at:
                    type: string
                    format: date-time
                  size_bytes:
                    type: integer
                  sha256:
                    type: string
                  is_current:
                    type: boolean

    RestoreOwnedSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required:
            [deck_id, version_id, version, restored_from, size_bytes, sha256, title]
          properties:
            deck_id:
              type: string
            version_id:
              type: string
            version:
              type: integer
              description: New live version (or unchanged if already current)
            restored_from:
              type: integer
            size_bytes:
              type: integer
            sha256:
              type: string
            title:
              type: string

    ListDecksSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required: [decks]
          properties:
            decks:
              type: array
              items:
                type: object
                required: [id, title, visibility, url, created_at]
                properties:
                  id:
                    type: string
                  title:
                    type: string
                  visibility:
                    type: string
                  url:
                    type: string
                  created_at:
                    type: string
                    format: date-time

    ClaimSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required: [claimed, failed]
          properties:
            claimed:
              type: array
              items:
                type: string
            failed:
              type: array
              items:
                type: object
                properties:
                  deck_id:
                    type: string
                  reason:
                    type: string

    ApiKeyExchangeSuccess:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data:
          type: object
          required: [api_key, prefix]
          properties:
            api_key:
              type: string
            prefix:
              type: string

    OkData:
      type: object
      required: [ok, data]
      properties:
        ok:
          const: true
        data: {}

  responses:
    AuthError:
      description: Missing or invalid credentials
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
    Forbidden:
      description: Not allowed
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
    NotFound:
      description: Deck not found
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
    QuotaExceeded:
      description: Plan or anonymous quota exceeded
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
    RateLimited:
      description: Rate limited
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
    InvalidHtml:
      description: Invalid upload
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
    MaliciousContent:
      description: Security scan rejected the deck
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
    IdempotencyConflict:
      description: Idempotency-Key reused with a different body (or still in flight)
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorBody'
