Skip to main content

WhatsappResource

The whatsapp resource provides methods for sending WhatsApp messages, managing typing indicators, and reactions.

send()

await contiguity.whatsapp.send(params: WhatsappSendParams): Promise<{ message_id: string }>
Send a WhatsApp message to a recipient.
params
WhatsappSendParams
required
message_id
string
Unique identifier for the sent message.
const result = await contiguity.whatsapp.send({
  to: '+1234567890',
  from: '+0987654321',
  message: 'Hello via WhatsApp!',
  attachments: ['https://example.com/document.pdf'],
  fallback: {
    when: ['whatsapp_unsupported', 'whatsapp_fails'],
    from: '+0987654321'
  }
});

console.log(result.message_id); // "msg_..."

reply()

await contiguity.whatsapp.reply(
  event: WebhookEventBase,
  params: Omit<WhatsappSendParams, 'to' | 'from'>
): Promise<{ message_id: string }>
Reply to an incoming WhatsApp webhook. Automatically fills to and from fields from the webhook event.
event
WebhookEventBase
required
The webhook event received from an incoming WhatsApp message.
params
Omit<WhatsappSendParams, 'to' | 'from'>
required
Message parameters (same as send() but without to and from).
message_id
string
Unique identifier for the sent reply.
app.post('/webhook', async (req, res) => {
  const event = contiguity.webhook.parse(req.body);
  
  if (event.type === 'whatsapp.incoming') {
    await contiguity.whatsapp.reply(event, {
      message: 'Thanks for your WhatsApp message!'
    });
  }
  
  res.sendStatus(200);
});

typing()

await contiguity.whatsapp.typing(params: WhatsappTypingParams): Promise<any>
Send a typing indicator to show that you’re composing a message.
params
WhatsappTypingParams
required
// Show typing indicator
await contiguity.whatsapp.typing({
  to: '+1234567890',
  from: '+0987654321',
  action: 'start'
});

// Simulate composing message...
await new Promise(resolve => setTimeout(resolve, 2000));

// Stop typing indicator and send message
await contiguity.whatsapp.typing({
  to: '+1234567890',
  from: '+0987654321',
  action: 'stop'
});

await contiguity.whatsapp.send({
  to: '+1234567890',
  from: '+0987654321',
  message: 'Here is my response!'
});

react()

await contiguity.whatsapp.react(
  action: 'add' | 'remove',
  params: WhatsappReactParams
): Promise<any>
Add or remove a reaction to a WhatsApp message.
action
'add' | 'remove'
required
Whether to add or remove the reaction.
params
WhatsappReactParams
required
// Add a reaction
await contiguity.whatsapp.react('add', {
  to: '+1234567890',
  reaction: 'πŸ‘',
  message: 'Thanks for the help!'
});

// Remove a reaction
await contiguity.whatsapp.react('remove', {
  to: '+1234567890',
  reaction: 'πŸ‘',
  message: 'Thanks for the help!'
});

Complete Example

import { Contiguity } from 'contiguity';

const contiguity = new Contiguity('contiguity_sk_...');

// Send a message with typing indicator
await contiguity.whatsapp.typing({
  to: '+1234567890',
  from: '+0987654321',
  action: 'start'
});

await new Promise(resolve => setTimeout(resolve, 1500));

const { message_id } = await contiguity.whatsapp.send({
  to: '+1234567890',
  from: '+0987654321',
  message: 'Hello! How can I help you today?',
  attachments: ['https://example.com/menu.pdf']
});

await contiguity.whatsapp.typing({
  to: '+1234567890',
  from: '+0987654321',
  action: 'stop'
});

console.log('Message sent:', message_id);

Build docs developers (and LLMs) love