openapi: 3.0.3
info:
  title: Qdial CRM Connector API
  version: 0.2.0-draft
  description: |
    Implement these six endpoints in your CRM and the Qdial Android app can
    pull leads, place calls on the agent's own SIM, and send the call result back.

    **How it fits together**

    1. Your web panel shows the agent a **CRM address** (the base URL of this API)
       and a one-time **6-digit code**.
    2. The agent types both into the app. The app calls `POST /pair` and receives a
       token bound to that phone. No password is stored on the phone.
    3. Every other request carries `Authorization: Bearer <token>`.
    4. After each call dialled from Qdial, the app sends `POST /calls`. If the
       phone is offline the log is kept on the phone and retried, so your endpoint
       must be idempotent on `client_call_id`.

    **Rules**

    - HTTPS only. JSON request and response bodies, UTF-8.
    - Times are ISO 8601 in UTC, for example `2026-11-28T04:35:00Z`.
    - Phone numbers are E.164, for example `+919876543210`.
    - Only calls to leads that came from `GET /leads/next` are reported. Numbers
      typed on the keypad, personal calls and incoming calls are never sent.
    - Qdial has no server of its own. The phone talks directly to your CRM.

    Status: draft, to be frozen with the first CRM implementation.
servers:
  - url: https://yourcrm.example/qdial-api
    description: Your CRM (replace with your own base URL)
security:
  - bearerAuth: []
tags:
  - name: Pairing
  - name: Agent
  - name: Leads
  - name: Calls
paths:
  /pair:
    post:
      tags: [Pairing]
      summary: Exchange a 6-digit code for a device token
      description: |
        The code is created in your web panel for one agent, is valid for a short
        time (suggested 10 minutes) and works once. Return a long random token and
        store only its hash together with `device_id`.
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PairRequest'
      responses:
        '200':
          description: Paired
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PairResponse'
        '400':
          $ref: '#/components/responses/Error'
        '401':
          description: Code is wrong, expired or already used
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorBody'
        '429':
          $ref: '#/components/responses/Error'
  /unpair:
    post:
      tags: [Pairing]
      summary: Revoke this device's token
      responses:
        '200':
          description: Token revoked
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Ok'
        '401':
          $ref: '#/components/responses/Unauthorized'
  /me:
    get:
      tags: [Agent]
      summary: Agent name and today's counts
      responses:
        '200':
          description: Agent profile
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MeResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
  /leads/next:
    get:
      tags: [Leads]
      summary: Next leads for this agent to call
      description: |
        Return the next leads in calling order and **lock** them to this agent for
        a short time (suggested 15 minutes) so two phones never receive the same
        lead. Return an empty list when nothing is left.
      parameters:
        - name: limit
          in: query
          schema:
            type: integer
            minimum: 1
            maximum: 20
            default: 5
      responses:
        '200':
          description: Leads
          content:
            application/json:
              schema:
                type: object
                required: [leads]
                properties:
                  leads:
                    type: array
                    items:
                      $ref: '#/components/schemas/Lead'
        '401':
          $ref: '#/components/responses/Unauthorized'
  /leads/{id}:
    get:
      tags: [Leads]
      summary: One lead in detail
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Lead
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Lead'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/Error'
  /calls:
    post:
      tags: [Calls]
      summary: Report one finished call
      description: |
        Sent once per lead call. For a call that was not answered, was busy or
        failed, `outcome`, `note` and `next_call_at` are null and no sheet is shown
        to the agent. Must be idempotent: if a log with
        the same `client_call_id` already exists, return `200` with
        `duplicate: true` and change nothing.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CallLog'
      responses:
        '200':
          description: Stored (or already stored)
          content:
            application/json:
              schema:
                type: object
                required: [ok]
                properties:
                  ok:
                    type: boolean
                    example: true
                  duplicate:
                    type: boolean
                    example: false
        '400':
          $ref: '#/components/responses/Error'
        '401':
          $ref: '#/components/responses/Unauthorized'
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
  responses:
    Unauthorized:
      description: Token missing, wrong or revoked. The app wipes its pairing and asks for a new code.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorBody'
    Error:
      description: Request could not be processed
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorBody'
  schemas:
    Ok:
      type: object
      properties:
        ok:
          type: boolean
          example: true
    ErrorBody:
      type: object
      required: [error]
      properties:
        error:
          type: object
          required: [code, message]
          properties:
            code:
              type: string
              example: invalid_code
            message:
              type: string
              description: Short text the app can show to the agent
              example: This code is wrong or has expired.
    PairRequest:
      type: object
      required: [code, device_id]
      properties:
        code:
          type: string
          pattern: '^[0-9]{6}$'
          example: '482913'
        device_id:
          type: string
          description: Random id created by the app on first launch. Not a hardware id.
          example: 9f2c1a7b5d3e4f60812a4c6e8b0d2f41
        device_name:
          type: string
          example: Android phone
        app_version:
          type: string
          example: 0.1.0
    PairResponse:
      type: object
      required: [token, agent]
      properties:
        token:
          type: string
          description: Opaque bearer token bound to this device
        agent:
          $ref: '#/components/schemas/Agent'
    Agent:
      type: object
      required: [id, name]
      properties:
        id:
          type: string
          example: '17'
        name:
          type: string
          example: Priya S
    MeResponse:
      type: object
      required: [agent]
      properties:
        agent:
          $ref: '#/components/schemas/Agent'
        crm_name:
          type: string
          example: Your CRM
        today:
          type: object
          properties:
            calls:
              type: integer
              example: 42
            answered:
              type: integer
              example: 19
            talk_seconds:
              type: integer
              example: 3120
        outcomes:
          type: array
          description: |
            Optional. The choices shown to the agent after an answered call. If you
            leave this out the app shows: interested, callback, not_interested,
            wrong_number, other.
          items:
            type: object
            required: [code, label]
            properties:
              code:
                type: string
                example: interested
              label:
                type: string
                example: Interested
    Lead:
      type: object
      required: [id, name, phone]
      properties:
        id:
          type: string
          example: '90211'
        name:
          type: string
          example: Rahul Verma
        phone:
          type: string
          description: E.164
          example: '+919876543210'
        stage:
          type: string
          description: Free text from your CRM, shown as is
          example: Follow-up
        notes:
          type: string
          description: Last note, shown before the call
          example: Asked to call after 4 pm
        last_contact_at:
          type: string
          format: date-time
          nullable: true
    CallLog:
      type: object
      required: [client_call_id, lead_id, phone, started_at, duration_seconds, status]
      properties:
        client_call_id:
          type: string
          description: Unique id made by the phone. Use it to ignore repeats.
          example: 5b0f6c2e-8d1a-4e77-9a53-0c1d2e3f4a5b
        lead_id:
          type: string
          description: The lead's id exactly as you sent it in /leads/next
          example: '90211'
        phone:
          type: string
          example: '+919876543210'
        started_at:
          type: string
          format: date-time
        duration_seconds:
          type: integer
          description: Talk time, measured from answer to hang-up. 0 when not answered.
          example: 96
        status:
          type: string
          enum: [answered, not_answered, busy, failed]
        outcome:
          type: string
          nullable: true
          description: The `code` the agent picked from your outcomes list (or the default list)
          example: interested
        note:
          type: string
          nullable: true
        next_call_at:
          type: string
          format: date-time
          nullable: true
