> ## Documentation Index
> Fetch the complete documentation index at: https://docs.d-api.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# On-demand history sweep

> For each chat (the ones in `chats`, or the `maxChats` with the most recent message), asks the phone for the messages older than the oldest one already stored, page by page, until the phone returns nothing. Each chat's anchor is its oldest stored message: chats with no stored message are left out. Runs in the background: the response only confirms the start and returns the request's `requestId`. The messages arrive as `history.message.*` events on RabbitMQ; with `webhookUrl` or `webhook: true`, the new messages also arrive at the webhook, in the `history.on_demand.messages` event. The cap per chat is `maxPagesPerChat` × `count`. The phone must be online, and its answer is best-effort. `requestFull` also asks for a FULL_HISTORY_SYNC_ON_DEMAND, accepted only on WhatsApp Business; on personal WhatsApp, the full history from pairing cannot be repeated without pairing again. `cancel` stops the sweep in progress. History media is not stored.



## OpenAPI

````yaml /api-reference/openapi.en.json post /api/v1/history/full-sync
openapi: 3.1.0
info:
  title: D-API - Complete WhatsApp Infrastructure
  description: >-
    A scalable, robust solution for automation managers, SaaS products and end
    users who need a reliable WhatsApp integration. It offers multiple
    simultaneous instances, real-time webhooks, an intuitive dashboard and a
    documented REST API with 99.9% uptime and latency under 30ms.
  version: 1.2.1
  contact:
    name: Suporte D-API
    email: suporte@d-api.cloud
    url: https://d-api.cloud
servers:
  - url: https://api.d-api.cloud
    description: Production
security:
  - ApiKeyAuth: []
tags:
  - name: Home
    description: Base endpoints
  - name: Sessions
    description: Endpoints for managing sessions
  - name: Messages
    description: Endpoints for sending messages
  - name: Cloud API
    description: >-
      WhatsApp Official API (Meta Cloud API): manual connection, template
      management (list/create/delete) and template sending.
  - name: IVR Calls
    description: 'Voice call dispatch (IVR): single or bulk, with audio or an IVR flow.'
  - name: SMS
    description: 'SMS sending: single or bulk.'
  - name: Commands
    description: >-
      Look up asynchronous commands (async: true) by the commandId returned when
      enqueued.
