API

HTML to Image

Generate an image (PNG, JPG, or WebP) from raw HTML content. The HTML is sent as a base64-encoded string in the request body.

Endpoint

POST /v1/html/image

All requests are processed synchronously. The response body is the raw image file.

POST /v2/html/image

Requests default to asynchronous processing (async: true, storage: true). Add "async": false to receive the raw image directly. See Sync vs. Async for details.


Quick Start

cURL

HTML_BASE64=$(echo '<html><body style="padding:40px;font-family:Arial"><h1 style="color:#2563eb">Hello World</h1><p>Generated by cloudlayer.io</p></body></html>' | base64)

curl -X POST "https://api.cloudlayer.io/v1/html/image" \
  -H "X-API-Key: your-api-key-here" \
  -H "Content-Type: application/json" \
  -d "{\"html\": \"$HTML_BASE64\"}" \
  --output result.png

JavaScript (fetch)

const html = `<html>
  <body style="padding: 40px; font-family: Arial;">
    <h1 style="color: #2563eb;">Hello World</h1>
    <p>Generated by cloudlayer.io</p>
  </body>
</html>`;

const response = await fetch("https://api.cloudlayer.io/v1/html/image", {
  method: "POST",
  headers: {
    "X-API-Key": "your-api-key-here",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    html: btoa(html),
  }),
});

const image = await response.arrayBuffer();

Python (requests)

import base64
import requests

html = """<html>
  <body style="padding: 40px; font-family: Arial;">
    <h1 style="color: #2563eb;">Hello World</h1>
    <p>Generated by cloudlayer.io</p>
  </body>
</html>"""

response = requests.post(
    "https://api.cloudlayer.io/v1/html/image",
    headers={
        "X-API-Key": "your-api-key-here",
        "Content-Type": "application/json",
    },
    json={
        "html": base64.b64encode(html.encode()).decode(),
    },
)

with open("result.png", "wb") as f:
    f.write(response.content)

cURL

HTML_BASE64=$(echo '<html><body style="padding:40px;font-family:Arial"><h1 style="color:#2563eb">Hello World</h1><p>Generated by cloudlayer.io</p></body></html>' | base64)

curl -X POST "https://api.cloudlayer.io/v2/html/image" \
  -H "X-API-Key: your-api-key-here" \
  -H "Content-Type: application/json" \
  -d "{\"html\": \"$HTML_BASE64\", \"async\": false}" \
  --output result.png

JavaScript (fetch)

const html = `<html>
  <body style="padding: 40px; font-family: Arial;">
    <h1 style="color: #2563eb;">Hello World</h1>
    <p>Generated by cloudlayer.io</p>
  </body>
</html>`;

const response = await fetch("https://api.cloudlayer.io/v2/html/image", {
  method: "POST",
  headers: {
    "X-API-Key": "your-api-key-here",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    html: btoa(html),
    async: false,
  }),
});

const image = await response.arrayBuffer();

Python (requests)

import base64
import requests

html = """<html>
  <body style="padding: 40px; font-family: Arial;">
    <h1 style="color: #2563eb;">Hello World</h1>
    <p>Generated by cloudlayer.io</p>
  </body>
</html>"""

response = requests.post(
    "https://api.cloudlayer.io/v2/html/image",
    headers={
        "X-API-Key": "your-api-key-here",
        "Content-Type": "application/json",
    },
    json={
        "html": base64.b64encode(html.encode()).decode(),
        "async": False,
    },
)

with open("result.png", "wb") as f:
    f.write(response.content)

Tip: Omit "async": false to use the default async mode. The API will return a JSON response with the job ID immediately, and the generated image will be stored and accessible via the Assets endpoint.


Parameters

Required

ParameterTypeDescription
htmlstringRequired. Base64-encoded HTML content. The HTML can include inline CSS, <style> tags, and <link> references to external stylesheets.

Image Parameters

These parameters control the image output format and processing.

ParameterTypeDefaultDescription
imageTypestring"png"Output image format. Options: "png", "jpg", "webp".
transparentbooleanfalseRender with a transparent background. Only works with "png" and "webp" formats. Your HTML must not set a background color on the <html> or <body> elements.
trimbooleanfalseTrim whitespace from all edges of the resulting image. Useful for generating tightly-cropped screenshots of specific content.

Base Parameters

These parameters control page loading behavior, viewport configuration, and general output settings.

