imgrender

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

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

Path parameters

FieldTypeRequiredDescription
idstringyesTemplate ID (set in the path)

Request body parameters

FieldTypeRequiredDescriptionDefault
variablesobjectyesMap of variable names to values. Variables defined in the template are replaced by name. Variables that are not defined are ignored.-
widthnumbernoOverride the template's default output width, in pixels.-
heightnumbernoOverride the template's default output height, in pixels.-
formatstringnoOverride the template's default output format. Allowed values are png, jpeg, and webp.-
qualitynumbernoOverride the template's default image quality (0-100). Applies only to jpeg and webp.-
useDraftbooleannoWhether 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. 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 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 codeHTTP status codeDescriptionSuggested action
10103400Invalid parameters or variable validation failedCheck request parameters such as variables against the message.
10104401API key authentication failedCheck that the API key is set correctly and is still valid.
20105403Image generation failedFollow the message. A common cause is a template rendering error.
30101403No credits availableCheck that your credits are sufficient and have not expired.
-409The template is not published, has been archived, or the draft is unavailableConfirm that the template is published, or turn off useDraft and retry.

For more common error codes, see Status codes and error codes.

On this page