Skip to main content
A connection’s history has a main path and a complement:
  1. Traditional path. When connecting, the phone sends the history and D-API stores it. Then you call POST /api/v1/history/sync to receive those messages at your webhook.
  2. Complement: on demand. If something is missing (chats with few messages, older periods), you ask the phone for the messages before the ones D-API already has, chat by chat.
Everything here applies only to unofficial connections. The Official API (Cloud API) has no device history.

Which path to use

1

Create the session with historySync: true

The option applies at creation, before pairing.
2

Connect (QR or code)

The phone sends the history in phases (INITIAL_BOOTSTRAP, RECENT, FULL) and D-API stores it.
3

Call POST /api/v1/history/sync

What was stored reaches your webhook as messages.received.
4

History missing? Use on demand

POST /api/v1/history/full-sync with webhookUrl or webhook: true: the new messages arrive in history.on_demand.messages. If nothing is missing, carry on with the live events.

Traditional path

1. Connect with history turned on

Pairing history only comes if the session is created with historySync: true (the default is false). The option applies at creation, before pairing: see Create a session.
When pairing, D-API asks WhatsApp for the largest history possible. The phone answers in phases, in this order, and D-API stores each one: You can follow the phases through the optional history_sync.progress event and the history_sync summary, both described in History and sync. Wait for the FULL phase to reach 100% before the next step.

Pairing limits

  • WhatsApp decides how much it sends. On import, the phone sends only the most recent chats (in one observed case, 400). Chats that have been idle for a long time may not arrive.
  • The device is the ceiling. Only what the phone still stores comes back. A message deleted from the phone, or one it never downloaded, doesn’t come back through any path.
  • History media is not stored. Messages arrive with text and metadata, without the file.
  • History doesn’t reach the webhook on its own. The history.message.* events are suppressed in the webhook to avoid a flood of calls (see History and sync). To receive it, use step 2.
On personal WhatsApp, the full pairing history can’t be requested again without pairing again. To go beyond it, use on-demand history.

2. Receive the history at the webhook (/history/sync)

POST /api/v1/history/sync resends to your webhook the messages D-API has already stored for the session, one call per message, in the same messages.received event as live messages.
string
required
Session id.
number
Only messages from this moment on. Unix in seconds or milliseconds.
number
Only messages up to this moment. Unix in seconds or milliseconds.
boolean
true for groups only, false for 1:1 chats only. Without it, both.
string
http or https URL that receives the messages. Without it, D-API uses the session’s webhook: in per-event mode, the messages.received URL; in single mode, the configured URL. Required if the session has no webhook.
integer
default:"100"
Maximum calls per second to your webhook, from 1 to 500. The limit applies to all of the account’s resends to the same destination combined.
The response confirms the scheduling and tells how many messages will go out:
  • total is the number of messages matching the filter. With total: 0, nothing is sent and syncing is false.
  • Only one resend runs per session. If one is already running, the response returns its syncJobId and a message with the progress, without opening another.
Track progress with GET /api/v1/history/sync/{sessionId}/status:
status goes from pending to processing and ends in completed or failed.

How messages arrive

  • One call per message, in the messages.received event, with the same data as the live event (id, from, to, type, fromMe, message, timestamp in milliseconds…). The User-Agent is D-API-History-Sync/1.0.
  • Newest to oldest, in pages of 1,000 messages.
  • No resend marker. The payload is the same as live: use the message id so you don’t store twice what you already received.
  • Media only goes when the message has a stored media_url. Pairing messages arrive without media.
  • Protocol messages and empty text messages are left out.
  • Pace: calls are spaced to respect delivery.maxRps. To estimate the duration, divide total by maxRps (18,172 ÷ 50 ≈ 6 min).
  • Retry: answer 2xx within 30 seconds. Without 2xx, the message is resent in up to 3 attempts, with exponential backoff starting at 2 s.
  • Slow webhook: if most calls take longer than 20 seconds, the resend is stopped and its status becomes failed. Speed up your endpoint (answer before processing) and fire it again.
Resend by period (where.messageTimestamp) on large accounts: it’s easier to track and to repeat only the part that failed.

Complement: on-demand history

Use it when the traditional path didn’t bring everything. On-demand asks the phone for the messages older than the oldest one D-API already has in a chat, and can deliver the new messages straight to your webhook (history.on_demand.messages).

How it works

Each request works like this:
  1. D-API takes the oldest stored message of the chat and uses it as the anchor.
  2. It asks the phone for a block of messages before it (WhatsApp recommends 50 per request; D-API accepts up to 100).
  3. The phone answers asynchronously, usually in about 2 seconds. The messages go through the same path as pairing (history.message.* events) and are stored.
  4. The oldest message of the block becomes the new anchor, and the next request goes back one more block.
