Skip to main content

ImageResponse API Reference

Reference for the ImageResponse constructor and its options.

ImageResponse

The ImageResponse class is the central API for generating and serving dynamic images. It extends the standard Web API Response and is designed to be returned directly from a SvelteKit server route (+server.ts).

The constructor takes the component/HTML template, rendering options, and component props.

		new ImageResponse<T extends string | Component<any>>(
	element: T,
	options?: ImageResponseOptions,
	props?: T extends Component<any> ? ComponentProps<T> : never
): Response;
	

ImageResponse Parameters

To create your dynamic Open Graph image, the ImageResponse constructor needs just three pieces of information, passed in this specific order. Think of this as defining the design, the settings, and the data for your image.

  1. Svelte Component or Raw HTML: The design of your image—what people will actually see.
  2. Options: The settings that define the image's size, fonts, and response headers.
  3. Props: The dynamic data (like a blog post title) to feed into your Svelte component.
src/routes/og/+server.ts
		import { ImageResponse } from '@ethercorps/sveltekit-og';
import { CustomFont, resolveFonts } from '@ethercorps/sveltekit-og/fonts';
import MySvelteComponent from '$lib/og/MySvelteComponent.svelte';
export const GET = async () => {
    const props = {
        title: 'Dynamic Title',
        subtitle: 'Generated with Sveltekit OG'
    };
	const fonts = [
		new CustomFont('Inter', () => fetch('https://example.com/fonts/Inter-Regular.ttf').then(res => res.arrayBuffer()), {weight: '400'})
    ]	
    return new ImageResponse(
		// 1. Svelte Component or Raw HTML	
        MySvelteComponent,
        // 2. Options
        {
            width: 1200,
            height: 630,
            fonts: await resolveFonts(fonts)
        },
        // 3. Svelte Component props
        props
    );
};
	

Svelte Component or HTML

This parameter defines the visual content of your Open Graph image. You can provide either a Svelte component or a raw HTML string.

element
type: Component | string
required

ImageResponse Options

The options object allows you to customize various technical aspects of the generated image and its HTTP response.

options
type: ImageResponse
Configuration options for the generated image and its response.
width
type: number
Sets the width of the final OG image in pixels.
default: 1200
height
type: number
Sets the height of the final OG image in pixels.
default: 630
required
format
type: 'svg' | 'png'
The file format for the generated image. PNG is standard for OG images, but SVG is also available.
default: png
fonts
type: Font[]
A list of font definitions (name, data, weight) used to render text in your design.
data
type: Buffer | ArrayBuffer
The font file data loaded as a Buffer or ArrayBuffer.
required
name
type: string
The font family name (must match your CSS `font-family`).
required
weight
type: number
The weight of the font (e.g., `400` for regular, `700` for bold).
style
type: 'normal' | 'italic'
The style of the font.
lang
type: string
The language subset for the font (e.g., 'en').
default: Noto Sans
emoji
type: 'twemoji' | 'openmoji' | 'blobmoji' | 'noto' | 'fluent' | 'fluentFlat'
Choose the style of emojis you want to use in your design (e.g., Twitter's Twemoji is the default).
default: twemoji
debug
type: boolean
Turn this on to show helpful layout guides and rulers on the image. Great for troubleshooting positioning!
default: false
status
type: number
The HTTP status code sent with the image (e.g., `200` for success).
default: 200
statusText
type: string
The HTTP status text sent alongside the status code.
default: Success
headers
type: Record<string, unknown>
Add any custom HTTP headers you need for the response.

We automatically add `Content-Type` based on the image format and `Cache-Control` for caching. If debug mode is enabled, the `Cache-Control` header is updated to `no-cache`.
required

Component Props

props
type: ComponentProps<typeof Component>
An object containing the dynamic data to be passed to your Svelte component (the first parameter). If your component expects a title or author, you pass it here!

This parameter is required if you use a Svelte component that expects props. The types for this object are automatically inferred from the props defined in your Svelte component.

All Options Example

Here is an example demonstrating how to use several optional parameters, including setting custom headers and enabling debug mode.

src/routes/advanced-og/+server.ts
		import { ImageResponse } from '@ethercorps/sveltekit-og';
import { resolveFonts } from '@ethercorps/sveltekit-og/fonts';
import MySvelteComponent from '$lib/og/MySvelteComponent.svelte';
import { FONT_DATA } from '$lib/fonts'; // Assume pre-loaded font data
 
export const GET = async ({ url }) => {
    // 1. Prepare dynamic data
    const isError = url.searchParams.has('error');
    
    // 2. Define custom options
    const options = {
        width: 1200,
        height: 630,
        debug: url.searchParams.has('debug'), // Toggle debug mode via query param
        emoji: 'blobmoji', // Use a different emoji set
        fonts: await resolveFonts(FONT_DATA),
        
        // 3. Conditional HTTP Response options
        status: isError ? 404 : 200,
        statusText: isError ? 'Not Found' : 'OK',
        headers: {
            // Override or add custom headers
            'X-OG-Generator': '@ethercorps/sveltekit-og',
        }
    };
    
    // 4. Define component props
    const props = {
        title: isError ? 'Page Not Found' : 'Standard Post Title',
    };
 
    return new ImageResponse(MySvelteComponent, options, props);
};