{"id":16763,"library":"baileys-redis-auth","title":"Baileys Redis Authentication State Handler","description":"baileys-redis-auth is a specialized JavaScript/TypeScript library designed to provide robust Redis-based authentication state storage for Baileys, the popular WhatsApp Web API wrapper. Currently at version 2.0.1, this library primarily targets Baileys v7, with major releases typically coinciding with significant Baileys version updates, indicating a focused and stable release cadence. It differentiates itself by offering seamless session persistence, allowing Baileys applications to resume connections without repeated QR code scanning. Key features include flexible storage options, supporting both simple key-value pairs and the more efficient Redis Hashes (HSET) for optimized management of multiple Baileys sessions under distinct namespaces. This makes it particularly suitable for scalable applications requiring reliable session management, requiring Node.js 18+ and a running Redis server instance.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/hbinduni/baileys-redis-auth","tags":["javascript","baileys","redis","auth","typescript"],"install":[{"cmd":"npm install baileys-redis-auth","lang":"bash","label":"npm"},{"cmd":"yarn add baileys-redis-auth","lang":"bash","label":"yarn"},{"cmd":"pnpm add baileys-redis-auth","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This library acts as an authentication handler for Baileys sessions and requires Baileys itself as a peer dependency for its functionality.","package":"baileys","optional":false}],"imports":[{"note":"The library is ESM-only since v2.0.0; CommonJS require() is not supported.","wrong":"const { useRedisAuthStateWithHSet } = require('baileys-redis-auth')","symbol":"useRedisAuthStateWithHSet","correct":"import { useRedisAuthStateWithHSet } from 'baileys-redis-auth'"},{"note":"Used for programmatic deletion of specific session data from Redis. Requires ESM import.","wrong":"const { deleteHSetKeys } = require('baileys-redis-auth')","symbol":"deleteHSetKeys","correct":"import { deleteHSetKeys } from 'baileys-redis-auth'"}],"quickstart":{"code":"import { useRedisAuthStateWithHSet } from 'baileys-redis-auth'\nimport { RedisOptions } from 'ioredis'\nimport makeWASocket, { DisconnectReason } from 'baileys'\nimport { Boom } from '@hapi/boom'\n\n// Define your Redis connection options\nconst redisOptions: RedisOptions = {\n  host: 'localhost',\n  port: 6379\n  // password: process.env.REDIS_PASSWORD ?? '', // Uncomment if your Redis has a password\n}\n\n// Define a unique session identifier for this Baileys session\nconst sessionId = 'my_baileys_session_example'\n\nasync function initializeBaileysWithHSet() {\n  const { state, saveCreds, redis: authRedisInstance } = await useRedisAuthStateWithHSet(\n    redisOptions,\n    sessionId,\n    console.log // Optional: pass logger for Redis connection events\n  )\n\n  authRedisInstance.on('connect', () =>\n    console.log(`Redis (from hook) connected for session: ${sessionId}`)\n  )\n  authRedisInstance.on('error', (err) =>\n    console.error(`Redis (from hook) error for session ${sessionId}:`, err)\n  )\n\n  const sock = makeWASocket({\n    auth: state,\n    printQRInTerminal: true\n  })\n\n  sock.ev.on('creds.update', saveCreds)\n\n  sock.ev.on('connection.update', (update) => {\n    const { connection, lastDisconnect } = update\n    if (connection === 'close') {\n      const shouldReconnect = (lastDisconnect?.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut\n      console.log('connection closed due to ', lastDisconnect?.error, ', reconnecting ', shouldReconnect)\n      // reconnect if not logged out\n      if (shouldReconnect) {\n        initializeBaileysWithHSet()\n      }\n    } else if (connection === 'open') {\n      console.log('opened connection')\n    }\n  })\n\n  // You can now use 'sock' for Baileys operations\n  console.log('Baileys initialized with Redis HSet state for session:', sessionId)\n}\n\ninitializeBaileysWithHSet().catch(err => console.error('Failed to initialize Baileys:', err))\n","lang":"typescript","description":"This quickstart demonstrates how to initialize Baileys with Redis authentication state using `useRedisAuthStateWithHSet`, set up basic connection event handling, and persist credentials."},"warnings":[{"fix":"Ensure your project is configured for ES modules (e.g., add `\"type\": \"module\"` to your `package.json`) and use `import` statements for all package imports.","message":"Version 2.0.0 and above are ESM-only modules. CommonJS (require()) syntax is no longer supported and will lead to runtime errors.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Run `npm install baileys-redis-auth baileys` or `yarn add baileys-redis-auth baileys` to ensure both packages are installed.","message":"Baileys is now a peer dependency. It must be explicitly installed in your project alongside `baileys-redis-auth`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Update all your Baileys import statements from `import { ... } from '@whiskeysockets/baileys'` to `import { ... } from 'baileys'`.","message":"The official Baileys package name changed from `@whiskeysockets/baileys` to `baileys` with its v7 release. This library aligns with the new package name.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure your Redis server is running and accessible from your application's environment. Verify `host`, `port`, and `password` in your `redisOptions`.","message":"A running Redis server instance is a prerequisite. The library will fail to connect if Redis is not available or if connection details are incorrect.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change your import statements to ES module syntax (e.g., `import { useRedisAuthStateWithHSet } from 'baileys-redis-auth'`) and ensure your `package.json` contains `\"type\": \"module\"`.","cause":"Attempting to use CommonJS `require()` syntax with `baileys-redis-auth` which is an ESM-only package since v2.0.0.","error":"ReferenceError: require is not defined"},{"fix":"Install Baileys using your package manager: `npm install baileys` or `yarn add baileys`.","cause":"The `baileys` package is a peer dependency but has not been installed in your project.","error":"Error: Cannot find package 'baileys' or its corresponding type declarations."},{"fix":"Verify that your Redis server is active and reachable. Check the `host`, `port`, and `password` provided in your `redisOptions` object.","cause":"The application failed to connect to the Redis server, likely because the server is not running, is inaccessible, or the connection details are incorrect.","error":"Error: connect ECONNREFUSED <ip_address>:<port>"},{"fix":"Ensure your bundler is correctly configured to handle ES Modules. Verify `\"type\": \"module\"` in `package.json` and check your bundler's resolution/transpilation settings for `node_modules`.","cause":"This often occurs in bundler environments (like Webpack or Vite) when mixing CommonJS and ESM or due to incorrect build configurations for ESM modules.","error":"TypeError: (0 , baileys_redis_auth__WEBPACK_IMPORTED_MODULE_0__.useRedisAuthStateWithHSet) is not a function"}],"ecosystem":"npm","meta_description":null}