Installation
- npm
- yarn
- pnpm
Requirements
- Node.js 20.0 or later
- Valid WSAPI API key and instance ID
Quick start
baseUrl, apiKey and instanceId are all required: the constructor throws if any of them is missing. Recipients use the WhatsApp JID format, <number>@s.whatsapp.net for people and <id>@g.us for groups.Method naming
Two conventions apply to every client:- All methods end in
Asyncand return aPromise. - Each method has a
tryprefixed twin (sendTextAsyncandtrySendTextAsync). The plain method throwsApiExceptionon failure; thetryvariant never throws and returns anApiResponseinstead.
API surface
WSApiClient groups every operation under a namespace:
Error handling
- Exception-based
- Try-based (non-throwing)
Methods throw
ApiException on failure. The API error body is available on problem:Sending media
Media requests useurl for a remote file or data for base64 content. Documents also require filename. Use mimeType when needed (voice uses OGG; stickers use WebP). Legacy names such as imageUrl, imageBase64 and fileName remain accepted and are translated before sending; canonical fields take precedence.
mentions, replyTo, replyToSenderId, ephemeralExpiration and isForwarded are accepted by every message type:
Acting on a message
Reactions, edits, receipts and deletions take the message ID as the first argument and the rest of the payload as the second:In a group,
chatId is the group JID and senderId is the participant who sent the message. In a one to one chat both are the same JID.Groups, chats, contacts and session
Receiving events
WSAPI delivers events to your own endpoint as a webhook. Point your instance at an HTTPS URL and parse the body withEventFactory. When a signing secret is configured, verifySignature checks the X-Webhook-Signature header for you, in constant time:
EventFactory.parseEvent throws on an event type it does not know, so keep the handler in a try/catch if you want unknown events to be ignored rather than to fail the delivery. See Events for the payload envelope and the full list of event types.
Account and compatibility
Account operations are Cloud-only; the OSS administrative API is separate. Create a named Cloud instance withclient.account.createSubscriptionInstanceAsync(subscriptionId, "Support") (also available in the Try variant).
Session responses use isConnected, isLoggedIn and deviceId; QR text is code. Invite-link responses use link; joining via link returns id. Joining via invite returns no object (HTTP 204). To rotate a link, call getInviteLinkAsync(groupId, true).
Message events, including history messages, expose optional adReferral. Persist message.adReferral?.ctwaClid when present; subsequent messages may not contain attribution.