Migrating from Meta's `whatsapp` package
This guide is for projects that use the npm package
whatsapp (GitHub:
WhatsApp/WhatsApp-Nodejs-SDK,
version 0.0.5-Alpha) and want to move to @solvejet/whatsapp-cloud-api.
@solvejet/whatsapp-cloud-api is an independent open-source project. It is not
affiliated with, endorsed by, or sponsored by Meta or WhatsApp.
The “before” code below was checked against the published whatsapp@0.0.5-Alpha
tarball, which includes its README, TypeScript source and build output.
Why you might migrate
Section titled “Why you might migrate”You do not have to. These are the practical reasons:
- Archived upstream. The GitHub repository is archived (read-only), and
0.0.5-Alpha, published April 2023, is the latest version on npm. It uses
CLOUD_API_VERSIONfrom your environment, so you pick the Graph version, but new Graph features are not added to the SDK. - Coverage. The package’s
messagesAPI covers sending messages and marking them read. It has no method for reactions, media upload or download, or template, WABA, flow, block or calling management. See the mapping and what it does that we do not. - Configuration. It reads settings from environment variables on construction. This package takes an explicit config object, which is easier to use with several phone numbers or tokens in one process.
- Types for webhooks and errors. Parsed deliveries here are a
discriminated union on
type, and failures are typed error classes. - Dependencies. The package has one runtime dependency (
@types/node).@solvejet/whatsapp-cloud-apihas none.
Install
Section titled “Install”npm uninstall whatsappnpm install @solvejet/whatsapp-cloud-apiRequires Node.js 20 or newer. whatsapp@0.0.5-Alpha declares node >= 16.19.1,
so check your runtime first.
API mapping
Section titled “API mapping”wa is new WhatsApp(...) from the whatsapp package; client is a
WhatsAppClient.
| Task | whatsapp 0.0.5-Alpha |
@solvejet/whatsapp-cloud-api |
|---|---|---|
| Create the client | new WhatsApp(senderPhoneNumberId?), config from environment variables |
new WhatsAppClient({ accessToken, phoneNumberId, businessAccountId, apiVersion }) |
| Send text | wa.messages.text({ body, preview_url }, recipient) |
client.messages.sendText(to, body, { previewUrl }) |
| Send image | wa.messages.image({ link } | { id }, recipient) |
client.messages.sendImage(to, { link } | { id }) |
| Video, audio, sticker | wa.messages.video(...), audio(...), sticker(...) |
client.messages.sendVideo, sendAudio, sendSticker |
| Document | wa.messages.document(body, recipient) |
client.messages.sendDocument(to, { link | id, caption, filename }) |
| Location | wa.messages.location(body, recipient) |
client.messages.sendLocation(to, { latitude, longitude, name, address }) |
| Contacts | wa.messages.contacts([contact], recipient) |
client.messages.sendContacts(to, [contact]) |
| Template | wa.messages.template({ name, language: { policy, code }, components }, recipient) |
client.messages.sendTemplate(to, { name, language: { code }, components }) |
| Interactive | wa.messages.interactive(interactiveObject, recipient) |
sendInteractiveButtons, sendInteractiveList, sendInteractiveCtaUrl, sendInteractiveFlow, sendProduct, sendProductList |
| Reply to a message | third argument: replyMessageId |
client.messages.sendRaw({ ..., context: { message_id } }) (see Reply context) |
| Reaction | not available (see note) | client.messages.sendReaction(to, { messageId, emoji }) |
| Mark as read | wa.messages.status({ status: 'read', message_id }) |
client.messages.markAsRead(messageId) |
| Upload media | not available | client.media.upload(...) |
| Download media | not available | client.media.download(mediaId), getUrl, downloadByUrl |
| Two-step PIN | wa.twoStepVerification.setPin(pin) |
client.phoneNumbers.setTwoStepPin(pin) |
| Webhook verification (GET) | wa.webhooks.start(cb) handles hub.mode and hub.verify_token |
verifyWebhookQuery(query, token) or WebhookHandler.handleVerification(query) |
| Signature (POST) | wa.webhooks.start(cb) checks x-hub-signature-256 with M4D_APP_SECRET |
verifySignature(rawBody, header, appSecret) or WebhookHandler.parse(rawBody, header) |
| Parse deliveries | cb(statusCode, headers, body, response, error); body is the raw JSON object |
handler.parse(rawBody, header) returns { messages, statuses, raw } |
| Stop the listener | wa.webhooks.stop(cb) |
not needed: you own the HTTP server |
| Update token | wa.updateAccessToken(token), updateSenderNumberId, updateTimeout |
construct another WhatsAppClient |
Notes:
- In the
whatsappsource,MessageTypesEnumhasReaction = 'sticker'andReactionMessageRequestBodyexists as a type, butMessagesAPIhas noreactionmethod. You would build the body yourself and pass it towa.messages.send. - Recipients in
whatsapparenumbers (recipient.toString()is sent). Heretois a string, so keep leading country codes as text. whatsappmethods return an object withstatusCode(),headers(), andresponseBodyToJSON(). They resolve for non-2xx responses too: checkstatusCode(). Here the returned promise resolves to the parsed Graph body and throws on API errors.
Before and after
Section titled “Before and after”1. Create a client and send text
Section titled “1. Create a client and send text”Before (whatsapp 0.0.5-Alpha), with CLOUD_API_ACCESS_TOKEN,
CLOUD_API_VERSION, and WA_PHONE_NUMBER_ID set in the environment:
import WhatsApp from 'whatsapp';
const wa = new WhatsApp(); // or new WhatsApp(Number(process.env.WA_PHONE_NUMBER_ID))const response = await wa.messages.text({ body: 'Hello world', preview_url: true }, 15551234567);console.log(response.statusCode(), await response.responseBodyToJSON());After:
import { WhatsAppClient } from '@solvejet/whatsapp-cloud-api';
const client = new WhatsAppClient({ accessToken: process.env.WHATSAPP_ACCESS_TOKEN!, phoneNumberId: process.env.WHATSAPP_PHONE_NUMBER_ID!, apiVersion: 'v26.0', // the default});
const res = await client.messages.sendText('15551234567', 'Hello world', { previewUrl: true,});console.log(res.messages[0]?.id);2. Media and templates
Section titled “2. Media and templates”Before:
await wa.messages.image({ link: 'https://example.com/cat.jpg', caption: 'A cat' }, 15551234567);await wa.messages.template( { name: 'hello_world', language: { policy: 'deterministic', code: 'en_US' } }, 15551234567,);In TypeScript, code is typed as the package’s LanguagesEnum, which the
package entry point does not export. Plain JavaScript can pass the string.
After:
import { WhatsAppClient } from '@solvejet/whatsapp-cloud-api';
const client = new WhatsAppClient({ accessToken: process.env.WHATSAPP_ACCESS_TOKEN!, phoneNumberId: process.env.WHATSAPP_PHONE_NUMBER_ID!,});const to = '15551234567';
await client.messages.sendImage(to, { link: 'https://example.com/cat.jpg', caption: 'A cat',});
await client.messages.sendTemplate(to, { name: 'hello_world', language: { code: 'en_US' },});3. Interactive messages
Section titled “3. Interactive messages”Before, you build the Graph interactive object yourself:
await wa.messages.interactive( { type: 'button', body: { text: 'Pick one' }, action: { buttons: [ { type: 'reply', reply: { id: 'yes', title: 'Yes' } }, { type: 'reply', reply: { id: 'no', title: 'No' } }, ], }, }, 15551234567,);After, there is one method per interactive type, and limits such as the three-button maximum are checked before sending:
import { WhatsAppClient } from '@solvejet/whatsapp-cloud-api';
const client = new WhatsAppClient({ accessToken: process.env.WHATSAPP_ACCESS_TOKEN!, phoneNumberId: process.env.WHATSAPP_PHONE_NUMBER_ID!,});
await client.messages.sendInteractiveButtons('15551234567', { body: 'Pick one', buttons: [ { id: 'yes', title: 'Yes' }, { id: 'no', title: 'No' }, ],});4. Mark as read, react, and handle media
Section titled “4. Mark as read, react, and handle media”Before, only mark-as-read exists:
await wa.messages.status({ status: 'read', message_id: 'wamid.XXX' });After:
import { WhatsAppClient } from '@solvejet/whatsapp-cloud-api';
const client = new WhatsAppClient({ accessToken: process.env.WHATSAPP_ACCESS_TOKEN!, phoneNumberId: process.env.WHATSAPP_PHONE_NUMBER_ID!,});
await client.messages.markAsRead('wamid.XXX');await client.messages.sendReaction('15551234567', { messageId: 'wamid.XXX', emoji: '👍' });
// Upload bytes, then send by id.const { id } = await client.media.upload({ file: new TextEncoder().encode('hello'), filename: 'hello.txt', type: 'text/plain',});await client.messages.sendDocument('15551234567', { id, filename: 'hello.txt' });
// Download media that a user sent you.const { data, mimeType } = await client.media.download(id);console.log(mimeType, data.byteLength);5. Receive webhooks
Section titled “5. Receive webhooks”Before, wa.webhooks.start(cb) opens a plain http server (it calls
http.createServer, so TLS has to come from your proxy) on
LISTENER_PORT, serving /${WEBHOOK_ENDPOINT}. The callback receives the
status code, headers, parsed body, the response object, and an error. In the
success path the callback is passed the response and must end it:
wa.webhooks.start((statusCode, headers, body, response, error) => { if (error) { console.error(error); return; } console.log(JSON.stringify(body)); response.end(); // the callback is handed the response; it must be ended});After, you own the server and pass the raw body to the SDK. This version uses
standard Request and Response types, so it works in Next.js route handlers,
Hono and Cloudflare Workers. Express and Fastify versions are in
examples/.
import { WebhookHandler, WhatsAppWebhookError } from '@solvejet/whatsapp-cloud-api';
const handler = new WebhookHandler({ verifyToken: process.env.WHATSAPP_VERIFY_TOKEN!, appSecret: process.env.WHATSAPP_APP_SECRET!,});
export function verify(request: Request): Response { const query: Record<string, string | undefined> = {}; for (const [key, value] of new URL(request.url).searchParams) query[key] = value; const { statusCode, body } = handler.handleVerification(query); return new Response(body, { status: statusCode });}
export async function receive(request: Request): Promise<Response> { const rawBody = await request.text(); // not request.json() try { const { messages, statuses } = handler.parse( rawBody, request.headers.get('x-hub-signature-256') ?? '', ); console.log(messages.length, 'messages', statuses.length, 'statuses'); return new Response('OK'); } catch (error) { if (error instanceof WhatsAppWebhookError) return new Response('Unauthorized', { status: 401 }); throw error; }}Reply context
Section titled “Reply context”whatsapp accepts a replyMessageId as the last argument of each send method
and adds context: { message_id } to the body. This package does not have a
typed option for it on the senders. Use sendRaw, which posts any
body and is not validated on the client:
import { WhatsAppClient } from '@solvejet/whatsapp-cloud-api';
const client = new WhatsAppClient({ accessToken: process.env.WHATSAPP_ACCESS_TOKEN!, phoneNumberId: process.env.WHATSAPP_PHONE_NUMBER_ID!,});
await client.messages.sendRaw({ to: '15551234567', type: 'text', text: { body: 'Replying to you' }, context: { message_id: 'wamid.XXX' },});Things that behave differently
Section titled “Things that behave differently”- Configuration.
whatsappreads environment variables when you construct it. Here nothing is read from the environment; pass values explicitly.CLOUD_API_VERSIONis required there. Here the default isv26.0. - Errors.
whatsappthrows plainErrors for missing configuration, and for HTTP responses it gives you a response object whosestatusCode()you check. Here you getWhatsAppApiErrorand subclasses (WhatsAppRateLimitError,WhatsAppReEngagementError,WhatsAppAuthenticationError),WhatsAppRequestErrorfor network and timeout failures, andWhatsAppValidationErrorfor local checks. See Error handling. - Retries. The
whatsappREADME listsMAX_RETRIES_AFTER_WAIT(default 30). In the 0.0.5-Alpha source that value is read into the config, and we did not find code that uses it to retry. Here,429,5xx, and network errors are retried (maxRetries, default 2) with backoff, andRetry-Afteris honored. Message sends are the exception: they retry on429only, because a send that failed after Meta accepted it could be repeated. SetretryMessageSends: trueto retry sends on5xxand network errors too, ormaxRetries: 0to handle retries yourself. Multipart media uploads are not retried. - Timeouts.
whatsappdefaults to 20,000 ms (REQUEST_TIMEOUT). Here it is 30,000 ms per attempt (timeoutMs). - Webhook signature. Both check the
X-Hub-Signature-256HMAC-SHA256 of the body with your App Secret. Differences: here the check is a constant-time comparison against the raw bytes you pass in, a missing or malformed header fails, and it runs on every parse. In thewhatsappsource the POST branch only handles requests that carry the header and compares the hex digests with==. - Raw body is required. Whatever web framework you use, give
WebhookHandler.parsethe exact bytes Meta sent. If your framework parses JSON first and you re-serialize it, the signature will not match. Pass the raw body as a string,Buffer,Uint8ArrayorArrayBuffer. In Express, useexpress.raw({ type: 'application/json' })orexpress.json({ verify })to keep the buffer on that route. In Next.js route handlers useawait request.text(). - Unset secrets throw at construction.
new WebhookHandler({ verifyToken, appSecret })throwsWhatsAppWebhookErrorwhen either value is present butundefined, empty or not a string. An unset environment variable therefore fails at startup instead of silently turning verification off, and the!non-null assertion in the examples does not change that at runtime. Omitting theappSecretkey entirely still means no signature enforcement (handler.parse(...)skips the check), so set it in production.verifySignature(rawBody, header, secret)returnsfalse, and does not throw, for an empty or non-string secret, a bad header or an unsupported body type. - Only the
messagesfield is read.extractMessages,extractStatusesandhandler.parsereturn data from changes whosefieldismessages. Other fields, such as call events from thecallsfield, are not returned as messages or statuses; read them fromparsed.raw. - Body size and errors. String and byte bodies over 3 MiB (Meta’s documented
maximum) are rejected before parsing. Change this with
maxBodyByteson the handler orparseWebhookoptions, or passfalseto turn the check off. “Not valid JSON” errors do not carry the parser error ascause, because its message can include a fragment of the request body. - No server. This package does not start an HTTP listener, so there is no
port, path or
stop()to configure. - Recipient type.
tois a string, not a number. It can also be{ bsuid }for business-scoped user IDs. For message bodies that this SDK does not wrap yet,client.messages.sendRaw(body)posts any body and skips the client-side checks. - Module formats. Both ESM and CommonJS work. Node 20 or newer is required.
What the whatsapp package does that this package does not
Section titled “What the whatsapp package does that this package does not”- A built-in webhook listener.
wa.webhooks.start(cb)creates an HTTP server with a configurable port and endpoint, and reports signature failures through the callback. This package has no server; see the examples for Express, Next.js, Hono and Fastify. - A typed
replyMessageIdargument on each sender. UsesendRawwithcontexthere, as shown above. - Environment-driven setup. If you rely on the
WA_*,M4D_*andCLOUD_API_*variables, you will need to map them to constructor options. - Node 16.19.1 to 19. The old package declares
node >= 16.19.1. This package needs Node 20+. - A debug logger.
DEBUG=trueswitches on logging inwhatsapp. This package has no logging switch. - Meta authorship and license.
whatsappis published by Meta Platforms under its own license text (see itsLICENSEfile). This package is a third-party MIT-licensed project. If your organization requires vendor-authored SDKs, that is a reason to stay.
Checklist
Section titled “Checklist”- Check Node is 20 or newer.
- Replace the dependency (
npm uninstall whatsapp, then install@solvejet/whatsapp-cloud-api). - Map your environment variables to
new WhatsAppClient({ accessToken, phoneNumberId, businessAccountId, apiVersion }). - Rename each
wa.messages.*call using the table. Recipients become strings. - Replace
wa.messages.status(...)withclient.messages.markAsRead(...). - Replace
wa.webhooks.start(cb)with your own routes. Keep the raw body bytes and pass them tohandler.parseunchanged. Create aWebhookHandlerwith both the verify token and the App Secret; an empty value for either throws at construction, so check your environment variables. The callback’s parsedbodybecomesparsed.raw, and messages and statuses come from themessagesfield only. Bodies over 3 MiB are rejected unless you setmaxBodyBytes. - Replace status-code checks on response objects with
try/catchon the error classes. - Decide on
maxRetriesand message de-duplication. - Replace any
replyMessageIduse withsendRawandcontext. - Remove the
whatsappenvironment variables you no longer use. - Optional: use reactions, media upload and download, templates, and the other resources.
Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.