Build a data-driven template
Most templates show what you send them: your app fetches a record, picks the fields, and sends them as props. A data-driven template does the fetching itself. Your app sends an id, such as a repository name, and the template loads the rest from an API before it renders, so the image always shows the data as it is now.
This guide builds a live card for a GitHub repository, the same template as the free GitHub README card. You need about 15 minutes. The playground works without an account; saving and the API need one (the free trial is enough).
How it works
A data-driven template has one more export than a normal one:
export const propsholds the inputs, here justrepo.export async function data(props)runs first. It gets the props, canfetchfrom an API, and returns an object.- The component gets the props with what
datareturned laid over them, and renders as usual.
{ repo: "react/react" } → data() → { repo, name, description, stars, … } → image
data runs everywhere the template runs, with the same rules: in the
playground preview (in your browser), over the render API and from image URLs.
Step 1: open the example
Open the GitHub repository card in the playground.
The preview shows a live card for react/react. Click Params and type
another repository, such as withastro/astro: the card changes to that
repository’s real name, description, stars and language.
Click Code to see the template. The rest of this guide walks through it.
Step 2: the inputs
export const props = {
repo: "react/react",
theme: "dark",
};
Only what the caller chooses goes here: which repository, and which theme.
Everything the API knows stays out of props.
The defaults must point at something that exists. The preview and the first image use them, and AI assistants check a template by running it, so a default that 404s fails the check.
Step 3: fetch the data
export async function data(props) {
const res = await fetch(`https://api.github.com/repos/${props.repo}`);
if (res.status === 404) {
throw new Error(`Repository not found: ${props.repo}`);
}
if (!res.ok) throw new Error(`GitHub answered ${res.status}`);
const repo = await res.json();
const license = repo.license?.spdx_id;
return {
owner: repo.owner.login,
name: repo.name,
avatar: `${repo.owner.avatar_url}&s=160`,
description: repo.description ?? "",
stars: repo.stargazers_count,
forks: repo.forks_count,
language: repo.language ?? "",
license: license && license !== "NOASSERTION" ? license : "",
};
}
A few habits that make data-driven templates reliable:
- Check
res.okand throw. A thrown error fails the render with your message, which is what you want for a repository that doesn’t exist. Don’t return placeholder values: a card that says “0 stars” looks real. - Return only what the component uses, already shaped for display. The GitHub response has about a hundred fields; the card needs eight.
- Give text a fallback. APIs return
nullfor empty fields (repo.descriptionisnullfor a repository without one), so use?? "". - Images from the API work like any image. The owner’s avatar URL goes
straight into
<img src={props.avatar}>.
fetch here is not the browser’s: it does GET requests to
a list of public APIs, returns ok, status,
json() and text(), and can’t send headers. A template can make up to 3
requests, 256 KB each.
Step 4: render it
Tell TypeScript about the merged props, so props.stars is known in the
component:
type Props = typeof props & Awaited<ReturnType<typeof data>>;
Then the component reads the data like any prop. Big numbers read better short:
// 1234 -> "1.2k", 253000 -> "253k".
function compact(n) {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
if (n >= 1_000) return `${(n / 1_000).toFixed(n >= 10_000 ? 0 : 1)}k`;
return String(n);
}
export default function RepoCard(props: Props) {
return (
<div class="flex h-full w-full flex-col justify-between bg-[#0d1117] p-20 text-white">
<div class="flex flex-col gap-6">
<div class="flex items-center gap-5">
<img src={props.avatar} class="h-[72px] w-[72px] rounded-full" />
<span class="text-[38px] text-[#9198a1]">{props.owner}</span>
</div>
<span class="text-[96px] font-bold leading-none">{props.name}</span>
<p class="m-0 text-[36px] text-[#9198a1]">{props.description}</p>
</div>
<span class="text-[34px] font-semibold">
{compact(props.stars)} stars · {compact(props.forks)} forks
</span>
</div>
);
}
export const options = { width: 1280, height: 640, format: "png" };
The full template in the playground adds a light theme, the language color, a license and icons, and shortens long descriptions. Change the design as you like, then sign in and click Save.
Step 5: use it from your app
The caller sends only the input.
With the render API, from your backend:
curl -X POST https://jsximage.com/api/render \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"template":"<template-uid>","props":{"repo":"withastro/astro"}}' \
--output astro.png
With a live image URL, for <img> tags, og:image and email: sign a URL
with just repo, as in
the Open Graph guide.
An image URL for a data-driven template is cached for 6 hours instead of 30
days, so the same URL follows the stars as they change. It uses one render the
first time it is opened, like any URL.
For a README, you don’t need an account at all: the free GitHub README card gives you Markdown with a public image URL for this template.
Other APIs
The same pattern works for every API on the list.
Two requests at once, with Promise.all, from the
npm package card:
export const props = { package: "react" };
export async function data(props) {
const [meta, downloads] = await Promise.all([
fetch(`https://registry.npmjs.org/${props.package}/latest`),
fetch(`https://api.npmjs.org/downloads/point/last-week/${props.package}`),
]);
if (!meta.ok) throw new Error(`npm answered ${meta.status}`);
const pkg = await meta.json();
return {
version: pkg.version,
description: pkg.description ?? "",
weekly: downloads.ok ? (await downloads.json()).downloads : 0,
};
}
- A Shopify product:
https://<shop>.myshopify.com/products/<handle>.jsongives the title, price, sale price and photo. See the Shopify product image tool. - Your own data: put a JSON file in a public GitHub repository and fetch it
from
raw.githubusercontent.com. Update the file, and the images follow.
Need an API that isn’t on the list? Tell us.
Troubleshooting
- “fetch: a template can only fetch from …” The URL’s host isn’t on the
list, or it isn’t
https. For Shopify, use the store’s.myshopify.comaddress, not its own domain. - “Repository not found” for a repository you can see: it is private. Templates fetch without your GitHub account, so only public data works.
- “GitHub answered 403” in the playground: GitHub allows 60 requests an hour per IP address without a token. The preview keeps responses for 10 minutes; wait a little, or edit with fewer repositories.
- “The template took too long to run” is about your code, not the request: waiting for a response doesn’t count toward the time limit.
With your AI assistant
The MCP server knows how to write data-driven templates. Ask for one in plain words:
Make a JSXImage template that shows a GitHub repository’s name, stars and latest release, in our brand colors. The input is just the repo name.
The assistant writes the data function, checks the template against a real
repository and gives you a link to the playground.
Questions? See the docs or email info@jsximage.com.