Skip to main content
Baileys provides a unified sendMessage function to send all types of messages. The function handles encryption, media upload, and delivery to all recipient devices automatically.

Basic usage

All messages are sent using the sendMessage method:
const jid: string
const content: AnyMessageContent
const options: MiscMessageGenerationOptions

await sock.sendMessage(jid, content, options)

Parameters

jid
string
required
The recipient’s JID (e.g., '[email protected]' for individual chats or '[email protected]' for groups)
content
AnyMessageContent
required
The message content object. See AnyMessageContent for all supported message types.
options
MiscMessageGenerationOptions
Optional configuration for message generation. See MiscMessageGenerationOptions for all available options.

Message content types

The AnyMessageContent type supports various message formats:
  • Text messages: Simple text, with support for mentions, quotes, and formatting
  • Media messages: Images, videos, audio, documents, stickers
  • Location messages: Share geographical coordinates
  • Contact messages: Share vCard contacts
  • Poll messages: Create interactive polls
  • Reaction messages: React to existing messages
  • Forward messages: Forward existing messages
See the specific pages for detailed examples:

Message generation options

The MiscMessageGenerationOptions parameter allows you to customize message behavior:

Quote a message

Reply to or quote any message by passing the original message object:
await sock.sendMessage(jid, { text: 'This is a reply' }, { quoted: originalMessage })
The quoted option works with all message types, not just text messages.

Ephemeral messages

Send messages that disappear after a set duration:
await sock.sendMessage(
  jid,
  { text: 'This message will disappear' },
  { ephemeralExpiration: 86400 } // 24 hours in seconds
)

Cached group metadata

Optimize group message sending by using cached metadata:
await sock.sendMessage(
  groupJid,
  { text: 'Hello group!' },
  { useCachedGroupMetadata: true }
)

Status (broadcast) messages

When sending status/broadcast messages, specify the recipient list:
await sock.sendMessage(
  'status@broadcast',
  { text: 'My status update' },
  { 
    statusJidList: [
      '[email protected]',
      '[email protected]'
    ]
  }
)

Return value

The sendMessage function returns a WAMessage object containing:
const message = await sock.sendMessage(jid, { text: 'Hello' })

console.log(message.key.id) // Message ID
console.log(message.key.fromMe) // true
console.log(message.messageTimestamp) // Unix timestamp

Error handling

try {
  await sock.sendMessage(jid, { text: 'Hello' })
} catch (error) {
  console.error('Failed to send message:', error)
}

Advanced features

Custom message ID

Generate your own message ID:
import { generateMessageIDV2 } from '@whiskeysockets/baileys'

const customId = generateMessageIDV2(sock.user?.id)
await sock.sendMessage(jid, { text: 'Hello' }, { messageId: customId })

Patch messages before sending

Modify messages before they’re sent (configured at socket level):
const sock = makeWASocket({
  patchMessageBeforeSending: async (message, recipientJids) => {
    // Modify the message here
    return message
  }
})

Best practices

  • Always handle errors when sending messages
  • Use useCachedGroupMetadata: true for better performance in groups
  • Implement proper rate limiting to avoid being blocked by WhatsApp
  • Store message IDs to track delivery and read status
Sending messages too quickly may result in temporary account restrictions. Implement appropriate delays between messages.

Next steps

Text messages

Learn about text formatting, mentions, and links

Media messages

Send images, videos, audio, and documents

Receiving messages

Handle incoming messages and events

Message modification

Edit, delete, and react to messages

Build docs developers (and LLMs) love