> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/whiskeysockets/Baileys/llms.txt
> Use this file to discover all available pages before exploring further.

# Generic Utilities

> General-purpose utility functions for Baileys including JSON serialization, timing, and key operations

Generic utilities provide common functionality needed throughout Baileys, including Buffer serialization, timing functions, and message key operations.

## BufferJSON

JSON serialization utilities for handling Buffers and Uint8Arrays.

```typescript theme={null}
export const BufferJSON = {
  replacer: (k: any, value: any) => any
  reviver: (_: any, value: any) => any
}
```

### replacer

Converts Buffers to JSON-serializable objects.

<ParamField path="k" type="any">
  The key being stringified
</ParamField>

<ParamField path="value" type="any">
  The value being stringified
</ParamField>

<ResponseField name="return" type="any">
  JSON-serializable representation of the value
</ResponseField>

### reviver

Converts JSON objects back to Buffers.

<ParamField path="_" type="any">
  The key (unused)
</ParamField>

<ParamField path="value" type="any">
  The value being parsed
</ParamField>

<ResponseField name="return" type="any">
  Reconstructed Buffer or original value
</ResponseField>

**Example:**

```typescript theme={null}
import { BufferJSON } from '@whiskeysockets/baileys'

// Object with Buffer
const data = {
  name: 'test',
  buffer: Buffer.from('hello world')
}

// Serialize with replacer
const json = JSON.stringify(data, BufferJSON.replacer)
console.log(json)
// {"name":"test","buffer":{"type":"Buffer","data":"aGVsbG8gd29ybGQ="}}

// Deserialize with reviver
const restored = JSON.parse(json, BufferJSON.reviver)
console.log(restored.buffer) // <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
console.log(restored.buffer.toString()) // "hello world"

// Works with nested objects
const nested = {
  user: {
    id: '123',
    keys: {
      publicKey: Buffer.from([1, 2, 3, 4])
    }
  }
}

const jsonNested = JSON.stringify(nested, BufferJSON.replacer)
const restoredNested = JSON.parse(jsonNested, BufferJSON.reviver)
console.log(restoredNested.user.keys.publicKey) // <Buffer 01 02 03 04>
```

**When to use:**

* When storing authentication state to JSON files
* For serializing keys and cryptographic data
* With `useMultiFileAuthState` or custom auth state handlers
* Any time you need to JSON.stringify/parse objects containing Buffers

***

## getKeyAuthor

Extracts the author/sender JID from a message key.

```typescript theme={null}
export const getKeyAuthor = (
  key: WAMessageKey | undefined | null,
  meId?: string
): string
```

<ParamField path="key" type="WAMessageKey | undefined | null">
  The message key to analyze
</ParamField>

<ParamField path="meId" type="string" optional default="'me'">
  Your JID to use if the message is from you
</ParamField>

<ResponseField name="return" type="string">
  The JID of the message author
</ResponseField>

**Example:**

```typescript theme={null}
import { getKeyAuthor } from '@whiskeysockets/baileys'

// Message from you
const myMessageKey = {
  remoteJid: '1234567890@s.whatsapp.net',
  fromMe: true,
  id: 'ABC123'
}

const author1 = getKeyAuthor(myMessageKey, 'me@s.whatsapp.net')
console.log(author1) // "me@s.whatsapp.net"

// Message from another user
const theirMessageKey = {
  remoteJid: '1234567890@s.whatsapp.net',
  fromMe: false,
  id: 'DEF456'
}

const author2 = getKeyAuthor(theirMessageKey)
console.log(author2) // "1234567890@s.whatsapp.net"

// Group message
const groupMessageKey = {
  remoteJid: '123456789@g.us',
  fromMe: false,
  participant: '9876543210@s.whatsapp.net',
  id: 'GHI789'
}

const author3 = getKeyAuthor(groupMessageKey)
console.log(author3) // "9876543210@s.whatsapp.net"
```

**When to use:**

* To identify who sent a message
* For displaying sender information in UI
* When processing reactions, poll updates, or event responses
* Handles group messages with participants correctly

***

## unixTimestampSeconds

Converts a Date to Unix timestamp in seconds.

```typescript theme={null}
export const unixTimestampSeconds = (date?: Date): number
```

<ParamField path="date" type="Date" optional default="new Date()">
  The date to convert (defaults to current time)
</ParamField>

<ResponseField name="return" type="number">
  Unix timestamp in seconds
</ResponseField>

**Example:**

```typescript theme={null}
import { unixTimestampSeconds } from '@whiskeysockets/baileys'

// Current timestamp
const now = unixTimestampSeconds()
console.log(now) // 1699564800

// Specific date
const specificDate = new Date('2024-01-01T00:00:00Z')
const timestamp = unixTimestampSeconds(specificDate)
console.log(timestamp) // 1704067200

// Use in message
const messageTimestamp = unixTimestampSeconds()
await sock.sendMessage(jid, {
  text: 'Hello',
  timestamp: messageTimestamp
})
```

**When to use:**

* When creating messages (for messageTimestamp)
* For comparing message times
* Any time you need Unix timestamps for WhatsApp protocol

