openapi: 3.0.3
info:
  title: ThunderProxy Subscription API (API-key)
  description: |
    API-key endpoints for working with a single subscription. Each subscription route
    requires an API key that belongs to the subscription owner.

    A valid key for another user receives
    `403 { "success": false, "error": "unauthorized" }`. A missing or unknown key also
    receives that 403.
  version: 1.0.0

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

security:
  - SubscriptionApiKey: []

paths:
  /api/subscriptions/{subscriptionId}/settings:
    patch:
      operationId: updateSubscriptionSettingsPatch
      summary: Update saved API-export settings
      description: |
        Writes settings to the `apiExport` surface of the subscription. The export endpoint
        reads them. Send the settings as a JSON body. Returns a JSON success envelope.

        `sessions` is an integer from 1 to 20000. Values outside that range or non-integers
        return `400`. Only resellable (rotating) product types are accepted. Other types return `400`.

        Invalid input returns a JSON `400` and does not change saved settings.
      tags:
        - Subscriptions
      parameters:
        - $ref: "#/components/parameters/SubscriptionId"
      requestBody:
        required: false
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/SubscriptionSettingsInput"
            example:
              country: US
              rotation: sticky
              sessions: 10
      responses:
        "200":
          description: Settings saved.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SettingsUpdateSuccess"
        "400":
          description: Validation failed or a referenced location is invalid. JSON error envelope.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "403":
          $ref: "#/components/responses/Forbidden"

    get:
      deprecated: true
      operationId: updateSubscriptionSettings
      summary: "Deprecated: update API-export settings with query parameters"
      description: |
        **Deprecated.** Use `PATCH` with a JSON body. This method changes data but is
        a `GET`, so it can be cached, logged, or replayed by proxies and browsers.
        It is kept only for existing integrations. It has the same checks as `PATCH`.
        Responses include `Deprecation: true` and a `Link` header that points to `PATCH`.

        The same query parameters apply: `country`, `state`, `city`, `rotation`,
        `sessions`, and `protocol`. Invalid input returns a JSON `400` and does not change
        saved settings.
      tags:
        - Subscriptions
      parameters:
        - $ref: "#/components/parameters/SubscriptionId"
        - name: country
          in: query
          required: false
          description: Country code, or `ANY` to remove geotargeting. Residential `US` is stored as `ROW`.
          schema:
            type: string
        - name: state
          in: query
          required: false
          description: State code. Requires a country. Cannot be combined with `ANY`.
          schema:
            type: string
            nullable: true
        - name: city
          in: query
          required: false
          description: City name. Requires a state.
          schema:
            type: string
            nullable: true
        - name: rotation
          in: query
          required: false
          schema:
            type: string
            enum: [sticky, rotating]
        - name: sessions
          in: query
          required: false
          description: "Sticky session count, integer from 1 to 20000."
          schema:
            type: integer
            minimum: 1
            maximum: 20000
        - name: protocol
          in: query
          required: false
          schema:
            type: string
            enum: [http, socks5]
      responses:
        "200":
          description: "Settings saved. Includes the header `Deprecation: true`."
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SettingsUpdateSuccess"
        "400":
          description: Validation failed or a referenced location is invalid. JSON error envelope.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "403":
          $ref: "#/components/responses/Forbidden"

  /api/subscriptions/{subscriptionId}/locations/countries:
    get:
      operationId: getSubscriptionGeotargetingCountries
      summary: List available countries for a subscription
      description: |
        Returns active country options for the subscription's product. Each option contains
        the exact `connectionString` value to append to a proxy username and a display label.
        A valid subscription with no available countries returns an empty array.
      tags:
        - Subscriptions
      parameters:
        - $ref: "#/components/parameters/SubscriptionId"
      responses:
        "200":
          description: Country options.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/LocationListSuccess"
        "400":
          $ref: "#/components/responses/BadRequest"
        "404":
          $ref: "#/components/responses/NotFound"
        "403":
          $ref: "#/components/responses/Forbidden"

  /api/subscriptions/{subscriptionId}/locations/states:
    get:
      operationId: getSubscriptionGeotargetingStates
      summary: List available states for a country
      description: |
        Returns active state options for the requested country and subscription product.
        A country with no states returns an empty array.
      tags:
        - Subscriptions
      parameters:
        - $ref: "#/components/parameters/SubscriptionId"
        - name: country
          in: query
          required: true
          schema:
            type: string
      responses:
        "200":
          description: State options.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/LocationListSuccess"
        "400":
          $ref: "#/components/responses/BadRequest"
        "404":
          $ref: "#/components/responses/NotFound"
        "403":
          $ref: "#/components/responses/Forbidden"

  /api/subscriptions/{subscriptionId}/locations/cities:
    get:
      operationId: getSubscriptionGeotargetingCities
      summary: List available cities for a state
      description: |
        Returns active city options for the requested country, state, and subscription product.
        A valid state with no cities returns an empty array.
      tags:
        - Subscriptions
      parameters:
        - $ref: "#/components/parameters/SubscriptionId"
        - name: country
          in: query
          required: true
          schema:
            type: string
        - name: state
          in: query
          required: true
          schema:
            type: string
      responses:
        "200":
          description: City options.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/LocationListSuccess"
        "400":
          $ref: "#/components/responses/BadRequest"
        "404":
          $ref: "#/components/responses/NotFound"
        "403":
          $ref: "#/components/responses/Forbidden"

  /api/subscriptions/{subscriptionId}/city-combinations:
    get:
      operationId: getResidentialSubscriptionCityCombinations
      summary: Download city combination usernames
      description: |
        Returns a text file with one proxy line per generated combination, with
        `Content-Disposition: attachment; filename="city_combinations.txt"`.

        `amount` is parsed with `parseInt`: missing, non-numeric, or non-positive values
        default to `2`. Values above `5` return `400`. The amount is rows per city
        combination, not a total row count.

        Only resellable (rotating) subscriptions are accepted. A generation failure returns
        a JSON `500` error. It never returns an empty `200`.
      tags:
        - Subscriptions
      parameters:
        - $ref: "#/components/parameters/SubscriptionId"
        - name: amount
          in: query
          required: false
          description: Rows per city combination. Defaults to 2. Maximum 5.
          schema:
            type: integer
            default: 2
            maximum: 5
      responses:
        "200":
          description: Text file, one line per generated proxy entry.
          headers:
            Content-Disposition:
              schema:
                type: string
                example: attachment; filename="city_combinations.txt"
          content:
            text/plain:
              schema:
                type: string
        "400":
          description: |
            JSON error: `Amount must be less than or equal to 5`,
            `No subscription found`, or `Only rotating subscriptions are supported`.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "403":
          $ref: "#/components/responses/Forbidden"
        "500":
          description: Generation failed. JSON error. Server details are logged without credentials.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"

  /public/subscriptions/export/{subscriptionId}/txt:
    get:
      operationId: getSubscriptionTxtOutput
      summary: Export proxy list
      description: |
        Returns the current generated proxy list as newline-separated text, using the saved
        `apiExport` settings. The export takes no location, rotation, or protocol query
        parameters; those are ignored.

        The response is `text/plain` with `\n` line endings and no attachment header.
      tags:
        - Export
      parameters:
        - $ref: "#/components/parameters/SubscriptionId"
      responses:
        "200":
          description: Newline-separated proxy lines.
          content:
            text/plain:
              schema:
                type: string
                example: |-
                  gateway.thunderproxy.com:12345:username-res-US-sid-12345678:password
        "400":
          description: JSON error for a missing subscription.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "403":
          $ref: "#/components/responses/Forbidden"

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.

  parameters:
    SubscriptionId:
      name: subscriptionId
      in: path
      required: true
      description: Subscription ID (MongoDB ObjectId string). Must belong to the API-key owner (see info description).
      schema:
        type: string

  responses:
    Forbidden:
      description: |
        Missing, invalid, or banned API key, email not verified, or an API key that does
        not own the subscription (non-owners receive `error: "unauthorized"`).
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    BadRequest:
      description: Invalid or unavailable location filter.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    NotFound:
      description: Subscription not found.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"

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

    SettingsUpdateSuccess:
      type: object
      required: [success, data]
      properties:
        success:
          type: boolean
          enum: [true]
        data:
          type: object
          required: [updated]
          properties:
            updated:
              type: boolean
              enum: [true]

    LocationListSuccess:
      type: object
      required: [success, data]
      properties:
        success:
          type: boolean
          enum: [true]
        data:
          type: array
          items:
            $ref: "#/components/schemas/LocationOption"

    LocationOption:
      type: object
      required: [connectionString, displayName, country]
      properties:
        connectionString:
          type: string
          description: Exact location component to append to the proxy username.
        displayName:
          type: string
          description: Human-readable location label.
        country:
          type: string
        state:
          type: string
        city:
          type: string

    SubscriptionSettingsInput:
      type: object
      description: Settings to write. All fields are optional. Unknown fields are ignored.
      properties:
        country:
          type: string
          description: Country code, or `ANY` to remove geotargeting. Residential `US` is stored as `ROW`.
        state:
          type: string
          nullable: true
          description: State code. Requires a country. Cannot be combined with `ANY`.
        city:
          type: string
          nullable: true
          description: City name. Requires a state.
        rotation:
          type: string
          enum: [sticky, rotating]
        sessions:
          type: integer
          minimum: 1
          maximum: 20000
          description: Sticky session count.
        protocol:
          type: string
          enum: [http, socks5]
