> For the complete documentation index, see [llms.txt](https://mrbarnk.gitbook.io/waabot-api-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mrbarnk.gitbook.io/waabot-api-documentation/sdks/nodejs-sdk.md).

# Nodejs SDK

### Installation

You can install the Waabot SDK using npm, yarn, or pnpm.

#### Using npm

```sh
npm install waabot-v2-sdk
```

#### Using yarn

```sh
yarn add waabot-v2-sdk
```

#### Using pnpm

```sh
pnpm add waabot-v2-sdk
```

### Getting Started

#### Initializing the SDK

First, instantiate the SDK with your `apiKey` and `apiSecret`.

```javascript
require('dotenv').config();

const { Waabot } = require('waabot-v2-sdk');

const apiKey = process.env.apiKey;
const apiSecret = process.env.apiSecret;
const to = process.env.to;

const app = new Waabot({ apiKey, apiSecret });
```

### Listening to New Messages

You can listen to new messages with the following code:

```javascript
app.socket.onMessage((message) => {
    const content = message.messages[0];
    const contact = message.contacts[0];

    console.log({ contact, content });
});
```

The interface of the message is as follows:

```typescript
export interface WebhookWhatsAppMessage {
    messaging_product: "whatsapp";
    metadata: {
        display_phone_number: string;
        phone_number_id: string;
    };
    contacts: { profile: { name: string }; wa_id: string }[];
    messages: WhatsAppMessageEntry[];
}

export interface WhatsAppMessageEntry {
    text?: { body: string };
    context: { from: string; id: string };
    from: string;
    id: string;
    timestamp: string;
    type: "interactive";
    interactive?: {
        type: "button_reply";
        button_reply: { id: string; title: string };
    };
}
```

### Sending a Text Message

To send a text message, use the following code:

```javascript
await app.message.sendText({
    recipient: to,
    type: 'TEXT',
    body: {
        text: { body: 'hello' }
    }
});
```

### Sending Reply Buttons

To send a message with reply buttons, use the following code:

```javascript
await app.message.sendReplyButtons({
    recipient: to,
    body: {
        interactive: {
            type: 'button',
            body: {
                text: 'hello3',
            },
            action: {
                buttons: [{
                    type: 'reply',
                    reply: {
                        id: 'greetings',
                        title: 'Good Morning'
                    }
                }]
            }
        }
    }
});
```

### Creating a Template

To create a template, use the following code:

```javascript
await app.template.create({
    name: "utility_test_1",
    category: 'UTILITY',
    language: 'en_US',
    components: [
        {
            type: "BODY",
            text: "Thank you for your order, {{1}}! Your confirmation number is {{2}}. If you have any questions, please use the buttons below to contact support. Thank you for being a customer!",
            example: {
                body_text: [
                    ["Pablo", "860198-230332"]
                ]
            }
        },
        {
            type: "BUTTONS",
            buttons: [
                {
                    type: 'PHONE_NUMBER',
                    text: "Call",
                    phone_number: "2348179803743"
                },
                {
                    type: 'URL',
                    text: "Contact Support",
                    url: "https://www.luckyshrub.com/support"
                }
            ]
        }
    ]
});
```

### Sending a Template

To send a template message, use the following code:

```javascript
await app.message.sendTemplate({
    recipient: to,
    type: 'TEMPLATE',
    body: {
        template: {
            name: "utility_test_2",
            language: {
                code: 'en_US'
            },
            components: [
                {
                    type: 'body',
                    parameters: [
                        {
                            type: 'text',
                            text: 'Bankole'
                        },
                        {
                            type: 'text',
                            text: '860198-230332'
                        }
                    ]
                }
            ]
        }
    }
});
```

These examples should help you get started with the Waabot SDK for Node.js. For more information, please refer to the official documentation at [npmjs.com/package/waabot-v2-sdk](https://www.npmjs.com/package/waabot-v2-sdk).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mrbarnk.gitbook.io/waabot-api-documentation/sdks/nodejs-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
