Boost Social Media Engagement with Dynamic OG Images Using Puppeteer

Learn how to boost social media engagement by dynamically generating custom Open Graph images using Puppeteer and an Express server for personalized user sharing.

Author

Madhav Bansal
Madhav BansalSenior Software Engineer - I

Date

Aug 14, 2024

Table of Contents

Creating dynamic Open Graph (OG) images and tags for your web applications can significantly enhance social sharing and user engagement. This tutorial will guide you through setting up an Express server that dynamically generates user-specific OG images using Puppeteer. We’ll use a custom HTML template, generate screenshots, and use these for social media previews.

What is an Open Graph (OG)?

Open Graph (OG) is a protocol that transforms web pages into rich, engaging content when shared on social media platforms. For example, when a link is shared on Facebook, OG tags dictate how it appears, including its title, description, and associated image. By tailoring these tags, you can make your shared links more appealing and interactive.

Prerequisites

  • Node.js installed
  • Puppeteer for Node.js

Step 1: Setting Up the Express Server


First, let’s create a new Express server.

Create a file named server.js and set up a basic Express server:

Step 2: Creating the Route Handler

Next, create a route that will handle the generation of the OG image. This route will generate an HTML template with dynamic data and use Puppeteer to take screenshots. In a real application, you might fetch user data from a database like SQL, Firebase, or MongoDB. For simplicity, we’ll hardcode the user data in this example.

Step 3: Generating HTML Template

Now, let’s create a function to generate an HTML template with dynamic user data. This template will be used to create the OG image.

Step 4: Capturing the OG Image with Puppeteer

We need a function to generate an OG image using Puppeteer. This function will render the HTML template and take a screenshot, saving it locally.

Step 5: Setting Up the Server Listener

Finally, we set up the server to listen on the specified port.

Usage

To use this setup, make a GET request to the /generate-og/:userId route, replacing :userId with the actual user ID you want to generate an OG image for. The server will generate a dynamic HTML template, capture an image with Puppeteer, and return an HTML response with the appropriate OG tags.

For example:
GET <http://localhost:3000/generate-og/12345>

This will generate an OG image for the user with ID 12345, save it locally, and return an HTML page with the OG tags ready for sharing.

Notes

  • Dynamic Data: While this example hardcodes user data, in a real-world application, you would fetch this data from a database such as SQL, Firebase, or MongoDB.
  • OG Tags: Open Graph tags are essential for ensuring your content is displayed correctly when shared on social media platforms.

Conclusion

By following these steps, you can dynamically generate OG images and tags tailored to individual users, enhancing the sharing experience and boosting engagement on social media platforms. This setup leverages the power of Puppeteer to create a seamless and efficient user experience

SHARE ON

Related Articles.

More from the engineering frontline.

Dive deep into our research and insights on design, development, and the impact of various trends to businesses.

From RFPs to Revenue: How We Built an AI Agent Team That Writes Technical Proposals in 60 Seconds
Article

Apr 9, 2026

From RFPs to Revenue: How We Built an AI Agent Team That Writes Technical Proposals in 60 Seconds

GeekyAnts built DealRoom.ai — four AI agents that turn RFPs into accurate technical proposals in 60 seconds, with real-time cost breakdowns and scope maps.

How We Built an AI System That Automates Senior Solution Architect Workflows
Article

Apr 6, 2026

How We Built an AI System That Automates Senior Solution Architect Workflows

Discover how we built a 4-agent AI co-pilot that converts complex RFPs into draft technical proposals in 15 minutes — with built-in conflict detection, assumption surfacing, and confidence scoring.

AI Code Healer for Fixing Broken CI/CD Builds Fast
Article

Apr 6, 2026

AI Code Healer for Fixing Broken CI/CD Builds Fast

A deep dive into how GeekyAnts built an AI-powered Code Healer that analyzes CI/CD failures, summarizes logs, and generates code-level fixes to keep development moving.

A Real-Time AI Fraud Decision Engine Under 50ms
Article

Apr 2, 2026

A Real-Time AI Fraud Decision Engine Under 50ms

A deep dive into how GeekyAnts built a real-time AI fraud detection system that evaluates transactions in milliseconds using a hybrid multi-agent approach.

Building an Autonomous Multi-Agent Fraud Detection System in Under 200ms
Article

Apr 1, 2026

Building an Autonomous Multi-Agent Fraud Detection System in Under 200ms

GeekyAnts built a 5-agent fraud detection pipeline that makes decisions in under 200ms — 15x cheaper than single-model systems, with full explainability built in.

Building a Self-Healing CI/CD System with an AI Agent
Article

Mar 31, 2026

Building a Self-Healing CI/CD System with an AI Agent

When code breaks a pipeline, developers have to stop working and figure out why. This blog shows how an AI agent reads the error, finds the fix, and submits it for review all on its own.

Scroll for more
View all articles