OG Image API

Generate Open Graph images, banners, and social cards from HTML templates with {{variables}} — design once, render thousands.

POST /v1/image → Binary image (image/png, image/jpeg, or image/webp)

Example

curl -X POST https://rasterkit.com/v1/image \
  -H "x-api-key: $RASTERKIT_API_KEY" \
  -H "content-type: application/json" \
  -d '{"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}}' \
  -o og-image.png
import { writeFile } from 'node:fs/promises'

const res = await fetch('https://rasterkit.com/v1/image', {
  method: 'POST',
  headers: {
    'x-api-key': process.env.RASTERKIT_API_KEY,
    'content-type': 'application/json',
  },
  body: JSON.stringify({"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}}),
})
if (!res.ok) throw new Error(`Render failed: ${res.status} ${await res.text()}`)
await writeFile('og-image.png', Buffer.from(await res.arrayBuffer()))
console.log('Saved og-image.png')
import os
import requests

res = requests.post(
    "https://rasterkit.com/v1/image",
    headers={"x-api-key": os.environ["RASTERKIT_API_KEY"]},
    json={"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}},
    timeout=60,
)
res.raise_for_status()
with open("og-image.png", "wb") as f:
    f.write(res.content)
print("Saved og-image.png")
<?php
$ch = curl_init('https://rasterkit.com/v1/image');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 60,
    CURLOPT_HTTPHEADER => [
        'x-api-key: ' . getenv('RASTERKIT_API_KEY'),
        'content-type: application/json',
    ],
    CURLOPT_POSTFIELDS => '{"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}}',
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($status !== 200) {
    throw new RuntimeException("Render failed: $status $body");
}
file_put_contents('og-image.png', $body);
echo "Saved og-image.png\n";
require "net/http"
require "json"

uri = URI("https://rasterkit.com/v1/image")
req = Net::HTTP::Post.new(uri)
req["x-api-key"] = ENV.fetch("RASTERKIT_API_KEY")
req["content-type"] = "application/json"
req.body = '{"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}}'

res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: 60) { |http| http.request(req) }
raise "Render failed: #{res.code} #{res.body}" unless res.code == "200"
File.binwrite("og-image.png", res.body)
puts "Saved og-image.png"
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
)

func main() {
	body := []byte(`{"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}}`)
	req, _ := http.NewRequest("POST", "https://rasterkit.com/v1/image", bytes.NewReader(body))
	req.Header.Set("x-api-key", os.Getenv("RASTERKIT_API_KEY"))
	req.Header.Set("content-type", "application/json")

	client := &http.Client{Timeout: 60 * time.Second}
	res, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	data, _ := io.ReadAll(res.Body)
	if res.StatusCode != 200 {
		panic(fmt.Sprintf("render failed: %d %s", res.StatusCode, data))
	}
	os.WriteFile("og-image.png", data, 0644)
	fmt.Println("Saved og-image.png")
}
import java.net.URI;
import java.net.http.*;
import java.nio.file.*;
import java.time.Duration;

public class Render {
    public static void main(String[] args) throws Exception {
        String body = """
            {"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}}""";
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://rasterkit.com/v1/image"))
            .timeout(Duration.ofSeconds(60))
            .header("x-api-key", System.getenv("RASTERKIT_API_KEY"))
            .header("content-type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(body))
            .build();

        HttpResponse<byte[]> res = HttpClient.newHttpClient()
            .send(request, HttpResponse.BodyHandlers.ofByteArray());
        if (res.statusCode() != 200) {
            throw new RuntimeException("Render failed: " + res.statusCode() + " " + new String(res.body()));
        }
        Files.write(Path.of("og-image.png"), res.body());
        System.out.println("Saved og-image.png");
    }
}
using System.Text;

var client = new HttpClient { Timeout = TimeSpan.FromSeconds(60) };
client.DefaultRequestHeaders.Add("x-api-key",
    Environment.GetEnvironmentVariable("RASTERKIT_API_KEY"));

var body = new StringContent(
    """{"template_id": "tpl_your_template", "variables": {"title": "Hello World", "author": "Paul"}}""",
    Encoding.UTF8, "application/json");

var res = await client.PostAsync("https://rasterkit.com/v1/image", body);
var bytes = await res.Content.ReadAsByteArrayAsync();
if (!res.IsSuccessStatusCode)
    throw new Exception($"Render failed: {(int)res.StatusCode} {Encoding.UTF8.GetString(bytes)}");

await File.WriteAllBytesAsync("og-image.png", bytes);
Console.WriteLine("Saved og-image.png");

Parameters

Send as a JSON body. Exactly the same fields work as query parameters on the signed GET variant.

ParameterTypeDefaultDescription
template_id string A stored template (create via dashboard or POST /v1/templates). Exactly one of template_id or template_html.
template_html string ≤500KB Inline HTML/CSS template with {{handlebars}} variables.
variables object {} Values substituted into the template. HTML-escaped by default; {{{var}}} opts out.
width int 64–4096 1200 Output width (1200×630 = OG standard).
height int 64–4096 630 Output height.
device_scale_factor number 1–3 1 Retina scaling.
format png | jpeg | webp png Output encoding.
quality int 1–100 80 jpeg/webp quality.
timeout_ms int 1000–60000 30000 Render time cap.
cache_ttl int 0–604800 (s) 0 Cache identical renders.
async bool false Return 202 + job id immediately instead of waiting.
webhook_url url POSTed (signed) when an async job finishes.

Responses

Inline templates

For one-offs or previews, pass template_html directly instead of template_id:

{
  "template_html": "<div style='width:1200px;height:630px;display:flex;align-items:center;justify-content:center;background:#0b0d12;color:#fff'><h1 style='font-size:64px'>{{title}}</h1></div>",
  "variables": { "title": "Hello World" }
}

For production, store templates and reference them by id — smaller requests and central editing.

Escaping

{{variable}} values are HTML-escaped (safe for user-generated titles). {{{variable}}} injects raw HTML — only for trusted content. Helpers available: uppercase, lowercase, truncate.