- Docs
- Example code
- Python - HTML to Image Example
Python - HTML to Image Example
Authentication with Python
Use HTTP Basic authentication with your API ID as the username and API key as the password. Find both in the dashboard. Keep credentials in server-side configuration or environment variables.
Python example code
The image examples use POST https://hcti.io/v1/image. Recordings use POST https://hcti.io/v1/recording and return a recording ID and media URL. See Recordings for timing, actions, and output options. Both creation endpoints require images:create; see authentication and API keys for scoped credentials and access errors.
Official PyPI client
Section titled “Official PyPI client”For typed requests, responses, and signed URL helpers, use the official html-css-to-image package.
pip install html-css-to-imagefrom html_css_to_image import CreateHtmlCssImageRequest, HtmlCssToImageClient
with HtmlCssToImageClient("your-api-id", "your-api-key") as client: result = client.create_image( CreateHtmlCssImageRequest( html="<div class='box'>Python ✅</div>", css=".box { border: 4px solid #03B875; padding: 20px; }", ) )
if result.success: print(result.url)else: print(result.status_code, result.error)Recordings
Section titled “Recordings”Record HTML/CSS or a webpage as an MP4, WebM, or GIF with create_recording:
from html_css_to_image import CreateUrlRecordingRequest, RecordingOutputOptions, RecordingSimpleScrollAction
result = client.create_recording(CreateUrlRecordingRequest( url="https://example.com", duration_ms=10000, actions=[RecordingSimpleScrollAction()], output=RecordingOutputOptions(format="mp4"),))if result.success: print(result.recording_url)Embed or request recording_url directly; it waits until the recording is ready. Use CreateHtmlCssRecordingRequest for HTML/CSS. See Recordings for triggers, actions, and output options.
The client also supports URL screenshots, templates, batches, image deletion, render options, and signed URLs. See the Python client repository for complete usage and API documentation.
Direct HTTP example
Section titled “Direct HTTP example”# pip3 install requestsimport requests
HCTI_API_ENDPOINT = "https://hcti.io/v1/image"# Retrieve these from https://htmlcsstoimage.com/dashboardHCTI_API_USER_ID = 'your-user-id'HCTI_API_KEY = 'your-api-key'
data = { 'html': "<div class='box'>Hello, world!</div>", 'css': ".box { color: white; background-color: #0f79b9; padding: 10px; font-family: Roboto }", 'google_fonts': "Roboto" }
image = requests.post(url=HCTI_API_ENDPOINT, data=data, auth=(HCTI_API_USER_ID, HCTI_API_KEY), timeout=60)image.raise_for_status()
print("Your image URL is: %s"%image.json()['url'])# https://hcti.io/v1/image/7ed741b8-f012-431e-8282-7eedb9910b32Turning analysis results into a weekly report
Section titled “Turning analysis results into a weekly report”After a Python job calculates sales totals or account activity, use those results to fill an HTML report. A Jinja template can handle headings, tables, and conditional sections without assembling a long HTML string in Python. Send the completed HTML and its CSS to the client, then include the returned image URL in your report email.
If the report draws charts with JavaScript, use render readiness to wait for the chart to finish. For an existing public dashboard, a URL screenshot may be simpler than recreating its layout. Choose PDF output when readers need several pages of tables rather than one image.
Need help?
Talk to a human. Email support@htmlcsstoimage.com and we’ll help you get started.