***

## delay

Creates a promise that resolves after a specified time.

```typescript theme={null}
export const delay = (ms: number): Promise<void>
```

<ParamField path="ms" type="number" required>
  Milliseconds to delay
</ParamField>

<ResponseField name="return" type="Promise<void>">
  Promise that resolves after the delay
</ResponseField>

**Example:**

```typescript theme={null}
import { delay } from '@whiskeysockets/baileys'

// Wait 1 second
await delay(1000)
console.log('1 second has passed')

// Use in message sending
for (const recipient of recipients) {
  await sock.sendMessage(recipient, { text: 'Hello' })
  await delay(500) // Wait 500ms between messages
}

// Retry with delay
async function sendWithRetry(jid, content) {
  for (let i = 0; i < 3; i++) {
    try {
      await sock.sendMessage(jid, content)
      return
    } catch (err) {
      if (i < 2) {
        await delay(1000 * (i + 1)) // Exponential backoff
      }
    }
  }
}
```

**When to use:**

* To add delays between operations
* For rate limiting message sending
* In retry logic with backoff
* To avoid triggering WhatsApp's anti-spam

***

## generateRegistrationId

Generates a random 14-bit registration ID for Signal protocol.

```typescript theme={null}
export const generateRegistrationId = (): number
```

<ResponseField name="return" type="number">
  Random 14-bit number (0-16383)
</ResponseField>

**Example:**

```typescript theme={null}
import { generateRegistrationId } from '@whiskeysockets/baileys'

const regId = generateRegistrationId()
console.log(regId) // Random number between 0 and 16383

// Used internally during auth state initialization
const authState = {
  creds: {
    registrationId: generateRegistrationId(),
    // ... other credentials
  }
}
```

**When to use:**

* Automatically used when creating new authentication credentials
* You rarely need to call this directly
* Part of the Signal protocol setup

***

## fetchLatestBaileysVersion

Fetches the latest Baileys version from GitHub.

```typescript theme={null}
export const fetchLatestBaileysVersion = async (
  options?: RequestInit
): Promise<{
  version: WAVersion
  isLatest: boolean
  error?: any
}>
```

<ParamField path="options" type="RequestInit" optional>
  Fetch options (headers, dispatcher, etc.)
</ParamField>

<ResponseField name="return" type="object">
  Object containing version array, isLatest flag, and optional error
</ResponseField>

**Example:**

```typescript theme={null}
import { fetchLatestBaileysVersion, makeWASocket } from '@whiskeysockets/baileys'

// Fetch and use latest version
const { version, isLatest } = await fetchLatestBaileysVersion()
console.log('Version:', version) // [2, 3000, 1033846690]
console.log('Is latest:', isLatest) // true

const sock = makeWASocket({
  version,
  // ... other options
})

// With custom fetch options
const { version: v2 } = await fetchLatestBaileysVersion({
  headers: {
    'User-Agent': 'MyBot/1.0'
  }
})
```

**When to use:**

* To ensure your bot uses the latest WhatsApp Web version
* Recommended for production bots
* Call once on startup

***

## fetchLatestWaWebVersion

Fetches the latest WhatsApp Web version directly from WhatsApp servers.

```typescript theme={null}
export const fetchLatestWaWebVersion = async (
  options?: RequestInit
): Promise<{
  version: WAVersion
  isLatest: boolean
  error?: any
}>
```

<ParamField path="options" type="RequestInit" optional>
  Fetch options
</ParamField>

<ResponseField name="return" type="object">
  Object containing version array, isLatest flag, and optional error
</ResponseField>

**Example:**

```typescript theme={null}
import { fetchLatestWaWebVersion, makeWASocket } from '@whiskeysockets/baileys'

// Fetch latest WA Web version
const { version, isLatest } = await fetchLatestWaWebVersion()
console.log('WA Web Version:', version) // [2, 3000, 1033846690]

const sock = makeWASocket({
  version,
  // ... other options
})
```

**When to use:**

* Alternative to `fetchLatestBaileysVersion`
* Gets version directly from WhatsApp instead of Baileys repo
* Use if you want the absolute latest WA Web version

***

## toNumber

Converts Long or number types to JavaScript number.

```typescript theme={null}
export const toNumber = (
  t: Long | number | null | undefined
): number
```

<ParamField path="t" type="Long | number | null | undefined">
  Value to convert (from protobuf Long type)
</ParamField>

<ResponseField name="return" type="number">
  JavaScript number (returns 0 for null/undefined)
</ResponseField>

**Example:**

```typescript theme={null}
import { toNumber } from '@whiskeysockets/baileys'

// Convert protobuf Long
const timestamp = message.messageTimestamp // Long type
const timestampNum = toNumber(timestamp)
console.log(timestampNum) // 1699564800

// Handles null/undefined
const nullValue = toNumber(null)
console.log(nullValue) // 0

// Regular numbers pass through
const regularNum = toNumber(12345)
console.log(regularNum) // 12345
```

**When to use:**

* When working with protobuf message fields
* To convert timestamps from messages
* Handles Long type from WAProto definitions
