Getting Started with Zavu's Messaging API
Zavu provides a unified API that lets you send messages across multiple channels—SMS, WhatsApp, and Email—with a single integration. In this guide, we'll walk through the basic setup and send your first message.
Prerequisites
Before you begin, make sure you have:- A Zavu account with API credentials
- Node.js 18+ or Bun installed
- Basic knowledge of JavaScript/TypeScript
Installation
Install the Zavu SDK using your preferred package manager:
bashnpm install @zavudev/sdk
Or with bun:
bashbun add @zavudev/sdk
Initialize the Client
Create a new file and initialize the Zavu client with your API key:
typescriptimport Zavudev from '@zavudev/sdk'; const client = new Zavudev({ apiKey: process.env.ZAVUDEV_API_KEY, });
Sending Your First Message
Now let's send a simple SMS message:
typescriptasync function sendWelcomeMessage() { try { const response = await client.messages.send({ to: '+1234567890', text: 'Welcome to Zavu!', channel: 'sms', }); console.log('Message ID:', response.message.id); console.log('Status:', response.message.status); console.log('Channel:', response.message.channel); } catch (error) { console.error('Failed to send message:', error); } } sendWelcomeMessage();
Sending WhatsApp Messages
Send messages via WhatsApp (within 24h conversation window):
typescriptconst response = await client.messages.send({ to: '+1234567890', text: 'Your order has been shipped!', channel: 'whatsapp', }); console.log('WhatsApp message sent:', response.message.id);
Adding Metadata
Track messages with custom metadata:
typescriptconst response = await client.messages.send({ to: '+1234567890', text: 'Your verification code is 123456', channel: 'sms', metadata: { orderId: 'ORD-12345', userId: 'user_abc', }, });
Retrieving a Message
Get details about a sent message:
typescriptconst response = await client.messages.retrieve('message_id_here'); console.log('Message details:', { id: response.message.id, to: response.message.to, status: response.message.status, channel: response.message.channel, createdAt: response.message.createdAt, });
Listing Messages
List messages with filtering and pagination:
typescript// Get a page of messages const page = await client.messages.list({ limit: 10, channel: 'sms', status: 'delivered', }); console.log('Messages:', page.items); // Check for more pages if (page.hasNextPage()) { const nextPage = await page.getNextPage(); console.log('Next page:', nextPage.items); }
Async Iteration
Iterate through all messages using async iterators:
typescriptfor await (const message of client.messages.list({ limit: 5 })) { console.log('Message:', message.id, message.status); }
Error Handling
Handle API errors gracefully:
typescriptimport Zavudev from '@zavudev/sdk'; try { const response = await client.messages.send({ to: '+1234567890', text: 'Hello!', }); } catch (error) { if (error instanceof Zavudev.APIError) { console.error('API Error:', error.status, error.message); } else { throw error; } }
Next Steps
Now that you've sent your first message, explore these resources:
- API Reference: Complete API documentation
- Templates: Create reusable WhatsApp templates
- Broadcasts: Send messages to multiple recipients
- Contacts: Manage your contact database