openapi: 3.1.0
info:
  title: Affinix casino integration API
  version: 1.0.0
  description: |
    How a casino or sportsbook sends players and activity to Affinix. Each brand has its own base URL
    on its tracking domain and its own API key. Guide: https://affinix360.com/docs

    The aff code: affiliate links redirect to your site with `?aff=<code>`. Store it unchanged and send it
    back with the player and every activity row. It may be URL-encoded once or twice; both are accepted.
    Format (only needed to read it yourself): base64( base64(iv) "." base64(base64(ciphertext)) "." base64(tag) ),
    AES-128-GCM, key = first 16 bytes of SHA-256(brand token key), 12-byte IV, 16-byte tag, plaintext JSON
    {"campaignId":int,"channelId":int,"assetId":int,"aff":int,"linkId":int,"clickId":string,"v":2}.
  contact:
    email: hello@affinix360.com
servers:
  - url: https://{trackingDomain}/api/v1
    variables:
      trackingDomain: { default: go.yourbrand.com, description: The brand's tracking domain }
security:
  - apiKey: []
paths:
  /players:
    post:
      summary: A player registered through an affiliate link
      operationId: createPlayer
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/PlayersRequest' }
      responses:
        '202':
          description: Created, already known, or an identical payload was already received.
          content:
            application/json:
              schema:
                type: object
                properties:
                  status: { const: ok }
                  player: { enum: [created, existing] }
                  duplicate: { type: boolean }
        '400': { $ref: '#/components/responses/BadRequest' }
        '401': { $ref: '#/components/responses/Unauthorized' }
  /activity:
    post:
      summary: Player activity for one hour
      operationId: sendActivity
      description: Stored per player and UTC hour (the hour containing `from`). Resending an hour replaces it.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/ActivityRequest' }
      responses:
        '202':
          description: Stored, or an identical payload was already received.
          content:
            application/json:
              schema:
                type: object
                properties:
                  status: { const: ok }
                  accepted: { type: integer }
                  skipped: { type: integer, description: Rows whose aff code could not be read or whose affiliate no longer exists }
                  duplicate: { type: boolean }
        '400': { $ref: '#/components/responses/BadRequest' }
        '401': { $ref: '#/components/responses/Unauthorized' }
components:
  securitySchemes:
    apiKey: { type: apiKey, in: header, name: affinix-api-key }
  responses:
    BadRequest:
      description: Invalid payload, unreadable aff token or unknown affiliate.
      content:
        application/json:
          schema:
            type: object
            properties:
              error: { type: string, examples: ['Invalid payload', 'unreadable aff token', 'unknown affiliate'] }
              issues: { type: array, items: { type: object } }
    Unauthorized:
      description: Missing or wrong affinix-api-key.
      content:
        application/json:
          schema: { type: object, properties: { error: { const: Unauthorized } } }
  schemas:
    UserId:
      oneOf:
        - { type: integer }
        - { type: string, pattern: '^[A-Za-z0-9_-]{1,64}$' }
    Amount:
      oneOf:
        - { type: number }
        - { type: string, pattern: '^-?\d+(\.\d+)?$' }
      default: 0
    PlayersRequest:
      type: object
      required: [players]
      properties:
        players:
          type: object
          required: [aff, user_id]
          properties:
            aff: { type: string, minLength: 16, maxLength: 2048 }
            user_id: { $ref: '#/components/schemas/UserId' }
            email: { type: [string, 'null'], format: email, maxLength: 254 }
            first_name: { type: [string, 'null'], maxLength: 120 }
            last_name: { type: [string, 'null'], maxLength: 120 }
            country: { type: [string, 'null'], description: ISO 3166-1 alpha-2 }
            created_at: { type: [string, 'null'], description: 'UTC, "YYYY-MM-DD HH:MM:SS" or ISO 8601', examples: ['2026-11-29 10:14:00'] }
    ActivityRequest:
      type: object
      required: [from, to, items]
      properties:
        from: { type: string, format: date-time }
        to: { type: string, format: date-time }
        items:
          type: array
          maxItems: 50000
          items: { $ref: '#/components/schemas/ActivityItem' }
    ActivityItem:
      type: object
      required: [aff, user_id, currency]
      additionalProperties: true
      properties:
        aff: { type: string, maxLength: 2048 }
        user_id: { $ref: '#/components/schemas/UserId' }
        currency: { type: string, pattern: '^[A-Z]{3}$' }
        casino_bets_sum: { $ref: '#/components/schemas/Amount' }
        casino_wins_sum: { $ref: '#/components/schemas/Amount' }
        sport_bets_sum: { $ref: '#/components/schemas/Amount' }
        sport_wins_sum: { $ref: '#/components/schemas/Amount' }
        deposits_sum: { $ref: '#/components/schemas/Amount' }
        deposits_count: { $ref: '#/components/schemas/Amount' }
        ftds_count: { $ref: '#/components/schemas/Amount' }
        ftds_sum: { $ref: '#/components/schemas/Amount' }
        withdrawals_sum: { $ref: '#/components/schemas/Amount' }
        withdrawals_count: { $ref: '#/components/schemas/Amount' }
        bonuses_sum: { $ref: '#/components/schemas/Amount' }
