The main class for managing templates and rendering images. Typically created via createRenderer(), but can be instantiated directly if needed.
Methods
getTemplate
Retrieves a template by its unique ID.
getTemplate ( id : string ): OgTemplate | undefined
Parameters
The template ID to look up
Returns
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
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.
Rendering options Width of the image in pixels
Height of the image in pixels
Whether to enable Right-to-Left support
Returns
A promise that resolves to the PNG image buffer
Rendering process
Template Lookup
Looks up the template by ID in the internal registry
Parameter Merging
Merges shared params with user params Priority: Parameters are merged with the following precedence
(highest to lowest):
params passed to renderToImage()
sharedParams from renderer configuration
Template default parameters
Cache Checking
Generates a cache key and checks for cached result
Before Render Hook
Calls beforeRender hook if configured
Core Rendering
Delegates to the core rendering engine
Cache Storage
Caches the generated image
After Render Hook
Calls afterRender hook if configured
Return Buffer
Returns the PNG buffer
Examples
Static Parameters
Async Parameters
RTL Support
Custom Dimensions
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 );