Generate image from HTML
Generate an image by passing complete HTML on every request. For production, the generate-image-from-template API is recommended.
The template-based API is recommended
For most use cases, use the Generate image from template API: save and publish a template in the template editor, then pass only variables in the request.
This API is for one-off rendering when you do not save a template and pass complete HTML every time.
With this API, you can turn an HTML snippet directly into a high-quality image and export it as WebP, PNG, or JPEG.
Request
Request method and URL
POST https://api.imgrender.net/open/v1/images/renderHeader 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" |
Request body parameters
| Field | Type | Required | Description | Default |
|---|---|---|---|---|
content | string | yes | HTML string that describes the image layout and content. See the template guide for how to write it. | - |
width | number | no | Width of the output image, in pixels. If omitted, the width adapts to the content. | - |
height | number | no | Height of the output image, in pixels. If omitted, the height adapts to the content. | - |
format | string | no | Output image format. png, jpeg, and webp are supported. | webp |
quality | number | no | Image quality (0-100). Applies only to jpeg and webp. | - |
fonts | array | no | List of custom font configurations, used to load and render external fonts in the content. See Custom fonts. | [] |
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 HTML and generate an image:
curl -X POST https://api.imgrender.net/open/v1/images/render \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-d '{
"content": "<div style=\"background-color: #3b82f6; padding: 40px; border-radius: 12px\"><span style=\"color: white; font-size: 32px; font-weight: bold\">Hello imgrender!</span></div>",
"width": 800,
"height": 400,
"format": "png"
}'const payload = {
content:
'<div style="background-color: #3b82f6; padding: 40px; border-radius: 12px"><span style="color: white; font-size: 32px; font-weight: bold">Hello imgrender!</span></div>',
width: 800,
height: 400,
format: 'png',
}
fetch('https://api.imgrender.net/open/v1/images/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))To register custom web fonts in a request, see Custom fonts.