imgrender

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.com/open/v1/images/render

Header parameters

FieldTypeRequiredDescription
X-API-KeystringyesUsed to authorize the request. See Authentication
Content-TypestringyesFixed value: "application/json; charset=utf-8"

Request body parameters

FieldTypeRequiredDescriptionDefault
contentstringyesHTML string that describes the image layout and content. See the template guide for how to write it.-
widthnumbernoWidth of the output image, in pixels. If omitted, the width adapts to the content.-
heightnumbernoHeight of the output image, in pixels. If omitted, the height adapts to the content.-
formatstringnoOutput image format. png, jpeg, and webp are supported.webp
qualitynumbernoImage quality (0-100). Applies only to jpeg and webp.-
fontsarraynoList 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.com/6e31cfcd683a36d0522a8cc34e244379.jpg?sign=xxx"
  }
}
  • code: The error code. 0 means the request succeeded; any other value indicates a problem.
  • message: A message corresponding to code. For more messages, see Status codes and error codes.
  • data: The returned data, present when code is 0. url is 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.com/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.com/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.

On this page