Skip to main content
The main class for managing templates and rendering images. Typically created via createRenderer(), but can be instantiated directly if needed.
For complete type definitions and interfaces, see the Type Definitions page.

Methods

getTemplate

Retrieves a template by its unique ID.
getTemplate(id: string): OgTemplate | undefined

Parameters

id
string
required
The template ID to look up

Returns

template
OgTemplate | undefined
The template definition, or undefined if not found

renderToImage

Renders a template to a PNG image buffer. This is the main method for generating OG images.
renderToImage<K extends keyof TMap>(
  templateId: K,
  params: TMap[K] | (() => Promise<TMap[K]>),
  options?: OgTemplateOptions
): Promise<Buffer>

Parameters

templateId
string
required
The ID of the template to render
params
object | function
required
Parameters specific to the chosen template, or a function returning a Promise of parameters. User-provided parameters override shared parameters.
options
OgTemplateOptions
Rendering options

Returns

buffer
Promise<Buffer>
A promise that resolves to the PNG image buffer

Rendering process

1

Template Lookup

Looks up the template by ID in the internal registry
2

Parameter Merging

Merges shared params with user paramsPriority: Parameters are merged with the following precedence (highest to lowest):
  1. params passed to renderToImage()
  2. sharedParams from renderer configuration
  3. Template default parameters
3

Cache Checking

Generates a cache key and checks for cached result
4

Before Render Hook

Calls beforeRender hook if configured
5

Core Rendering

Delegates to the core rendering engine
6

Cache Storage

Caches the generated image
7

After Render Hook

Calls afterRender hook if configured
8

Return Buffer

Returns the PNG buffer

Examples

const buffer = await renderer.renderToImage("basic", {
  title: "Hello World",
  layout: "centered",
});

Usage Example

import { createRenderer } from "@ogify/core";
import basicTemplate from "@ogify/templates/basic";
import type { TemplateParams } from "@ogify/templates/basic";

const renderer = createRenderer<{ basic: TemplateParams }>({
  templates: { basic: basicTemplate },
});

// Get template info
const template = renderer.getTemplate("basic");
console.log(template?.fonts);

// Render image
const buffer = await renderer.renderToImage("basic", {
  title: "My Page",
  subtitle: "A great description",
  layout: "centered",
});

// Save or return the buffer
await writeFile("og-image.png", buffer);