Class: Client
Defined in: client/Client.ts:69
WhatsApp Web client without a browser, with a built-in command router.
Exampleβ
const wa = createClient({ session: 'my-bot' });
wa.command('ping', (ctx) => ctx.reply('pong π'));
wa.hears(/hola/i, (ctx) => ctx.reply('hi!'));
wa.on('ready', (me) => console.log('Connected:', me.number));
await wa.start();
Extendsβ
EventEmitter
Constructorsβ
Constructorβ
new Client(
options?):Client
Defined in: client/Client.ts:85
Parametersβ
options?β
ClientOptions = {}
Returnsβ
Client
Overridesβ
EventEmitter.constructor
Propertiesβ
info?β
optionalinfo?:ClientInfo
Defined in: client/Client.ts:83
Account information once connected.
Accessorsβ
commandPrefixesβ
Get Signatureβ
get commandPrefixes():
string[]
Defined in: client/Client.ts:116
Configured command prefixes (used by Context).
Returnsβ
string[]
socketβ
Get Signatureβ
get socket():
object
Defined in: client/Client.ts:121
Raw Baileys socket (low-level access). Throws if there is no connection.
Returnsβ
object
Methodsβ
addGroupParticipants()β
addGroupParticipants(
jid,participants):Promise<object[]>
Defined in: client/Client.ts:759
Parametersβ
jidβ
string
participantsβ
string | string[]
Returnsβ
Promise<object[]>
blockUser()β
blockUser(
jid):Promise<void>
Defined in: client/Client.ts:683
Blocks a contact.
Parametersβ
jidβ
string
Returnsβ
Promise<void>
chat()β
chat(
id,name?):Chat
Defined in: client/Client.ts:582
Returns a fluent handle for a chat.
Parametersβ
idβ
string
name?β
string
Returnsβ
clearHistory()β
clearHistory(
chatId?):void
Defined in: client/Client.ts:605
Clears message history for one chat, or all chats.
Parametersβ
chatId?β
string
Returnsβ
void
command()β
command(
name,handler):this
Defined in: client/Client.ts:340
Registers a handler for one or more commands (!ping, /start, β¦).
Parametersβ
nameβ
string | string[]
handlerβ
Returnsβ
this
conversation()β
conversation(
id,expectedSender?):Conversation
Defined in: client/Client.ts:590
Returns a rich per-user handle: send + persistent state + history + ask.
expectedSender (used by ctx.conversation) scopes ask to a participant.
Parametersβ
idβ
string
expectedSender?β
string
Returnsβ
createGroup()β
createGroup(
subject,participants):Promise<GroupMetadata>
Defined in: client/Client.ts:745
Creates a group with the given subject and participants.
Parametersβ
subjectβ
string
participantsβ
string[]
Returnsβ
Promise<GroupMetadata>
deleteMessage()β
deleteMessage(
chatId,key):Promise<void>
Defined in: client/Client.ts:561
Deletes a message for everyone.
Parametersβ
chatIdβ
string
keyβ
WAMessageKey
Returnsβ
Promise<void>
demoteGroupParticipants()β
demoteGroupParticipants(
jid,participants):Promise<object[]>
Defined in: client/Client.ts:771
Parametersβ
jidβ
string
participantsβ
string | string[]
Returnsβ
Promise<object[]>
destroy()β
destroy():
Promise<void>
Defined in: client/Client.ts:821
Closes the connection without unlinking the device.
Returnsβ
Promise<void>
editMessage()β
editMessage(
chatId,key,newText):Promise<Message>
Defined in: client/Client.ts:566
Edits a previously sent (own) message.
Parametersβ
chatIdβ
string
keyβ
WAMessageKey
newTextβ
string
Returnsβ
Promise<Message>
emit()β
emit<
K>(event, ...args):boolean
Defined in: client/Client.ts:870
Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments
to each.
Returns true if the event had listeners, false otherwise.
import { EventEmitter } from 'node:events';
const myEmitter = new EventEmitter();
// First listener
myEmitter.on('event', function firstListener() {
console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
const parameters = args.join(', ');
console.log(`event with parameters ${parameters} in third listener`);
});
console.log(myEmitter.listeners('event'));
myEmitter.emit('event', 1, 2, 3, 4, 5);
// Prints:
// [
// [Function: firstListener],
// [Function: secondListener],
// [Function: thirdListener]
// ]
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener
Type Parametersβ
Kβ
K extends keyof ClientEvents
Parametersβ
eventβ
K
argsβ
...ClientEvents[K]
Returnsβ
boolean
Sinceβ
v0.1.26
Overridesβ
EventEmitter.emit
forwardMessage()β
forwardMessage(
to,message,options?):Promise<Message>
Defined in: client/Client.ts:571
Forwards a message to another chat.
Parametersβ
toβ
string
messageβ
WAMessage
options?β
MiscMessageGenerationOptions
Returnsβ
Promise<Message>
getBlocklist()β
getBlocklist():
Promise<string[]>
Defined in: client/Client.ts:693
Returns the list of blocked JIDs.
Returnsβ
Promise<string[]>
getGroupInviteLink()β
getGroupInviteLink(
jid):Promise<string>
Defined in: client/Client.ts:790
Current invite link of a group (https://chat.whatsapp.com/β¦).
Parametersβ
jidβ
string
Returnsβ
Promise<string>
getGroupMetadata()β
getGroupMetadata(
jid):Promise<GroupMetadata>
Defined in: client/Client.ts:740
Full metadata of a group.
Parametersβ
jidβ
string
Returnsβ
Promise<GroupMetadata>
getLid()β
getLid(
number):Promise<string|undefined>
Defined in: client/Client.ts:616
Gets the LID associated with a phone number, if WhatsApp exposes it.
Parametersβ
numberβ
string
Returnsβ
Promise<string | undefined>
getProfilePictureUrl()β
getProfilePictureUrl(
jid,highRes?):Promise<string|undefined>
Defined in: client/Client.ts:651
Profile picture URL of a chat/contact, or undefined if not available.
Parametersβ
jidβ
string
highRes?β
boolean = false
Returnsβ
Promise<string | undefined>
getStatus()β
getStatus(
jid):Promise<string|undefined>
Defined in: client/Client.ts:677
Fetches a contact's status/about text, if visible.
Parametersβ
jidβ
string
Returnsβ
Promise<string | undefined>
group()β
group(
id):Group
Defined in: client/Client.ts:735
Returns a fluent handle for a group.
Parametersβ
idβ
string
Returnsβ
hears()β
hears(
trigger,handler):this
Defined in: client/Client.ts:349
Registers a handler that fires when the text matches a pattern.
Parametersβ
triggerβ
handlerβ
Returnsβ
this
historyFor()β
historyFor(
chatId):Message[]
Defined in: client/Client.ts:600
Recent in-memory message history for a chat.
Parametersβ
chatIdβ
string
Returnsβ
Message[]
initialize()β
initialize():
Promise<void>
Defined in: client/Client.ts:137
Starts the connection. Emits qr (or pairing_code) the first time and
ready once it is operational.
Returnsβ
Promise<void>
isRegisteredUser()β
isRegisteredUser(
number):Promise<boolean>
Defined in: client/Client.ts:610
Is the number registered on WhatsApp?
Parametersβ
numberβ
string
Returnsβ
Promise<boolean>
joinGroupViaLink()β
joinGroupViaLink(
linkOrCode):Promise<string|undefined>
Defined in: client/Client.ts:802
Joins a group via an invite link or code. Returns the group JID.
Parametersβ
linkOrCodeβ
string
Returnsβ
Promise<string | undefined>
leaveGroup()β
leaveGroup(
jid):Promise<void>
Defined in: client/Client.ts:785
Leaves a group.
Parametersβ
jidβ
string
Returnsβ
Promise<void>
logout()β
logout():
Promise<void>
Defined in: client/Client.ts:810
Logs out of WhatsApp and deletes the local credentials.
Returnsβ
Promise<void>
messages()β
messages(
options?):AsyncIterableIterator<Context>
Defined in: client/Client.ts:207
Async iterator over incoming messages (not your own). Lets you handle
messages with a linear for await loop instead of registering callbacks.
Messages are buffered between iterations, so none are lost.
Parametersβ
options?β
signal?β
AbortSignal
Returnsβ
AsyncIterableIterator<Context>
Exampleβ
for await (const ctx of wa.messages()) {
if (ctx.command === 'ping') await ctx.reply('pong');
}
next()β
next<
K>(event,options?):Promise<ClientEvents[K][0]>
Defined in: client/Client.ts:220
Awaits the next occurrence of an event (one-shot promise).
Type Parametersβ
Kβ
K extends keyof ClientEvents
Parametersβ
eventβ
K
options?β
signal?β
AbortSignal
Returnsβ
Promise<ClientEvents[K][0]>
off()β
off<
K>(event,listener):this
Defined in: client/Client.ts:863
Alias for emitter.removeListener().
Type Parametersβ
Kβ
K extends keyof ClientEvents
Parametersβ
eventβ
K
listenerβ
(...args) => void
Returnsβ
this
Sinceβ
v10.0.0
Overridesβ
EventEmitter.off
on()β
on<
K>(event,listener):this
Defined in: client/Client.ts:849
Adds the listener function to the end of the listeners array for the event
named eventName. No checks are made to see if the listener has already
been added. Multiple calls passing the same combination of eventName and
listener will result in the listener being added, and called, multiple times.
server.on('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the
event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
Type Parametersβ
Kβ
K extends keyof ClientEvents
Parametersβ
eventβ
K
listenerβ
(...args) => void
The callback function
Returnsβ
this
Sinceβ
v0.1.101
Overridesβ
EventEmitter.on
once()β
once<
K>(event,listener):this
Defined in: client/Client.ts:856
Adds a one-time listener function for the event named eventName. The
next time eventName is triggered, this listener is removed and then invoked.
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the
event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
Type Parametersβ
Kβ
K extends keyof ClientEvents
Parametersβ
eventβ
K
listenerβ
(...args) => void
The callback function
Returnsβ
this
Sinceβ
v0.3.0
Overridesβ
EventEmitter.once
promoteGroupParticipants()β
promoteGroupParticipants(
jid,participants):Promise<object[]>
Defined in: client/Client.ts:767
Parametersβ
jidβ
string
participantsβ
string | string[]
Returnsβ
Promise<object[]>
react()β
react(
to,key,emoji):Promise<void>
Defined in: client/Client.ts:517
Reacts to a message with an emoji (empty string to remove the reaction).
Parametersβ
toβ
string
keyβ
WAMessageKey
emojiβ
string
Returnsβ
Promise<void>
removeGroupParticipants()β
removeGroupParticipants(
jid,participants):Promise<object[]>
Defined in: client/Client.ts:763
Parametersβ
jidβ
string
participantsβ
string | string[]
Returnsβ
Promise<object[]>
removeProfilePicture()β
removeProfilePicture(
jid?):Promise<void>
Defined in: client/Client.ts:662
Removes a profile picture (your own by default).
Parametersβ
jid?β
string
Returnsβ
Promise<void>
revokeGroupInviteLink()β
revokeGroupInviteLink(
jid):Promise<string>
Defined in: client/Client.ts:796
Revokes a group's invite link and returns the new one.
Parametersβ
jidβ
string
Returnsβ
Promise<string>
send()β
send(
to,content,options?):Promise<Message>
Defined in: client/Client.ts:393
Sends raw Baileys content (or text) to a chat.
Parametersβ
toβ
string
contentβ
string | AnyMessageContent
options?β
MiscMessageGenerationOptions = {}
Returnsβ
Promise<Message>
sendAudio()β
sendAudio(
to,source,options?):Promise<Message>
Defined in: client/Client.ts:424
Parametersβ
toβ
string
sourceβ
options?β
AudioOptions = {}
Returnsβ
Promise<Message>
sendContact()β
sendContact(
to,contacts,options?):Promise<Message>
Defined in: client/Client.ts:542
Sends one or more contact cards (vCards).
Parametersβ
toβ
string
contactsβ
options?β
MiscMessageGenerationOptions
Returnsβ
Promise<Message>
sendDocument()β
sendDocument(
to,source,options?):Promise<Message>
Defined in: client/Client.ts:443
Parametersβ
toβ
string
sourceβ
options?β
DocumentOptions = {}
Returnsβ
Promise<Message>
sendFromLink()β
sendFromLink(
to,link,options?):Promise<Message>
Defined in: client/Client.ts:488
Sends whatever a link points to, auto-detecting the media kind
(image/video/audio/document) from the extension or Content-Type. Works
with any http(s) URL, including S3 public or presigned URLs, or a local path.
Pass type to skip detection.
Parametersβ
toβ
string
linkβ
string
options?β
SendFromLinkOptions = {}
Returnsβ
Promise<Message>
Exampleβ
await wa.sendFromLink('34600112233', 'https://my-bucket.s3.amazonaws.com/report.pdf');
await wa.sendFromLink('34600112233', 'https://cdn.example.com/clip.mp4', { caption: 'look' });
sendImage()β
sendImage(
to,source,options?):Promise<Message>
Defined in: client/Client.ts:412
Parametersβ
toβ
string
sourceβ
options?β
CaptionOptions = {}
Returnsβ
Promise<Message>
sendLocation()β
sendLocation(
to,location,options?):Promise<Message>
Defined in: client/Client.ts:457
Parametersβ
toβ
string
locationβ
options?β
MiscMessageGenerationOptions
Returnsβ
Promise<Message>
sendPoll()β
sendPoll(
to,poll,options?):Promise<Message>
Defined in: client/Client.ts:522
Sends a poll.
Parametersβ
toβ
string
pollβ
options?β
MiscMessageGenerationOptions
Returnsβ
Promise<Message>
sendSticker()β
sendSticker(
to,source,options?):Promise<Message>
Defined in: client/Client.ts:533
Sends a sticker (a WebP image works best).
Parametersβ
toβ
string
sourceβ
options?β
MiscMessageGenerationOptions
Returnsβ
Promise<Message>
sendText()β
sendText(
to,text,options?):Promise<Message>
Defined in: client/Client.ts:407
Parametersβ
toβ
string
textβ
string
options?β
TextOptions = {}
Returnsβ
Promise<Message>
sendVideo()β
sendVideo(
to,source,options?):Promise<Message>
Defined in: client/Client.ts:418
Parametersβ
toβ
string
sourceβ
options?β
CaptionOptions = {}
Returnsβ
Promise<Message>
sendVoice()β
sendVoice(
to,source,options?):Promise<Message>
Defined in: client/Client.ts:435
Voice note (audio with ptt: true).
Parametersβ
toβ
string
sourceβ
options?β
MiscMessageGenerationOptions = {}
Returnsβ
Promise<Message>
setName()β
setName(
name):Promise<void>
Defined in: client/Client.ts:667
Updates your display name.
Parametersβ
nameβ
string
Returnsβ
Promise<void>
setPresence()β
setPresence(
state,to?):Promise<void>
Defined in: client/Client.ts:638
Updates your presence, optionally scoped to a chat.
Parametersβ
stateβ
"unavailable" | "available" | "composing" | "recording" | "paused"
to?β
string
Returnsβ
Promise<void>
setProfilePicture()β
setProfilePicture(
source,jid?):Promise<void>
Defined in: client/Client.ts:656
Sets a profile picture (your own by default, or a group you admin).
Parametersβ
sourceβ
jid?β
string
Returnsβ
Promise<void>
setStatus()β
setStatus(
status):Promise<void>
Defined in: client/Client.ts:672
Updates your status/about text.
Parametersβ
statusβ
string
Returnsβ
Promise<void>
start()β
start():
Promise<void>
Defined in: client/Client.ts:129
Starts the connection (alias of initialize).
Returnsβ
Promise<void>
stateFor()β
stateFor(
chatId):ConversationState
Defined in: client/Client.ts:595
The persistent state for a chat.
Parametersβ
chatIdβ
string
Returnsβ
stream()β
stream<
K>(event,options?):AsyncIterableIterator<ClientEvents[K][0]>
Defined in: client/Client.ts:212
Async iterator over any client event ('qr', 'reaction', β¦).
Type Parametersβ
Kβ
K extends keyof ClientEvents
Parametersβ
eventβ
K
options?β
signal?β
AbortSignal
Returnsβ
AsyncIterableIterator<ClientEvents[K][0]>
subscribeToPresence()β
subscribeToPresence(
jid):Promise<void>
Defined in: client/Client.ts:646
Subscribes to a contact's presence so 'presence' events start arriving.
Parametersβ
jidβ
string
Returnsβ
Promise<void>
unblockUser()β
unblockUser(
jid):Promise<void>
Defined in: client/Client.ts:688
Unblocks a contact.
Parametersβ
jidβ
string
Returnsβ
Promise<void>
updateGroupDescription()β
updateGroupDescription(
jid,description):Promise<void>
Defined in: client/Client.ts:755
Updates a group's description.
Parametersβ
jidβ
string
descriptionβ
string
Returnsβ
Promise<void>
updateGroupSubject()β
updateGroupSubject(
jid,subject):Promise<void>
Defined in: client/Client.ts:750
Updates a group's subject (name).
Parametersβ
jidβ
string
subjectβ
string
Returnsβ
Promise<void>
use()β
use(
middleware):this
Defined in: client/Client.ts:334
Registers a global middleware (runs for every incoming message).
Parametersβ
middlewareβ
Returnsβ
this
waitForMessage()β
waitForMessage(
filter?,options?):Promise<Context>
Defined in: client/Client.ts:706
Waits for the next incoming message that matches filter (any message by
default). Useful for questionβanswer flows.
Parametersβ
filter?β
(ctx) => boolean
options?β
signal?β
AbortSignal
timeoutMs?β
number
Returnsβ
Promise<Context>
Throwsβ
if it times out or the signal aborts before a match.
waitUntilReady()β
waitUntilReady(
timeoutMs?):Promise<ClientInfo>
Defined in: client/Client.ts:175
Waits until the connection is ready.
Parametersβ
timeoutMs?β
number
Returnsβ
Promise<ClientInfo>