WhatsApp Business API for first contact with a new lead
The scenario sends a new lead a personalised WhatsApp template as the first sales touch.
Use case overview
The scenario sends a new lead a personalised WhatsApp template as the first sales touch. The message includes the lead name, the specialist's name, and where the request came from. Quick-reply buttons let the lead tap «Yes, interested» or «Call me later»; taps arrive via webhook for routing to sales or scheduling.
Template example
Hello, {{1}}! My name is {{2}}, and I am following up on your inquiry from {{3}}. I am ready to tell you more and answer all your questions — choose a convenient option below.
[Yes, interested]
Quick-reply button labels are fixed in the Meta template — only body variables are sent via the API; taps return via webhook.
Variables and purpose
{{1}}— lead or customer name{{2}}— specialist or manager name introducing themselves{{3}}— lead source or request context (e.g. website form, Instagram ad, partner referral)
Filled-in example
Hello, Alexey! My name is Maria, and I am following up on your inquiry from the company website. I am ready to tell you more and answer all your questions — choose a convenient option below.
[Yes, interested]
When to use it
- lead generation
- b2b and services
- agencies
Business value
- CRM or form integration captures a new lead event
- System resolves lead phone, specialist name, and source label
- First-contact template is sent with two quick-reply engagement buttons
- Lead taps interest or callback-later — webhook routes to sales workflow
- Delivery and engagement are logged in CRM for timely follow-up
Workflow
- A new lead event is captured from a form, ad, or CRM.
- Lead phone, specialist name, and source label are resolved from the payload.
- A personalised first-contact template is sent with three body variables and two quick-reply buttons.
- The lead taps «Yes, interested» or «Call me later» — the event arrives via webhook for sales routing.
- Delivery and engagement are logged in CRM for follow-up.
- Delivery progress is reported asynchronously — typically
sent, thendelivered(or failed/undelivered). - Your system receives status via webhook (
hooks[]) or pollsGET …/hookInfo?messageId=<id>and handles failures if needed.
Technical implementation
Prerequisites
- An approved WhatsApp template with three body variables and two quick-reply buttons.
- A 1MSG account with template send API and a configured webhook for incoming messages.
- CRM or lead capture integration with phone, specialist, and source fields.
Code examples
Node.js
#!/usr/bin/env node
// === Configuration (replace "___" placeholders) ===
const API_BASE_URL = "https://api.1msg.io"; // production 1MSG API base URL
const CHANNEL_ID = "___"; // channel ID from 1MSG dashboard
const API_TOKEN = "___"; // channel JWT token (Bearer)
const TEMPLATE_NAME = "___"; // approved template name
const TEMPLATE_NAMESPACE = "___"; // template namespace (422 without it)
const TEMPLATE_LANGUAGE = "___"; // template language code, e.g. "en"
// === Test data ===
const TEST_PHONE = "___"; // client phone in international format
const TEST_LEADNAME = "___"; // {{1}} lead name
const TEST_SPECIALIST = "___"; // {{2}} specialist name
const TEST_LEADSOURCE = "___"; // {{3}} lead source
function normalizePhone(phone) {
return String(phone).replace(/\D/g, "");
}
function assertConfigured(values) {
for (const [key, value] of Object.entries(values)) {
if (value === "___" || value === "" || value === undefined || value === null) {
throw new Error(`Missing configuration value: ${key}`);
}
}
}
async function sendTemplateMessage({ phone, leadName, specialist, leadSource }) {
assertConfigured({
CHANNEL_ID,
API_TOKEN,
TEMPLATE_NAME,
TEMPLATE_NAMESPACE,
TEMPLATE_LANGUAGE,
phone,
leadName,
specialist,
leadSource,
});
const url = `${API_BASE_URL}/${CHANNEL_ID}/sendTemplate`;
// params carries body and button blocks (dynamic buttons).
const requestBody = {
phone: normalizePhone(phone),
template: TEMPLATE_NAME,
namespace: TEMPLATE_NAMESPACE,
language: {
policy: "deterministic",
code: TEMPLATE_LANGUAGE,
},
params: [
{
type: "body",
parameters: [
{ type: "text", text: String(leadName) }, // {{1}} lead name
{ type: "text", text: String(specialist) }, // {{2}} specialist name
{ type: "text", text: String(leadSource) }, // {{3}} lead source
],
},
],
};
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${API_TOKEN}`,
},
body: JSON.stringify(requestBody),
});
const raw = await res.text();
let data;
try {
data = JSON.parse(raw);
} catch {
data = null;
}
if (!res.ok || !data || data.sent !== true) {
console.error("Send failed. API response:");
console.error(raw);
process.exit(1);
}
console.log("Message sent to client.");
console.log("API response:", raw);
return data;
}
function handleIncomingMessage(message) {
const text = (message && (message.text || message.body || "")).trim();
// Meta quick_reply label (from template.txt) → route key (code_contract.ref) "interested"
if (text === "Да, интересно") {
console.log("Reply received: interested");
return { status: "interested" };
}
// Meta quick_reply label (from template.txt) → route key (code_contract.ref) "call_later"
if (text === "Перезвоните позже") {
console.log("Reply received: call_later");
return { status: "call_later" };
}
console.log("Free-text reply — handle separately.");
return { status: "other" };
}
if (require.main === module) {
sendTemplateMessage({
phone: TEST_PHONE,
leadName: TEST_LEADNAME,
specialist: TEST_SPECIALIST,
leadSource: TEST_LEADSOURCE,
}).catch((err) => {
console.error("Execution failed:", err.message);
process.exit(1);
});
}
module.exports = { sendTemplateMessage, handleIncomingMessage };
Immediate API response (synchronous)
- HTTP 2xx and JSON
"sent": truemean 1MSG accepted the message for sending — not that it already reached the customer's phone. - Save the `id` field from the response (value looks like
wamid.…). Use it to correlate delivery callbacks or polling. - The response may also include
messageanddescription— informational only.
Delivery status (asynchronous)
- Register a webhook (
POST …/webhook) so 1MSG POSTs delivery updates to your HTTPS endpoint in a separate `hooks[]` payload (sent,delivered,read, or failed/undelivered when applicable). - Optionally poll:
GET {base}/{channel}/hookInfo?messageId=<id from sendTemplate>. - In practice, delivery often completes within a few seconds — but that is not guaranteed by the API contract.
Common errors
- Invalid or non-normalized phone number
- Unapproved or missing template name / namespace
- No customer opt-in for WhatsApp business messages
- Template variable count mismatch (422 from API)
- Delivery failure — check status webhook and retry policy
FAQ
- Do I need an approved template? Yes — cold-start WhatsApp messages require a Meta-approved template.
- Can I customize the message text? Body variables are dynamic; fixed text and button labels are set in the Meta template.
- How do I check delivery?
sent: trueonly confirms acceptance. Track delivery via webhookhooks[]orGET …/hookInfo?messageId=<id>. - What if the message is not delivered? Log the failed/undelivered hook, verify opt-in and template status, then retry or fall back to another channel.
- Can I connect this to my CRM or backend? Yes — trigger the API call from your platform webhook or event handler.
CTA
Ready to use first contact with a new lead? Connect your 1MSG channel and run the code examples above.
Related
Build WhatsApp automation in minutes
Use 1MSG to automate this workflow and try it with our free demo.
