Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/andresilva-cc/grafex/llms.txt

Use this file to discover all available pages before exploring further.

render() is the primary entry point for programmatic image generation in Grafex. It accepts a path to a .tsx composition file, transpiles and executes the file in Node.js, loads the resulting HTML into a headless browser, and returns a buffer containing the final image. The browser instance is created on the first call and reused for all subsequent calls within the same process, so there is no per-call launch overhead after the first render.

Signature

async function render(
  compositionPath: string,
  options?: RenderOptions,
): Promise<RenderResult>

Parameters

compositionPath
string
required
Path to the .tsx composition file. Relative paths are resolved from the current working directory of the Node.js process.
options
RenderOptions
Optional configuration object. All fields override the values in the composition’s config export. If a field is not provided, the value from config (or the named variant’s config) is used; if neither is set, the documented default applies.

Return value

Returns a Promise that resolves to a RenderResult object.
buffer
Buffer
required
Raw image data. Write directly to a file with fs.writeFile or pipe into another stream.
width
number
required
Effective render width in pixels, after applying scale. A 1200px composition with scale: 2 returns width: 2400.
height
number
required
Effective render height in pixels, after applying scale. A 630px composition with scale: 2 returns height: 1260.
scale
number
required
The device pixel ratio used for this render.
format
'png' | 'jpeg'
required
The output format used. Reflects whichever source (call options → variant config → base config → default) provided the final value.

Examples

import { render, close } from 'grafex';
import { writeFile } from 'node:fs/promises';

try {
  const result = await render('./card.tsx');
  await writeFile('./card.png', result.buffer);
  console.log(`Saved ${result.width}×${result.height} ${result.format}`);
} finally {
  await close();
}
Option values set in the call always override the composition’s config export. The resolution order is: call options → variant config → base config → built-in default.
Passing variant when the composition has no config.variants defined throws an error. Check first with getCompositionConfig + hasVariants if variant presence is uncertain at call time.

Build docs developers (and LLMs) love