ParameterTypeDefaultDescription
autoScrollbooleanfalseScroll the page to the bottom before capturing. Triggers lazy-loaded content.
delaynumber0Wait time in milliseconds after the page loads before capturing the image.
filenamestringSet the Content-Disposition filename on the response. Include the appropriate extension (e.g., "screenshot.png").
heightstringSet the capture area height. Accepts CSS units (e.g., "600px", "100vh").
widthstringSet the capture area width. Accepts CSS units (e.g., "800px", "100vw").
inlinebooleanfalseDisplay the image inline in the browser instead of triggering a download.
landscapebooleanfalseRender in landscape orientation.
preferCSSPageSizebooleanfalseUse CSS @page size for dimensions.
projectIdstringAssociate with a project for dashboard organization.
scalenumber1Page rendering scale (0.1 to 2). Higher values produce higher-resolution images.
timeoutnumber30000Maximum time in milliseconds to wait for the page to load.
timeZonestringBrowser timezone (IANA name, e.g., "America/New_York").
viewPortobjectBrowser viewport configuration. See HTML to PDF — viewPort.
waitForSelectorobjectWait for a CSS selector before capturing. See HTML to PDF — waitForSelector.
waitUntilstring"networkidle2"Navigation completion strategy: "load", "domcontentloaded", "networkidle0", "networkidle2".

Examples

Generate a WebP Image

curl -X POST "https://api.cloudlayer.io/v2/html/image" \
  -H "X-API-Key: your-api-key-here" \
  -H "Content-Type: application/json" \
  -d "{
    \"html\": \"$(echo '<h1>Hello</h1>' | base64)\",
    \"imageType\": \"webp\"
  }" \
  --output result.webp

Transparent Background (PNG)

{
  "html": "PGRpdiBzdHlsZT0icGFkZGluZzogMjBweDsgZm9udC1zaXplOiAyNHB4OyI+VHJhbnNwYXJlbnQgQmFja2dyb3VuZDwvZGl2Pg==",
  "imageType": "png",
  "transparent": true,
  "trim": true
}

Important: When using transparent: true, make sure your HTML does not set background or background-color on the <html> or <body> elements. Any background color will override transparency.

High-Resolution Retina Image

Combine scale and viewPort.deviceScaleFactor for crisp, high-DPI images:

{
  "html": "...",
  "imageType": "png",
  "scale": 2,
  "viewPort": {
    "width": 1200,
    "height": 630,
    "deviceScaleFactor": 2
  }
}

Social Media Card (OG Image)

Generate Open Graph images for social media sharing:

JavaScript (fetch)

const html = `<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      margin: 0; padding: 0;
      width: 1200px; height: 630px;
      display: flex; align-items: center; justify-content: center;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      font-family: 'Arial', sans-serif;
      color: white;
    }
    .card {
      text-align: center;
      padding: 60px;
    }
    h1 { font-size: 56px; margin-bottom: 16px; }
    p { font-size: 24px; opacity: 0.9; }
  </style>
</head>
<body>
  <div class="card">
    <h1>${title}</h1>
    <p>${subtitle}</p>
  </div>
</body>
</html>`;

const response = await fetch("https://api.cloudlayer.io/v2/html/image", {
  method: "POST",
  headers: {
    "X-API-Key": "your-api-key-here",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    html: btoa(html),
    imageType: "png",
    viewPort: {
      width: 1200,
      height: 630,
    },
    trim: false,
  }),
});

const image = await response.arrayBuffer();

Trimmed Element Screenshot

Capture just the content with no extra whitespace:

{
  "html": "...",
  "imageType": "png",
  "trim": true,
  "viewPort": {
    "width": 800,
    "height": 600
  }
}

Tips

  • Image dimensions: The output image size is determined by the viewport and content dimensions. Use viewPort to control the rendering area and width/height to set explicit capture dimensions.
  • Retina quality: Set scale: 2 or viewPort.deviceScaleFactor: 2 for crisp images on high-DPI displays. This doubles the pixel dimensions.
  • File size: Use "jpg" for photographs and complex images (smaller file size). Use "png" for graphics with text, logos, or transparency. Use "webp" for the best compression-to-quality ratio.
  • Transparency: Only works with "png" and "webp". Remove all background colors from your HTML/CSS.
  • Dynamic content: Use waitUntil: "networkidle0" and delay for pages that load charts, maps, or other dynamic content.