openapi: 3.0.3
info:
  title: ThunderProxy Account API
  description: |
    Read account balance and paginated subscription summaries for the caller.
    Describes observed behavior of `GET /v1/api/account` as implemented in
    `routes/api/account.js`, `controller/api/account.js`, and
    `service/api/account.js`.
  version: 1.0.0

servers:
  - url: https://api.thunderproxy.com/v1/api
    description: Production
  - url: http://localhost:3500/v1/api
    description: Local development (default PORT)

security:
  - SubscriptionApiKey: []

paths:
  /account:
    get:
      operationId: getAccount
      summary: Get account balance and subscriptions
      description: |
        Returns the account balance and one page of the caller's subscriptions,
        sorted by `createdAt` descending. A page beyond the last page returns an
        empty `subscriptions` array with `totalPages: 0` when there are no results.
      tags:
        - Account
      parameters:
        - $ref: "#/components/parameters/Page"
        - $ref: "#/components/parameters/Size"
        - $ref: "#/components/parameters/ActiveOnly"
      responses:
        "200":
          description: Account balance and subscription page.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AccountSuccessResponse"
        "400":
          description: Query validation failed. Messages are joined with `; `.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "401":
          description: API key matched middleware but the user record was not found on the second lookup.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "403":
          description: Missing, invalid, or banned API key, or email not verified.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ForbiddenResponse"
        "500":
          description: Unhandled server error.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ServerErrorResponse"

components:
  securitySchemes:
    SubscriptionApiKey:
      type: apiKey
      in: header
      name: X-API-Key
      description: API key from your dashboard profile. Send it in the `X-API-Key` header.
    SubscriptionApiKeyQuery:
      type: apiKey
      in: query
      name: apiKey
      deprecated: true
      description: |
        Deprecated. Your API key in the `apiKey` query parameter. Still accepted for
        existing integrations. If both the header and this parameter are sent, the header wins.
        Do not put keys in URLs. Use the `X-API-Key` header instead.

  parameters:
    Page:
      name: page
      in: query
      description: 1-based page index. Missing or empty defaults to 1.
      schema:
        type: integer
        minimum: 1
        default: 1
    Size:
      name: size
      in: query
      description: |
        Page size. Missing or empty defaults to 50. Values above 50 are clamped to 50.
        Zero, negative, decimal, and non-numeric values return `400`.
      schema:
        type: integer
        minimum: 1
        maximum: 50
        default: 50
    ActiveOnly:
      name: activeOnly
      in: query
      description: |
        When `true`, only subscriptions with `deleted: false` are returned.
        This does not check expiry or remaining bandwidth. Omitted defaults to `false`.
      schema:
        type: string
        enum: ["true", "false"]
        default: "false"

  schemas:
    AccountSuccessResponse:
      type: object
      required: [success, data]
      properties:
        success:
          type: boolean
          enum: [true]
        data:
          type: object
          required: [account, subscriptions, pagination]
          properties:
            account:
              type: object
              required: [balance]
              properties:
                balance:
                  type: number
                  description: Account balance in USD.
            subscriptions:
              type: array
              items:
                $ref: "#/components/schemas/AccountSubscription"
            pagination:
              $ref: "#/components/schemas/Pagination"

    AccountSubscription:
      type: object
      description: |
        Summary of one subscription. Fields are only present when the underlying
        subscription has the value; treat optional fields as nullable in clients.
      required: [id, active, expiresAt, type, authUser]
      properties:
        id:
          type: string
          description: Subscription ID (MongoDB ObjectId string).
        active:
          type: boolean
          description: "True when the subscription is not deleted. Expiry is not considered."
        expiresAt:
          type: string
          format: date-time
          nullable: true
          description: Mapped from the subscription renewal date.
        type:
          type: string
          description: Customer-facing product line slug, for example `rotating-residential`.
        authUser:
          type: string
          description: Base username for proxy authentication.
        dataLeft:
          type: number
          description: Only present for resellable (rotating) products. Remaining bandwidth in raw bytes (not GB).
        ips:
          type: integer
          description: Only present for static products. Equals the number of proxy list entries.

    Pagination:
      type: object
      required: [page, size, totalItems, totalPages]
      properties:
        page:
          type: integer
        size:
          type: integer
          minimum: 1
          maximum: 50
        totalItems:
          type: integer
        totalPages:
          type: integer
          description: "0 when there are no matching subscriptions."

    ErrorResponse:
      type: object
      required: [success, error]
      properties:
        success:
          type: boolean
          enum: [false]
        error:
          type: string

    ForbiddenResponse:
      type: object
      required: [success, error]
      properties:
        success:
          type: boolean
          enum: [false]
        error:
          type: string
          enum: ["unauthorized", "User email is not verified"]

    ServerErrorResponse:
      type: object
      required: [success, error]
      properties:
        success:
          type: boolean
          enum: [false]
        error:
          type: string
          enum: ["server error"]