This repeats until the phone answers empty, which means there is no more history for that chat.
  • The phone must be online. The request goes to the device itself, and the answer is its own: there is no guarantee it comes, or that it comes complete.
  • The anchor comes from the database. A chat with no stored message is left out: on-demand only extends chats that already exist in D-API.
D-API does this paging for you with the history sweep (POST /api/v1/history/full-sync, under History in the API reference), for one chat or for many.

One chat

Pass the chat JID in chats. The sweep goes back page by page until it comes back empty or until maxPagesPerChat.
Groups use the group JID, for example 120363000000000000@g.us. Chats the phone indexes by LID are resolved automatically: if the phone-number form comes back empty, D-API tries the other one.

Full sweep (full-sync)

Without chats, the sweep goes through the chats with the most recent message first, up to maxChats.
The response only confirms the start. The sweep runs in the background:
data.chats tells how many chats entered the sweep. It can be lower than requested: chats with no stored message are left out. The requestId identifies the request and comes back in every batch delivered to the webhook.

Parameters

string
required
Session id.
string[]
JIDs of the chats to sweep. Without it, the maxChats chats with the most recent message are used. With it, the processing order is not guaranteed.
integer
default:"200"
How many chats to sweep, from 1 to 1000. Also caps chats.
integer
default:"50"
Messages requested per page, from 1 to 100.
integer
default:"20"
Pages per chat, from 1 to 200. The chat stops earlier if the phone answers empty.
integer
default:"3000"
Interval between requests to the phone, from 1000 to 60000 ms.
boolean
default:"false"
Also requests the full history again. WhatsApp Business only. See the requestFull section below.
integer
default:"3650"
Days of history requested in requestFull, from 1 to 3650.
boolean
default:"false"
Stops the running sweep. The other fields are ignored.
boolean
default:"false"
Delivers the new messages to the session’s webhook (the messages.received URL), in the history.on_demand.messages event. See Receive the messages through the webhook.
string
URL that receives this request’s messages. Passing webhookUrl already turns delivery on, with no need for webhook: true. It must be http or https and point to a public address.

Ceiling per chat

Each chat brings at most maxPagesPerChat × count messages beyond what was already stored. With the defaults, 20 × 50 = 1,000 messages. To go further in a chat, raise maxPagesPerChat (up to 200 × 100 = 20,000) or run the sweep again: it restarts from the new oldest message.

Estimated time

Each page waits intervalMs and then the phone’s answer (about 2 s). In the worst case, when no chat ends before the ceiling:
In practice it finishes sooner: most chats answer empty well before the page ceiling.

Track and cancel

  • Result: follow the stored messages per chat (see How to check the result). On RabbitMQ, messages arrive through the history.message.* events as each page comes back.
  • One at a time: only one sweep runs per session. While it runs, a new call answers 409.
  • Cancel: send cancel: true. The response has data.status cancelled (a sweep was running and stopped) or not_running (none was running).
The route accepts ?async=true. The response comes back right away as a queued command, and the start result arrives through the command.result webhook. See Async commands.

requestFull: full history again

requestFull: true sends, in addition to the sweep, a FULL_HISTORY_SYNC_ON_DEMAND request: the phone resends the full history, as at pairing.
  • WhatsApp Business only. On personal WhatsApp, the phone refuses with ERROR_REQUEST_ON_NON_SMB_PRIMARY and nothing arrives. The API still answers 200, because the request was sent: the refusal comes later, from the device.
  • When accepted, the volume is large: a whole FULL arrives again, with many messages that were already stored.
The response has data.full_request_id with the request id. requestFull works even with no stored chat to serve as an anchor.

Best practices and limits

  • Keep the pace. The default of 3 s between requests is safe. Short intervals don’t speed up the phone and raise the chance it stops answering.
  • Don’t fire in bulk. Run one sweep per connection and wait for it to finish. Avoid firing it on every connection of the account at once.
  • Large connections, in rounds. Start with the chats that matter (with chats) or a small maxChats, check the result, then widen. Each round restarts from the oldest stored message, so running again doesn’t repeat what already came.
  • An empty answer is the end. Empty means the phone has nothing before the anchor (or didn’t find the anchor). Asking again doesn’t change the result.
  • On-demand doesn’t discover chats. It only goes back in time in chats D-API already has. A chat that never came in the import can’t be fetched by it.
  • An offline phone stops everything. After 3 requests in a row with no answer, the whole sweep stops, so requests don’t pile up on the device.

How to check the result

  • Dashboard: open the chat in the dashboard and scroll to the beginning. Fetched messages show up in chronological order.
  • API: list the chat’s messages in ascending order and look at the oldest:
