> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shardcloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Host WhatsApp Bots

> Learn how to create and host WhatsApp bots on Shard Cloud using whatsapp-web.js.

## Introduction

This guide covers creating and deploying WhatsApp bots on Shard Cloud using the whatsapp-web.js library.

<Warning>
  WhatsApp bots work by connecting to WhatsApp Web. Your phone must stay connected to the internet for the bot to work.
</Warning>

## Creating Your Bot

### Setup

```bash theme={null}
npm init -y
npm install whatsapp-web.js qrcode
```

### Basic Bot Code

```javascript index.js theme={null}
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode');

const client = new Client({
  authStrategy: new LocalAuth(),
  puppeteer: {
    headless: true,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
    ],
  },
});

client.on('qr', async (qr) => {
  try {
    await qrcode.toFile('./qrcode.png', qr);
    console.log('QR Code saved as qrcode.png');
  } catch (err) {
    console.error('Error generating QR:', err);
  }
});

client.on('ready', () => {
  console.log('WhatsApp client is ready!');
});

client.on('message', async (msg) => {
  if (msg.body === '!ping') {
    await msg.reply('Pong!');
  }
});

client.initialize();
```

### Package.json

```json package.json theme={null}
{
  "name": "whatsapp-bot",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "whatsapp-web.js": "^1.23.0",
    "qrcode": "^1.5.0"
  }
}
```

## Authentication

When you first run the bot, it will generate a QR code (`qrcode.png`). You need to:

1. Download the QR code from Shard Cloud's file manager
2. Scan it with WhatsApp on your phone (Settings → Linked Devices → Link a Device)
3. Once authenticated, the session is saved and you won't need to scan again

<Note>
  The `LocalAuth` strategy saves the session in a `.wwebjs_auth` folder. This persists between restarts.
</Note>

## Shard Cloud Configuration

```systemd .shardcloud theme={null}
DISPLAY_NAME=WhatsApp Bot
DESCRIPTION=WhatsApp Bot with whatsapp-web.js
MAIN=index.js
MEMORY=1024
VERSION=recommended
```

<Warning>
  WhatsApp bots require more memory due to Puppeteer. Use at least 512MB-1024MB.
</Warning>

## Deploying

<Steps>
  <Step title="Prepare Your Files">
    Ensure you have:

    * `index.js`
    * `package.json`
    * `.shardcloud`
  </Step>

  <Step title="Exclude Unnecessary Files">
    Remove: `node_modules/`, `package-lock.json`
  </Step>

  <Step title="Create ZIP Archive">
    Compress your project folder.
  </Step>

  <Step title="Upload to Shard Cloud">
    Go to [Shard Cloud Dashboard](https://shardcloud.app/dash/applications) and upload.
  </Step>

  <Step title="Authenticate">
    After first start, download the QR code from the file manager and scan with WhatsApp.
  </Step>
</Steps>

## Additional Resources

Visit the [whatsapp-web.js documentation](https://wwebjs.dev/) for more information.

## Troubleshooting

<AccordionGroup>
  <Accordion title="QR code not generating">
    * Ensure Puppeteer has enough memory (1024MB+)
    * Check logs for Chromium errors
  </Accordion>

  <Accordion title="Session expires frequently">
    * Ensure your phone stays connected to the internet
    * Keep the WhatsApp app open on your phone
  </Accordion>

  <Accordion title="Chromium errors">
    Make sure you're using the `--no-sandbox` and `--disable-setuid-sandbox` flags.
  </Accordion>
</AccordionGroup>