paths:
  /api/v1/history/full-sync:
    post:
      tags:
        - History
      summary: On-demand history sweep
      description: >-
        For each chat (the ones in `chats`, or the `maxChats` with the most
        recent message), asks the phone for the messages older than the oldest
        one already stored, page by page, until the phone returns nothing. Each
        chat's anchor is its oldest stored message: chats with no stored message
        are left out. Runs in the background: the response only confirms the
        start and returns the request's `requestId`. The messages arrive as
        `history.message.*` events on RabbitMQ; with `webhookUrl` or `webhook:
        true`, the new messages also arrive at the webhook, in the
        `history.on_demand.messages` event. The cap per chat is
        `maxPagesPerChat` × `count`. The phone must be online, and its answer is
        best-effort. `requestFull` also asks for a FULL_HISTORY_SYNC_ON_DEMAND,
        accepted only on WhatsApp Business; on personal WhatsApp, the full
        history from pairing cannot be repeated without pairing again. `cancel`
        stops the sweep in progress. History media is not stored.
      operationId: postApiV1HistoryFull-sync
      parameters:
        - schema:
            anyOf:
              - type: boolean
              - type: string
          in: query
          name: async
          required: false
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                sessionId:
                  minLength: 1
                  type: string
                  description: Session id.
                chats:
                  type: array
                  maxItems: 1000
                  items:
                    minLength: 1
                    type: string
                  description: >-
                    JIDs of the chats to sweep, e.g.
                    `5511999999999@s.whatsapp.net` or `120363000000000000@g.us`.
                    Without `chats`, sweeps the `maxChats` chats with the most
                    recent message.
                maxChats:
                  type: integer
                  minimum: 1
                  maximum: 1000
                  default: 200
                  description: >-
                    How many chats to sweep (also caps `chats`). Default 200,
                    maximum 1000.
                count:
                  type: integer
                  minimum: 1
                  maximum: 100
                  default: 50
                  description: Messages requested per page. Default 50.
                maxPagesPerChat:
                  type: integer
                  minimum: 1
                  maximum: 200
                  default: 20
                  description: >-
                    Maximum pages per chat. Default 20. A chat also stops
                    earlier, when the phone returns nothing.
                intervalMs:
                  type: integer
                  minimum: 1000
                  maximum: 60000
                  default: 3000
                  description: >-
                    Interval between requests to the phone, in milliseconds.
                    Default 3000.
                requestFull:
                  type: boolean
                  default: false
                  description: >-
                    Also asks for a FULL_HISTORY_SYNC_ON_DEMAND. Only WhatsApp
                    Business accepts it; personal WhatsApp answers
                    ERROR_REQUEST_ON_NON_SMB_PRIMARY.
                days:
                  type: integer
                  minimum: 1
                  maximum: 3650
                  default: 3650
                  description: >-
                    How many days of history to ask for with `requestFull`.
                    Default 3650.
                cancel:
                  type: boolean
                  default: false
                  description: Stops the sweep in progress. The other fields are ignored.
                webhook:
                  type: boolean
                  default: false
                  description: >-
                    Delivers the new messages fetched by the sweep in the
                    `history.on_demand.messages` event, in batches of up to 100
                    per chat, to the URL that receives `messages.received` in
                    the session. Without that URL and without `webhookUrl`, the
                    route answers 400.
                webhookUrl:
                  type: string
                  format: uri
                  description: >-
                    URL that receives the `history.on_demand.messages` event for
                    this request. It already turns delivery on, with no need for
                    `webhook: true`. Must be http or https and point to a public
                    address.
              required:
                - sessionId
            example:
              sessionId: minha-sessao
              maxChats: 50
              count: 50
              maxPagesPerChat: 10
              intervalMs: 3000
          multipart/form-data:
            schema:
              type: object
              properties:
                sessionId:
                  minLength: 1
                  type: string
                  description: Session id.
                chats:
                  type: array
                  maxItems: 1000
                  items:
                    minLength: 1
                    type: string
                  description: >-
                    JIDs of the chats to sweep, e.g.
                    `5511999999999@s.whatsapp.net` or `120363000000000000@g.us`.
                    Without `chats`, sweeps the `maxChats` chats with the most
                    recent message.
                maxChats:
                  type: integer
                  minimum: 1
                  maximum: 1000
                  default: 200
                  description: >-
                    How many chats to sweep (also caps `chats`). Default 200,
                    maximum 1000.
                count:
                  type: integer
                  minimum: 1
                  maximum: 100
                  default: 50
                  description: Messages requested per page. Default 50.
                maxPagesPerChat:
                  type: integer
                  minimum: 1
                  maximum: 200
                  default: 20
                  description: >-
                    Maximum pages per chat. Default 20. A chat also stops
                    earlier, when the phone returns nothing.
                intervalMs:
                  type: integer
                  minimum: 1000
                  maximum: 60000
                  default: 3000
                  description: >-
                    Interval between requests to the phone, in milliseconds.
                    Default 3000.
                requestFull:
                  type: boolean
                  default: false
                  description: >-
                    Also asks for a FULL_HISTORY_SYNC_ON_DEMAND. Only WhatsApp
                    Business accepts it; personal WhatsApp answers
                    ERROR_REQUEST_ON_NON_SMB_PRIMARY.
                days:
                  type: integer
                  minimum: 1
                  maximum: 3650
                  default: 3650
                  description: >-
                    How many days of history to ask for with `requestFull`.
                    Default 3650.
                cancel:
                  type: boolean
                  default: false
                  description: Stops the sweep in progress. The other fields are ignored.
                webhook:
                  type: boolean
                  default: false
                  description: >-
                    Delivers the new messages fetched by the sweep in the
                    `history.on_demand.messages` event, in batches of up to 100
                    per chat, to the URL that receives `messages.received` in
                    the session. Without that URL and without `webhookUrl`, the
                    route answers 400.
                webhookUrl:
                  type: string
                  format: uri
                  description: >-
                    URL that receives the `history.on_demand.messages` event for
                    this request. It already turns delivery on, with no need for
                    `webhook: true`. Must be http or https and point to a public
                    address.
              required:
                - sessionId
          text/plain:
            schema:
              type: object
              properties:
                sessionId:
                  minLength: 1
                  type: string
                  description: Session id.
                chats:
                  type: array
                  maxItems: 1000
                  items:
                    minLength: 1
                    type: string
                  description: >-
                    JIDs of the chats to sweep, e.g.
                    `5511999999999@s.whatsapp.net` or `120363000000000000@g.us`.
                    Without `chats`, sweeps the `maxChats` chats with the most
                    recent message.
                maxChats:
                  type: integer
                  minimum: 1
                  maximum: 1000
                  default: 200
                  description: >-
                    How many chats to sweep (also caps `chats`). Default 200,
                    maximum 1000.
                count:
                  type: integer
                  minimum: 1
                  maximum: 100
                  default: 50
                  description: Messages requested per page. Default 50.
                maxPagesPerChat:
                  type: integer
                  minimum: 1
                  maximum: 200
                  default: 20
                  description: >-
                    Maximum pages per chat. Default 20. A chat also stops
                    earlier, when the phone returns nothing.
                intervalMs:
                  type: integer
                  minimum: 1000
                  maximum: 60000
                  default: 3000
                  description: >-
                    Interval between requests to the phone, in milliseconds.
                    Default 3000.
                requestFull:
                  type: boolean
                  default: false
                  description: >-
                    Also asks for a FULL_HISTORY_SYNC_ON_DEMAND. Only WhatsApp
                    Business accepts it; personal WhatsApp answers
                    ERROR_REQUEST_ON_NON_SMB_PRIMARY.
                days:
                  type: integer
                  minimum: 1
                  maximum: 3650
                  default: 3650
                  description: >-
                    How many days of history to ask for with `requestFull`.
                    Default 3650.
                cancel:
                  type: boolean
                  default: false
                  description: Stops the sweep in progress. The other fields are ignored.
                webhook:
                  type: boolean
                  default: false
                  description: >-
                    Delivers the new messages fetched by the sweep in the
                    `history.on_demand.messages` event, in batches of up to 100
                    per chat, to the URL that receives `messages.received` in
                    the session. Without that URL and without `webhookUrl`, the
                    route answers 400.
                webhookUrl:
                  type: string
                  format: uri
                  description: >-
                    URL that receives the `history.on_demand.messages` event for
                    this request. It already turns delivery on, with no need for
                    `webhook: true`. Must be http or https and point to a public
                    address.
              required:
                - sessionId
      responses:
        '200':
          description: Sweep started (or cancelled, with `cancel`)
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                  commandId:
                    type: string
                  requestId:
                    type: string
                    description: >-
                      Request id. It comes in every batch of the
                      `history.on_demand.messages` event (`data.requestId`), to
                      match the messages to this call. Not returned with
                      `cancel`.
                  sessionId:
                    type: string
                  data:
                    type: object
                    properties:
                      status:
                        type: string
                        enum:
                          - started
                          - cancelled
                          - not_running
                        description: >-
                          `started`: sweep started. `cancelled`: the sweep in
                          progress was stopped. `not_running`: there was no
                          sweep to cancel.
                      chats:
                        type: integer
                        description: Chats included in the sweep.
                      count:
                        type: integer
                        description: Effective messages per page.
                      max_pages_per_chat:
                        type: integer
                        description: Effective pages per chat.
                      interval_ms:
                        type: integer
                        description: Effective interval between requests.
                      full_request_id:
                        type: string
                        description: Id of the FULL request, when `requestFull` was sent.
                  timestamp:
                    type: string
                required:
                  - success
                  - sessionId
              example:
                success: true
                commandId: 8f14e45f-ceea-467a-9b1c-2f4d9d6a1c10
                requestId: 3f6c1d2e-9a4b-4c8d-b7e1-5a2f0c9d8e71
                sessionId: minha-sessao
                data:
                  status: started
                  chats: 50
                  count: 50
                  max_pages_per_chat: 10
                  interval_ms: 3000
                timestamp: '2026-12-01T14:00:00.000Z'
        '400':
          description: >-
            No chat with a stored message to anchor the request, or an invalid
            parameter
          content:
            application/json:
              example:
                success: false
                error: No stored messages to anchor the history request
                sessionId: minha-sessao
        '404':
          description: Session not found
          content:
            application/json:
              example:
                success: false
                error: Session not found
                sessionId: minha-sessao
        '409':
          description: >-
            A sweep is already running (send `cancel: true` to stop it) or the
            session is not connected
          content:
            application/json:
              example:
                success: false
                commandId: 8f14e45f-ceea-467a-9b1c-2f4d9d6a1c10
                error: >-
                  a history full sync is already running for this session; send
                  cancel=true to stop it
                sessionId: minha-sessao
        '502':
          description: The session failed to run the request
          content:
            application/json:
              example:
                success: false
                commandId: 8f14e45f-ceea-467a-9b1c-2f4d9d6a1c10
                error: full history request failed
                sessionId: minha-sessao
        '504':
          description: The session did not answer in time
          content:
            application/json:
              example:
                success: false
                commandId: 8f14e45f-ceea-467a-9b1c-2f4d9d6a1c10
                error: Command timeout - no response received within 30 seconds
                sessionId: minha-sessao
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: 'API Key for authentication. Format: <API_KEY>'

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.