- Traditional path. When connecting, the phone sends the history and D-API
stores it. Then you call
POST /api/v1/history/syncto receive those messages at your webhook. - 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.
Which path to use
Create the session with historySync: true
Connect (QR or code)
INITIAL_BOOTSTRAP, RECENT,
FULL) and D-API stores it.Call POST /api/v1/history/sync
messages.received.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 withhistorySync: true (the default is false). The option applies at
creation, before pairing: see
Create a session.
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.
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.
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.totalis the number of messages matching the filter. Withtotal: 0, nothing is sent andsyncingisfalse.- Only one resend runs per session. If one is already running, the response
returns its
syncJobIdand amessagewith the progress, without opening another.
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.receivedevent, with the samedataas the live event (id,from,to,type,fromMe,message,timestampin milliseconds…). TheUser-AgentisD-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
idso 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, dividetotalbymaxRps(18,172 ÷ 50 ≈ 6 min). - Retry: answer
2xxwithin 30 seconds. Without2xx, 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.
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:- D-API takes the oldest stored message of the chat and uses it as the anchor.
- It asks the phone for a block of messages before it (WhatsApp recommends 50 per request; D-API accepts up to 100).
- The phone answers asynchronously, usually in about 2 seconds. The
messages go through the same path as pairing (
history.message.*events) and are stored. - The oldest message of the block becomes the new anchor, and the next request goes back one more block.
POST /api/v1/history/full-sync, under History in the API reference), for one
chat or for many.
One chat
Pass the chat JID inchats. The sweep goes back page by page until it comes
back empty or until maxPagesPerChat.
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)
Withoutchats, the sweep goes through the chats with the most recent
message first, up to maxChats.
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
maxChats chats with the most
recent message are used. With it, the processing order is not guaranteed.requestFull section below.messages.received
URL), in the history.on_demand.messages event. See
Receive the messages through the webhook.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 mostmaxPagesPerChat × 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 waitsintervalMs and then the phone’s answer (about 2 s). In the
worst case, when no chat ends before the 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 hasdata.statuscancelled(a sweep was running and stopped) ornot_running(none was running).
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.
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 smallmaxChats, 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:
Receive the fetched messages
History messages arrive through RabbitMQ, in thehistory.message.*
events. In the webhook they are suppressed, with two exceptions:
- In the sweep, send
webhookUrlorwebhook: trueto receive the new messages at the webhook. See the next section. - After the sweep,
POST /api/v1/history/syncalso resends what was stored, as in the traditional path.
Receive the messages through the webhook
WithwebhookUrl 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.
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:data of
messages.received: id, from
and to, type, fromMe, message, timestamp in milliseconds and the
other fields.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
fromfor received messages and intofor messages sent from the phone, with theirlid. The connection’s number is on the other side, withoutlid. - Group:
tois the group (ajidending in@g.usand the group name). When the sender’s phone number can’t be found,from.jidcarries the@lid, as in the live event.
messages:
media_url is null and
media_data has no file to download.Delivery, retry and duplicates
- Each batch is a
POSTwithContent-Type: application/jsonandUser-Agent: D-API-History-On-Demand/1.0. Answer2xxwithin 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
webhookUrlstarts 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
batchIdto ignore repeats, or theidof 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
requestFullis not delivered through this event.
FAQ
I connected, but no history showed up
I connected, but no history showed up
historySync: true. Pairing history only
comes with the option turned on at creation, before pairing. Create the
session with historySync: true and pair again./history/sync answers 400: webhookUrl required
/history/sync answers 400: webhookUrl required
webhookUrl in the body or
configure the session’s webhook with messages.received.The /history/sync resend ended with status failed
The /history/sync resend ended with status failed
2xx right away and process later, lower
delivery.maxRps if needed, and fire it again, preferably by period.The sweep started, but nothing new showed up
The sweep started, but nothing new showed up
The phone was offline
The phone was offline
Error 400: No stored messages to anchor the history request
Error 400: No stored messages to anchor the history request
chats.Error 409: already running or not connected
Error 409: already running or not connected
cancel: true to stop it)
or the session is not connected.Error: no handler registered for command type: history_full_sync
Error: no handler registered for command type: history_full_sync
Error 504: the session didn't answer in time
Error 504: the session didn't answer in time
?async=true and follow it through the command.result webhook.Error 400 when asking for webhook delivery
Error 400 when asking for webhook delivery
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.I asked for webhook delivery, but no POST arrived
I asked for webhook delivery, but no POST arrived
Messages came without image, audio or document
Messages came without image, audio or document
An old chat doesn't show up at all
An old chat doesn't show up at all
Can I request the full history again on personal WhatsApp?
Can I request the full history again on personal WhatsApp?
requestFull is only accepted on WhatsApp
Business; on personal WhatsApp, the phone refuses with
ERROR_REQUEST_ON_NON_SMB_PRIMARY.