WhatsApp Service
The WhatsApp Service allows sending messages using the WhatsApp Business Cloud API.
Usage
Initialization
Initialize the WhatsAppService with your Meta app credentials.
import { WhatsAppService } from '@orbitus/core/services/whatsapp';
const whatsappService = new WhatsAppService('meta-cloud', {
phoneNumberId: process.env.WHATSAPP_PHONE_ID,
accessToken: process.env.WHATSAPP_TOKEN
});Sending a Text Message
await whatsappService.sendMessage({
to: '905551234567', // Phone number with country code
type: 'text',
text: { body: 'Hello! Your order is ready.' }
});Sending a Template Message
WhatsApp requires using approved templates for business-initiated conversations.
await whatsappService.sendMessage({
to: '905551234567',
type: 'template',
template: {
name: 'order_update',
language: { code: 'en_US' },
components: [
{
type: 'body',
parameters: [
{ type: 'text', text: 'ORD-123' }, // Variable {{1}}
{ type: 'text', text: 'Shipped' } // Variable {{2}}
]
}
]
}
});Configuration
Environment Variables
| Variable | Description |
|---|---|
WHATSAPP_PHONE_ID | The Phone Number ID from your WhatsApp Business API setup. |
WHATSAPP_TOKEN | A permanent or temporary access token for the API. |
Requirements
- A Meta Developer account.
- A configured WhatsApp Business App.
- A verified phone number.
Last updated on