DEV Community

Cover image for How I Built a Custom WhatsApp Cloud Partner App to Slash Marketing Costs
Akash Pattnaik
Akash Pattnaik

Posted on

How I Built a Custom WhatsApp Cloud Partner App to Slash Marketing Costs

Heya all ๐Ÿ‘‹ I am the CTO at a dessert chain startup. We use WhatsApp marketing messages regularly to push content. In India, the cost per marketing message is 0.86 INR.

Till last month, we were using authkey.io. While reliable, the costs were starting to stack up as we scaled.

What is authkey.io?

Authkey.io is a cloud-based CPaaS (Communication Platform as a Service) that provides APIs for businesses to automate and manage customer communications across multiple channels. It allows developers and companies to integrate messaging and identity verification into their apps, websites, or software, often using a "pay-as-you-go" pricing model.

The Problem: Fixed Costs for Failed Deliveries

Since we are a startup, cost matters to us! I started noticing somethingโ€”WhatsApp doesnโ€™t actually charge for all messages, only for messages that are delivered. However, third-party providers often charge a flat rate (like 0.95 INR) per message sent, regardless of status. On our scale of 2,000 users with a 50% failure rate, we were paying significantly more than necessary.

I realized I could save 60-70% of our marketing costs by building our own system directly on the WhatsApp Cloud API.

Feature Authkey.io (Third-Party) Custom Cloud API (WhatsApp Cost)
Cost per Message 0.95 INR (per message sent) 0.86 INR (per message delivered)
Messages Sent 2,000 2,000
Messages Delivered 1,000 1,000
Total Cost Calculation 2,000 sent * 0.95 INR 1,000 delivered * 0.86 INR
Total Cost 1,900 INR ๐Ÿ˜ญ 860 INR ๐Ÿ˜ƒ

The Architecture: Supabase, Next.js, and QStash

I built a customized solution using Supabase and a Next.js app deployed on Vercel. Leveraging serverless architecture was great, but I quickly hit the 5-minute execution limit on Vercel when broadcasting to thousands of users.

To solve this, I used QStashโ€™s message queues. Instead of one long request, I send a single request to QStash to split numbers into batches of 20. QStash then triggers 100 separate requests to my Vercel deployment, each taking about 50-60 seconds. This ensured no messages were missed due to timeouts.

What is QStash?

QStash is a serverless messaging and scheduling solution designed specifically for HTTP and serverless environments. Created by Upstash, it functions as a middleman (message broker) that takes HTTP requests and forwards them to your target API endpoints. Instead of calling an endpoint directly, you send the request to QStash, which then handles the heavy lifting of background tasks and API integrations.

Optimizing Webhooks with Next.js 15+ after()

Using a Meta webhook route, I receive status updates and incoming messages. Initially, I used a "Fire-And-Forget" architecture, but Vercel containers would sometimes close before processing finished. I transitioned to the new after() API in Next.js 15, which allows work to continue after the response is sent. This magically slashed response times and improved reliability.

Connect with me ๐Ÿ™

Top comments (0)