openapi: 3.1.0
info:
  title: Sentiment Analysis API
  version: "2.0.0"
  description: |
    A serverless API that scores text for sentiment and urgency (e.g. support
    tickets, reviews), manages subscriptions, and listens for Stripe webhooks.
servers:
  - url: https://triagesupport.com
paths:
  /hello:
    get:
      summary: Health check
      description: Returns a simple “API is running” message.
      security:
        - ApiKeyAuth: []
      responses:
        "200":
          description: Service is up
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Sentiment API is up and running!
  /analyze:
    post:
      summary: Score text for sentiment and urgency
      description: |
        Intended for support tickets, reviews, or any short customer message.
        Returns a sentiment label/score plus an urgency classification so
        high-urgency, negative messages can be triaged first.
      security:
        - ApiKeyAuth: []
      parameters:
        - in: header
          name: x-api-key
          required: true
          schema:
            type: string
        - in: header
          name: Idempotency-Key
          required: false
          description: |
            Reuse the same value on a retry (e.g. after a timeout) to get back
            the original result instead of a new billable call. Honored for
            24 hours. Reusing a key with a different request body returns 409.
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [text]
              properties:
                text:
                  type: string
                  description: The message to analyze
      responses:
        "200":
          description: Sentiment and urgency scores
          content:
            application/json:
              schema:
                type: object
                properties:
                  sentiment:
                    type: string
                    enum: [positive, neutral, negative]
                  sentiment_score:
                    type: number
                  urgency:
                    type: string
                    enum: [low, medium, high]
                  reason:
                    type: string
                    description: A short phrase, bounded to 300 characters.
        "400":
          description: |
            The request body is missing, not a JSON object, has a
            non-string or blank 'text' field, or 'text' exceeds the
            4000-character limit.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    example: "'text' must be a string"
        "409":
          description: Idempotency-Key was already used with a different request body
        "429":
          description: |
            Monthly free-tier quota exceeded. Distinct from any future
            rate-limiting response (not yet implemented) - this is about
            the trial allowance, not request frequency.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    example: Monthly quota exceeded
                  allowance:
                    type: integer
                    description: The total free-tier calls allowed per calendar month.
                  remaining:
                    type: integer
                    description: Always 0 on this response - included for a consistent response shape.
                  reset_time:
                    type: string
                    format: date-time
                    description: UTC start of the next calendar month, when a fresh allowance becomes available.
        "413":
          description: Request body exceeds the server's size limit.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    example: Request body too large
        "502":
          description: |
            The analysis service is unavailable, or its output failed
            contract validation (missing/invalid field, out-of-range or
            non-finite sentiment_score, an unexpected response
            structure, or a 'reason' over 300 characters). Never billed.
        "503":
          description: |
            Either a request with this Idempotency-Key is still being
            processed by a concurrent call, or usage/quota tracking is
            temporarily unavailable - in both cases, retry shortly with
            the same Idempotency-Key. The model is never called on a
            503, so no Stripe charge is ever made for that attempt. A
            free-tier credit can in rare cases still end up consumed
            even though you received a 503 (our own confirmation of the
            reservation was lost, not the reservation itself) - retrying
            with the same key resumes that same reservation rather than
            spending a second one, so this is never double-counted.
          headers:
            Retry-After:
              schema:
                type: integer
              description: Only present for the concurrent-request case.
  /demo:
    post:
      summary: Unauthenticated try-it-yourself sentiment/urgency scoring
      description: |
        Public demo endpoint for the landing page. No API key, no
        billing, no persistence - results are not recorded anywhere and
        there is no idempotency support. Subject to its own smaller text
        limit and per-source daily attempt ceilings; not intended for
        production integration (use /analyze for that).
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [text]
              properties:
                text:
                  type: string
                  description: The message to analyze (max 500 characters).
      responses:
        "200":
          description: Sentiment and urgency scores
          content:
            application/json:
              schema:
                type: object
                properties:
                  sentiment:
                    type: string
                    enum: [positive, neutral, negative]
                  sentiment_score:
                    type: number
                  urgency:
                    type: string
                    enum: [low, medium, high]
                  reason:
                    type: string
                    description: A short phrase, bounded to 300 characters.
        "400":
          description: |
            The request body is missing, not a JSON object, has a
            non-string or blank 'text' field, or 'text' exceeds the
            500-character limit.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    example: "'text' must be a string"
        "413":
          description: Request body exceeds the server's size limit.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    example: Request body too large
        "429":
          description: A per-source or global daily demo-attempt ceiling was reached.
        "502":
          description: |
            The analysis service is unavailable, or its output failed
            contract validation. Never billed (this endpoint is never
            billed in the first place).
  /signup:
    post:
      summary: Request a free-tier API key (email confirmation required)
      description: |
        No payment required. Always returns a generic 202 - the same
        response whether the email is new, already has an account, or
        already has a confirmation pending, so the response itself never
        reveals which. A new signup gets a confirmation email with a
        one-time link; the key is only issued once that link is confirmed
        via POST /signup/confirm. An email that already has an account
        gets a notice to that inbox instead, never a new or rotated key.
        Call /checkout afterwards to upgrade to the metered paid plan.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [email]
              properties:
                email:
                  type: string
                  format: email
      responses:
        "202":
          description: Signup accepted - check the given email for a confirmation link
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: pending
        "400":
          description: Missing or malformed email
        "429":
          description: Per-IP signup attempt limit exceeded
        "503":
          description: Temporarily unavailable - safe to retry
  /signup/confirm:
    get:
      summary: Confirmation page for a signup link
      description: |
        Renders the page a confirmation email's link opens. Performs no
        action by itself (so an email client or scanner pre-fetching the
        link can never activate an account) - the page's own form submits
        the POST below, which is the only thing that actually issues a key.
      parameters:
        - name: email
          in: query
          required: true
          schema:
            type: string
        - name: token
          in: query
          required: true
          schema:
            type: string
      responses:
        "200":
          description: Confirmation page (HTML)
    post:
      summary: Confirm a signup and receive the API key
      description: |
        The token from the confirmation email is the only proof of
        ownership required - it is single-use and expires 30 minutes
        after being sent. Replaying the same token after it already
        succeeded returns the same key again (safe retry after a lost
        response); any other mismatch, unknown email, or expired token
        returns the same generic error, never revealing which.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [email, token]
              properties:
                email:
                  type: string
                token:
                  type: string
      responses:
        "200":
          description: API key issued
          content:
            application/json:
              schema:
                type: object
                properties:
                  api_key:
                    type: string
                  plan:
                    type: string
                    example: free
        "400":
          description: This confirmation link is invalid or has expired
        "503":
          description: Temporarily unavailable - safe to retry
  /checkout:
    post:
      summary: Start a Stripe Checkout session to upgrade to the paid plan
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [api_key, success_url, cancel_url]
              properties:
                api_key:
                  type: string
                success_url:
                  type: string
                cancel_url:
                  type: string
      responses:
        "200":
          description: Checkout session created
          content:
            application/json:
              schema:
                type: object
                properties:
                  checkout_url:
                    type: string
        "400":
          description: Missing required fields
        "403":
          description: Invalid API key
        "502":
          description: Stripe error
  /webhook:
    post:
      summary: Stripe webhook handler
      description: |
        Receives Stripe events (checkout.session.completed) and marks the
        corresponding API key as upgraded to the paid plan.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
      responses:
        "200":
          description: Event processed
        "400":
          description: Invalid webhook signature
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
