Skip to main content
WhatsApp Guides

WhatsApp Flow Dynamic Image Overlays: Serverless Webhook Guide

Priya Patel
9 min read
Views 0
Featured image for WhatsApp Flow Dynamic Image Overlays: Serverless Webhook Guide

WhatsApp Flow Dynamic Image Overlays: Serverless Webhook Guide

Personalization in automated messaging often determines whether a user stays engaged or abandons the interaction. While WhatsApp Flows provide a structured way to gather information, the results frequently rely on static text or generic templates. Static images do not scale for use cases like event tickets, personalized discount vouchers, or real-time account snapshots. High-volume support operations frequently experience user confusion when automated confirmations lack visual specific details.

I have managed support queues where generic confirmation messages caused hundreds of duplicate tickets. Users wanted to see their specific details on a visual asset. Dynamic image overlays solve this by injecting user-specific data onto a base image during the flow execution. This approach provides immediate visual confirmation and builds trust.

The Problem with Static Flow Assets

Standard WhatsApp templates and flows support media headers. These headers usually point to a static URL or a pre-uploaded media ID. If you need to show a customer a loyalty card with their current point balance, a static image fails. You would need to pre-generate millions of images or rely on text-only updates.

Pre-generating images is inefficient. It wastes storage space and creates a lag between data changes and visual updates. Relying on text alone lacks the professional finish users expect from premium brands. A serverless webhook approach generates the visual asset at the exact moment the user needs it. This ensures the data is accurate and the experience feels integrated.

Prerequisites for Dynamic Image Manipulation

Before implementing this architecture, ensure you have the following components ready.

  1. WhatsApp Business API or WASenderApi: You need a way to send and receive flow data. If you prefer to avoid the complexities of official template approvals for rapid prototyping, WASenderApi offers a session-based alternative that handles media through real-time webhooks.
  2. Serverless Environment: Use AWS Lambda, Vercel Functions, or Google Cloud Functions. These platforms scale to zero when not in use and handle spikes in traffic during marketing broadcasts.
  3. Image Processing Library: Sharp is the industry standard for Node.js. It is fast and has a small memory footprint.
  4. Cloud Storage: An S3-compatible bucket (like AWS S3 or Cloudflare R2) to host the generated images.
  5. WhatsApp Flow: A configured flow with a data_exchange action.

Step 1: Architecting the Data Exchange

The process begins when a user interacts with your WhatsApp Flow. The flow sends a POST request to your webhook URL. This request contains the user's input.

{
  "version": "3.0",
  "action": "data_exchange",
  "screen": "SCREEN_ID",
  "data": {
    "user_name": "Priya Patel",
    "membership_level": "Gold",
    "points_balance": "1250",
    "expiry_date": "2025-12-31"
  },
  "flow_token": "flow_token_12345"
}

Your serverless function must intercept this payload. It extracts the values needed for the overlay. In this example, the name, level, and points are the dynamic elements.

Step 2: Serverless Image Manipulation Logic

The core logic involves three layers: the base image, the dynamic text layer (rendered as an SVG), and the composition step.

Sharp allows you to composite multiple layers. Using an SVG for the text layer is the most reliable way to handle fonts and positioning without complex coordinate math.

const sharp = require('sharp');

async function generateOverlay(userData) {
  const width = 800;
  const height = 400;

  const svgText = `
    <svg width="${width}" height="${height}">
      <style>
        .name { fill: #ffffff; font-size: 40px; font-family: Arial; font-weight: bold; }
        .points { fill: #ffd700; font-size: 60px; font-family: Arial; }
      </style>
      <text x="50" y="100" class="name">${userData.user_name}</text>
      <text x="50" y="200" class="points">${userData.points_balance} Points</text>
    </svg>`;

  const svgBuffer = Buffer.from(svgText);

  return sharp('base_card.png')
    .composite([{ input: svgBuffer, top: 0, left: 0 }])
    .png()
    .toBuffer();
}

This function creates a buffer. This buffer is the actual image file. It exists in memory and is ready for upload.

Step 3: Hosting and Returning the Asset

WhatsApp requires a public URL or a media ID to display an image. Once the image is generated, upload it to your cloud storage. Set a lifecycle policy on the bucket to delete images after 24 hours. This keeps storage costs low and respects data privacy.

After the upload, your webhook must respond to the WhatsApp Flow request. You have two options. You can update the current flow screen with the image URL or finish the flow and send the image as a standalone message.

If using the official API, you might need to upload the file to the WhatsApp media endpoint to get a media_id. If using WASenderApi, sending the public URL in a message via the API is often faster for real-time delivery.

Practical Use Cases

Personalized Discount Vouchers

Instead of a text code, send a high-quality graphic. Include the user's name and an expiration date. This makes the offer feel exclusive. Support teams see fewer questions about validity when the expiration date is clearly visible on the voucher image.

Event Registration Passes

When a user registers for an event via a Flow, generate a pass immediately. Overlay a QR code containing their unique registration ID. Users save these images to their galleries for easy access at the venue.

Financial Snapshots

For banking or loyalty apps, show a visual representation of a balance or a monthly spending chart. Visuals are easier to process than lists of numbers.

Handling Latency and Edge Cases

WhatsApp Flow webhooks have a 10-second timeout. Image manipulation and cloud uploads must happen within this window. If the process takes longer, the user sees an error.

To optimize performance, keep your base images small. Use a content delivery network (CDN) for fast asset fetching. If the generation logic is heavy, acknowledge the flow request immediately with a "Processing" screen and send the image asynchronously as a separate message once it is ready.

Font rendering is another common hurdle. Serverless environments like AWS Lambda do not always have standard fonts installed. You must package your font files with your deployment or use a library that supports loading fonts from a buffer.

Troubleshooting Flow Image Errors

If images fail to appear, check these three common failure points.

  1. CORS and Permissions: Ensure your storage bucket allows public read access for the generated URL. If WhatsApp cannot fetch the file, the message will fail.
  2. SVG Syntax: A single missing closing tag in your SVG string will cause Sharp to throw an error. Log your generated SVG strings during development to verify their structure.
  3. Memory Limits: Serverless functions have memory constraints. Processing a 10MB image in a 128MB Lambda function will cause an out-of-memory error. Aim for base images under 1MB.

FAQ

Do I need a separate server for this? No. Serverless functions are sufficient. They only run when a request arrives. This is cost-effective for most businesses.

Which image formats are supported? WhatsApp supports JPEG and PNG. PNG is better for overlays because it supports transparency and keeps text crisp.

Can I use this for dynamic QR codes? Yes. Use a library like qrcode in Node.js to generate a buffer. Pass that buffer to Sharp as another overlay layer.

Is there a limit to how many images I can generate? Your limits depend on your serverless provider and your WhatsApp API throughput. Most serverless platforms handle thousands of concurrent executions.

How do I ensure data privacy? Do not include sensitive personal identifiable information (PII) like full credit card numbers on images. Use temporary URLs and set your storage bucket to auto-delete files after a short period.

Conclusion

Dynamic image overlays transform WhatsApp Flows from simple forms into professional visual experiences. By combining serverless webhooks with image manipulation libraries, you provide users with immediate and personalized value. This reduces the burden on support teams and increases conversion rates. Start with a simple text-on-image overlay and expand to more complex compositions as your requirements grow. Ensure your infrastructure prioritizes speed to remain within the webhook timeout limits.

Share this guide

Share it on social media or copy the article URL to send it anywhere.

Use the share buttons or copy the article URL. Link copied to clipboard. Could not copy the link. Please try again.