npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dynamic-og-image

v1.0.0

Published

A lightweight Node.js package to generate dynamic Open Graph (OG) images using Puppeteer.

Downloads

13

Readme

Dynamic OG Image Generator

A lightweight and flexible Node.js package to generate dynamic Open Graph (OG) images for social media sharing. This package allows developers to inject dynamic data into an HTML template and generate images in real time.


📖 Table of Contents


✨ Features

  • 📷 Dynamic OG Image Generation
  • 🎨 Customizable Templates
  • 🚀 Fast Puppeteer-based Rendering
  • 🏗 Supports Any Use Case (Stock Prices, Blog Previews, Custom Metadata, etc.)
  • 🔄 PNG Output Format (with potential support for JPEG/SVG in future versions)
  • 🏎 Optimized for Performance & Caching
  • 📦 Easy Integration with Express.js & Other Backends

📦 Installation

Install the package using npm or yarn:

npm install dynamic-og-image
yarn add dynamic-og-image

🛠 API Documentation

generateOgImage(options: OgImageOptions): Promise<Buffer>

This function generates an Open Graph (OG) image dynamically by injecting data into a provided HTML template.

📌 Parameters

| Parameter | Type | Default | Description | --- | --- | --- | --- | | template | string | none | HTML template containing placeholders (e.g., {{ title }}) where dynamic data will be injected. | data | Record<string, any> | none | An object containing key-value pairs that replace the placeholders inside the template. | type | string | png | The format of the generated image ('png' | 'jpeg'). | quality | number | 60 | Puppeteer's timeout in milliseconds. | puppeteerArgs | { args: string[] } | Default Puppeteer args | Custom Puppeteer launch arguments.

🔄 Returns

  • A Promise<Buffer> that resolves to a Buffer containing the generated PNG image.

🌐 Using in an Express API

You can use this package inside an Express.js route to serve OG images dynamically.

const express = require('express');
import { generateOgImage } from 'dynamic-og-image';

const app = express();

// 🔹 NOTE: You can replace this api endpoint according to your project api conventions
app.get('/og-image', async (req, res) => {
  try {
    const { title = 'Default Title', description = 'Default Description' } = req.query;

    const htmlTemplate = `
      <html>
        <head>
          <style>
            body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 500px; width: 800px; background: #f0f0f0; }
            .content { text-align: center; }
            h1 { color: #333; }
          </style>
        </head>
        <body>
          <div class="content">
            <h1>{{ title }}</h1>
            <p>{{ description }}</p>
          </div>
        </body>
      </html>
    `;

    // 🔹 NOTE: You can fetch data from your API or database using the parameter here.
    const imageBuffer = await generateOgImage({ template: htmlTemplate, data: { title, description } });

    res.writeHead(200, { 'Content-Type': 'image/png' });
    res.end(imageBuffer, 'binary');
  } catch (error) {
    res.status(500).json({ error: 'Failed to generate OG image' });
  }
});

🖼 Using the Generated OG Image in Frontend

You can use the dynamically generated OG image URL in your frontend by adding it to the tag:

<meta property="og:image" content="https://yourdomain.com/og-image?id=_id" />

This ensures that when your page is shared on social media, the dynamically generated image appears.

📊 Example: Generating a Stock Market OG Image

You can customize your template and the data passed to it based on your use case.

Request:

GET /og-image?stockId=1234

Custom Template for Stocks:

const stockTemplate = `
  <html>
    <head>
      <style>
        body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 500px; width: 800px; background: #121212; color: #fff; }
        .content { text-align: center; }
        h1 { font-size: 40px; margin-bottom: 10px; }
        p { font-size: 28px; }
        .change { color: {{ changeColor }}; font-weight: bold; }
      </style>
    </head>
    <body>
      <div class="content">
        <h1>{{ stock }}</h1>
        <p>Current Price: ${{ price }}</p>
        <p class="change">Change: {{ change }}</p>
      </div>
    </body>
  </html>
`;

const stockData = {
  stock: 'Apple',
  price: '180.50',
  change: '+1.25%',
  changeColor: '#0f0',
};

const imageBuffer = await generateOgImage({ template: stockTemplate, data: stockData });

⚡ Performance Considerations

  • Avoid Generating Images on Every Request → Implement caching (Redis, CDN) to optimize response times.

🎨 Customization Options

You can modify your template and customize the data you pass based on your needs.

  • Add logos, branding, and colors to match your theme.
  • Use different query parameters to generate images dynamically.
  • Adjust text, styles, and layout using inline CSS.

📜 License

This project is licensed under the MIT License.