Generate image from template (recommended)
Synchronously generate an image from a published template by substituting variables (recommended)
With this API, you can use a template created and published in the imgrender dashboard or the template editor, pass variable values, and synchronously generate an image. The server loads the template content, validates the variables, finishes rendering, and returns a CDN URL for the image.
Recommended usage
This is the recommended way to call the API in production: design and publish the template in the template editor, then pass only variables in the request. You do not need to submit complete HTML every time.
If you have not created a template yet, start with Quick start and the template guide. See Variables for how to write variables.
Request
Request method and URL
POST https://api.imgrender.net/open/v1/images/templates/{id}/render{id} is the template ID. You can find it on the template details page in the dashboard.
Header parameters
| Field | Type | Required | Description |
|---|---|---|---|
| X-API-Key | string | yes | Used to authorize the request. See Authentication |
| Content-Type | string | yes | Fixed value: "application/json; charset=utf-8" |
Path parameters
| Field | Type | Required | Description |
|---|---|---|---|
id | string | yes | Template ID (set in the path) |
Request body parameters
| Field | Type | Required | Description | Default |
|---|---|---|---|---|
variables | object | yes | Map of variable names to values. Variables defined in the template are replaced by name. Variables that are not defined are ignored. | - |
width | number | no | Override the template's default output width, in pixels. | - |
height | number | no | Override the template's default output height, in pixels. | - |
format | string | no | Override the template's default output format. Allowed values are png, jpeg, and webp. | - |
quality | number | no | Override the template's default image quality (0-100). Applies only to jpeg and webp. | - |
useDraft | boolean | no | Whether to render draft content. Published content is used by default. Use draft content only while debugging. | false |
Limitations
- The template must be published first. If it is not published or has been archived, the API returns
409. - To pass complete HTML on every request without a saved template, use the Generate image from HTML API.
Response
When the request succeeds, the HTTP status code is 200 and data is returned as JSON:
{
"code": 0,
"message": "ok",
"data": {
"url": "https://res.imgrender.net/6e31cfcd683a36d0522a8cc34e244379.jpg?sign=xxx"
}
}code: The error code.0means the request succeeded; any other value indicates a problem.message: A message corresponding tocode. For more messages, see Status codes and error codes.data: The returned data, present whencodeis0.urlis the image URL. The image URL is valid for 5 minutes, so download or display the image promptly. After it expires, send the request again to get a new URL.
Example
Below is a complete request example that shows how to pass variables for a template ID and generate an image:
curl -X POST https://api.imgrender.net/open/v1/images/templates/:template_id/render \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-d '{
"variables": {
"title": "Weekend raffle",
"nickname": "Davinci",
"avatar": "https://example.com/avatar.jpg"
},
"format": "png",
"quality": 90
}'const payload = {
variables: {
title: 'Weekend raffle',
nickname: 'Davinci',
avatar: 'https://example.com/avatar.jpg',
},
format: 'png',
quality: 90,
}
fetch(
'https://api.imgrender.net/open/v1/images/templates/:template_id/render',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'YOUR_API_KEY',
},
body: JSON.stringify(payload),
},
)
.then((res) => res.json())
.then((result) => console.log('Image URL:', result.data.url))Error codes
| Error code | HTTP status code | Description | Suggested action |
|---|---|---|---|
10103 | 400 | Invalid parameters or variable validation failed | Check request parameters such as variables against the message. |
10104 | 401 | API key authentication failed | Check that the API key is set correctly and is still valid. |
20105 | 403 | Image generation failed | Follow the message. A common cause is a template rendering error. |
30101 | 403 | No credits available | Check that your credits are sufficient and have not expired. |
| - | 409 | The template is not published, has been archived, or the draft is unavailable | Confirm that the template is published, or turn off useDraft and retry. |
For more common error codes, see Status codes and error codes.