Compare it with the oldest before the sweep. If it didn’t change, the phone answered empty for that chat.

Receive the fetched messages

History messages arrive through RabbitMQ, in the history.message.* events. In the webhook they are suppressed, with two exceptions:
  • In the sweep, send webhookUrl or webhook: true to receive the new messages at the webhook. See the next section.
  • After the sweep, POST /api/v1/history/sync also resends what was stored, as in the traditional path.

Receive the messages through the webhook

With webhookUrl or webhook: true, each page the phone returns becomes one or more POSTs to your webhook, in the history.on_demand.messages event.

Where it goes

webhookUrl must be http or https and point to a public address: private or reserved network addresses are refused. It is checked again before each delivery.
history.on_demand.messages is not a subscribable event: it doesn’t show up in the webhook configuration and can’t be turned on there. It is only sent for requests made with webhookUrl or webhook: true, and only for that request.

What arrives

Only new messages, the ones D-API didn’t have yet. Messages the phone returns that were already stored are not resent. Each POST carries a batch of up to 100 messages from one chat:
string
required
The requestId returned by the sweep call. Use it to match the batch to the request.
string
required
Batch id. The same batch always has the same batchId.
string
required
Id of the phone’s answer (the page) that produced the batch. A large page can become more than one batch.
string
required
JID of the batch’s chat.
number
required
Number of messages in messages.
object[]
required
The messages, each in the same format as the data of messages.received: id, from and to, type, fromMe, message, timestamp in milliseconds and the other fields.
In from and to, jid is the phone number (@s.whatsapp.net), name comes from the contact, and lid shows up only when it is known, as in the live event:
  • 1:1 chat: the contact is in from for received messages and in to for messages sent from the phone, with their lid. The connection’s number is on the other side, without lid.
  • Group: to is the group (a jid ending in @g.us and the group name). When the sender’s phone number can’t be found, from.jid carries the @lid, as in the live event.
Group message inside messages:
No media. History media is not stored: media_url is null and media_data has no file to download.

Delivery, retry and duplicates

  • Each batch is a POST with Content-Type: application/json and User-Agent: D-API-History-On-Demand/1.0. Answer 2xx within 30 seconds.
  • If your server doesn’t answer 2xx, the batch is resent with exponential backoff (5 s, 10 s, 20 s, 40 s, 80 s), in up to 6 attempts. After that, the batch is dropped.
  • If webhookUrl starts pointing to a blocked address (for example, after a DNS change), the batch is dropped right away, with no more attempts.
  • D-API never builds the same batch twice. But a retry can arrive after you have processed the batch (for example, if your response got lost). Use the batchId to ignore repeats, or the id of each message.
  • Batches arrive as the phone answers, with no guaranteed order between them. Sort by the messages’ timestamp.
  • Only on-demand pages are included. The full history requested with requestFull is not delivered through this event.

FAQ

The session was created without historySync: true. Pairing history only comes with the option turned on at creation, before pairing. Create the session with historySync: true and pair again.
The session has no webhook configured. Pass webhookUrl in the body or configure the session’s webhook with messages.received.
Your webhook was too slow (most calls took longer than 20 seconds) and the resend was stopped. Answer 2xx right away and process later, lower delivery.maxRps if needed, and fire it again, preferably by period.
For each chat, the phone may answer empty: it has nothing before the oldest stored message. Check the oldest message of a few chats through the API. If it didn’t change, that is the device’s limit.
Requests go unanswered. A chat with no answer is skipped, and after 3 requests in a row with no answer the whole sweep stops. Keep the phone online, with internet and WhatsApp open, and run the sweep again: it picks up where it stopped, from the oldest stored message.
None of the requested chats has a stored message to serve as an anchor. On-demand only extends chats that already exist. Check the JIDs in chats.
A sweep is already running in this session (send cancel: true to stop it) or the session is not connected.
The connection still runs an earlier version, without on-demand history. Contact support to update it.
The start of the sweep wasn’t confirmed in time. Try again, or use ?async=true and follow it through the command.result webhook.
With webhook: true, the request came without webhookUrl and the session doesn’t have the messages.received event enabled in the webhook: pass webhookUrl or configure messages.received. With webhookUrl, the URL was refused: use http or https and a public address.
Only new messages are delivered. If the phone answered empty, or only with messages D-API already had, there is no batch to send.
That’s expected: history media is not stored. Only text and metadata arrive.
If it didn’t come in the pairing import, on-demand can’t fetch it: it needs a stored message of the chat to start from.
Not without pairing again. requestFull is only accepted on WhatsApp Business; on personal WhatsApp, the phone refuses with ERROR_REQUEST_ON_NON_SMB_PRIMARY.