Skip to main content

Overview

makeWASocket is the main factory function for creating a new WhatsApp Web socket connection. It initializes and returns a WASocket instance that provides access to all WhatsApp functionality.

Function signature

function makeWASocket(config: UserFacingSocketConfig): WASocket

Parameters

config
UserFacingSocketConfig
required
Configuration object for the socket connection. See SocketConfig for all available options.

Required configuration

The only required property is auth, which manages authentication state:
config.auth
AuthenticationState
required
Authentication state object containing credentials and keys. See AuthenticationState for details.

Return value

WASocket
object
Returns a socket instance with methods and properties for interacting with WhatsApp. The socket is a layered object combining functionality from multiple modules:The socket includes many more methods for handling messages, chats, presence, business features, and more.

Basic usage

import makeWASocket, { useMultiFileAuthState } from '@whiskeysockets/baileys'

// Load or create authentication state
const { state, saveCreds } = await useMultiFileAuthState('auth_info')

// Create socket connection
const sock = makeWASocket({
  auth: state,
  printQRInTerminal: true
})

// Listen for connection updates
sock.ev.on('connection.update', (update) => {
  const { connection, lastDisconnect } = update
  
  if(connection === 'close') {
    console.log('Connection closed')
  } else if(connection === 'open') {
    console.log('Connection opened')
  }
})

// Save credentials when updated
sock.ev.on('creds.update', saveCreds)

Usage with custom configuration

import makeWASocket, { 
  DisconnectReason,
  useMultiFileAuthState 
} from '@whiskeysockets/baileys'
import pino from 'pino'

const { state, saveCreds } = await useMultiFileAuthState('auth_info')

const sock = makeWASocket({
  auth: state,
  logger: pino({ level: 'info' }),
  printQRInTerminal: true,
  // Browser metadata
  browser: ['MyApp', 'Chrome', '1.0.0'],
  // Custom timeouts
  connectTimeoutMs: 60000,
  defaultQueryTimeoutMs: 60000,
  // Mark as online when connected
  markOnlineOnConnect: true,
  // Sync full chat history
  syncFullHistory: false,
  // Message handling
  getMessage: async (key) => {
    // Retrieve message from your database
    return await getMessageFromDB(key)
  }
})

sock.ev.on('creds.update', saveCreds)

sock.ev.on('connection.update', (update) => {
  const { connection, lastDisconnect } = update
  
  if(connection === 'close') {
    const shouldReconnect = 
      lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut
    
    if(shouldReconnect) {
      // Reconnect if not logged out
      makeWASocket(config)
    }
  }
})

// Listen for new messages
sock.ev.on('messages.upsert', async ({ messages }) => {
  for(const msg of messages) {
    console.log('Received message:', msg)
  }
})

Event handling

The socket’s ev property is an event emitter that fires various events:
sock.ev.on('connection.update', (update) => {
  console.log('Connection status:', update.connection)
})
The socket merges defaults from DEFAULT_CONNECTION_CONFIG with your provided configuration. Only the auth property is required; all other settings have sensible defaults.

See also

Build docs developers (and LLMs) love