Documentation

Your First Message

A step-by-step guide to sending your first WhatsApp message with Node.js

Your First Message

This guide walks you through setting up the Chatbase API in a Node.js project and sending your first WhatsApp message end-to-end.


What You'll Build

A small Node.js script that:

  1. Authenticates with the Chatbase API
  2. Sends a WhatsApp template message to a phone number
  3. Handles the response and errors gracefully

Prerequisites

  • Node.js 18+
  • A Chatbase account with WhatsApp connected
  • An approved template (the built-in hello_world template works for testing)
  • Your API key from API Manage in the CRM dashboard

Project Setup

mkdir chatbase-first-message && cd chatbase-first-message
npm init -y
npm install axios dotenv

Create a .env file:

CHATBASE_API_KEY=wpapi_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CHATBASE_BASE_URL=https://chatbase.in/api/v1/whatsapp

The Code

Create send.js:

require('dotenv').config();
const axios = require('axios');

const api = axios.create({
  baseURL: process.env.CHATBASE_BASE_URL,
  headers: {
    Authorization: `Bearer ${process.env.CHATBASE_API_KEY}`,
    'Content-Type': 'application/json',
  },
});

async function sendFirstMessage(to) {
  try {
    const { data } = await api.post('/messages/send', {
      to,
      type: 'template',
      template: {
        name: 'hello_world',
        language: { code: 'en_US' },
      },
    });

    console.log('Message sent!');
    console.log('Message ID:', data.message_id);
    console.log('Status:', data.status);
  } catch (err) {
    if (err.response) {
      console.error('API Error:', err.response.status, err.response.data.message);
    } else {
      console.error('Network Error:', err.message);
    }
  }
}

// Replace with a real phone number in E.164 format (no +)
sendFirstMessage('919876543210');

Run it:

node send.js

Expected output:

Message sent!
Message ID: wamid.HBgLOTE5ODc2NTQzMjEwFQ...
Status: accepted

Understanding the Response

FieldDescription
successtrue = message accepted by WhatsApp servers
message_idUse this to track delivery via webhooks
statusaccepted means queued — not yet delivered

Delivery is asynchronous. The accepted status means Meta has received it. You'll get the actual delivered/read/failed status via a webhook or by polling the messages list.


Sending a Text Reply

Once a customer messages you, you have 24 hours to reply with free-form text:

await api.post('/messages/send', {
  to: '919876543210',
  type: 'text',
  text: { body: 'Thanks for reaching out! How can I help you today?' },
});

Error Handling

try {
  const { data } = await api.post('/messages/send', payload);
} catch (err) {
  const status = err.response?.status;
  const message = err.response?.data?.message;

  if (status === 401) console.error('Invalid API key');
  else if (status === 403) console.error('Feature not in plan:', message);
  else if (status === 429) console.error('Rate limit hit — wait and retry');
  else if (status === 422) console.error('Invalid phone number format');
  else console.error('Unexpected error:', message);
}

Next Steps