whatsapp-bun
v1.0.1
Published
A lightweight, zero-dependency WhatsApp Business API client built for Bun and Next.js.
Readme
whatsapp-bun
A lightweight, zero-dependency WhatsApp Business API client built for Bun and Next.js.
Installation
bun add /whatsapp-bunSetup
You need credentials from the Meta App Dashboard. Add them to your .env file:
WHATSAPP_TOKEN="EAAG..."
WHATSAPP_PHONE_ID="12345..."
WHATSAPP_VERIFY_TOKEN="secret"Next.js App Router Example
Create a route handler at app/api/webhook/route.ts to handle both verification (GET) and incoming messages (POST).
import { WhatsAppClient } from "@emilianscheel/whatsapp";
const client = new WhatsAppClient({
token: process.env.WHATSAPP_TOKEN!,
phoneId: process.env.WHATSAPP_PHONE_ID!,
verifyToken: process.env.WHATSAPP_VERIFY_TOKEN!,
});
// 1. Webhook Verification (Handshake)
export async function GET(req: Request) {
return client.verifyWebhook(req);
}
// 2. Handle Incoming Messages
export async function POST(req: Request) {
const message = await client.parseMessage(req);
if (message) {
console.log(`New message from ${message.from}`);
// Handle Text
if (message.type === "text") {
if (message.content.toLowerCase() === "ping") {
await client.sendText(message.from, "Pong!");
}
}
// Handle Image
if (message.type === "image") {
const buffer = await client.downloadMedia(message.mediaId);
// ... save buffer to disk or S3 ...
await client.sendText(message.from, "Image received!");
}
}
return new Response("OK");
}