Pre-assign a Zavu phone number that the client will register with WhatsApp:
Copy
// First, purchase a phone numberconst phoneNumber = await zavu.phoneNumbers.purchase({ phoneNumber: '+14155551234', name: 'Client: Acme Corp',});// Then create the invitation with the phone numberconst invitation = await zavu.invitations.create({ clientName: 'Acme Corp', clientEmail: 'contact@acme.com', phoneNumberId: phoneNumber.id, expiresInDays: 14,});console.log('Invitation URL:', invitation.url);
When you pre-assign a phone number, the invitation page will show the number and automatically display the verification code when it’s received via phone call.
Integrate WhatsApp setup into your SaaS onboarding:
Copy
async function onboardNewCustomer(customer: Customer) { // 1. Create the customer in your system await createCustomer(customer); // 2. Create a Zavu invitation const invitation = await zavu.invitations.create({ clientName: customer.companyName, clientEmail: customer.email, expiresInDays: 14, }); // 3. Send onboarding email with the invitation link await sendEmail({ to: customer.email, subject: 'Connect Your WhatsApp Business', body: ` Welcome to our platform! To enable WhatsApp messaging, please complete the setup: ${invitation.url} This link expires in 14 days. `, }); // 4. Store the invitation ID for tracking await saveInvitationId(customer.id, invitation.id);}
async function checkInvitationStatus(invitationId: string) { const invitation = await zavu.invitations.get({ invitationId }); switch (invitation.status) { case 'completed': console.log('Client connected! Sender ID:', invitation.senderId); // Start sending messages through this sender break; case 'pending': case 'in_progress': console.log('Still waiting for client to complete setup'); break; case 'expired': console.log('Invitation expired, creating new one...'); // Create a new invitation break; case 'cancelled': console.log('Invitation was cancelled'); break; } return invitation;}
Set up a webhook to be notified when invitations are completed:
Copy
// In your webhook handlerapp.post('/webhooks/zavu', async (req, res) => { const event = req.body; if (event.type === 'sender.created') { // A new sender was created (possibly from an invitation) const sender = event.data; console.log('New sender:', sender.id, sender.phoneNumber); // Update your system await updateCustomerWhatsAppStatus(sender.phoneNumber, 'connected'); } res.status(200).send('OK');});