Image output

Twitter Tweet Screenshot

Screenshot of a tweet

Learn how to use HTML/CSS to Image to generate a screenshot of a tweet. This example makes use of Twitter's embed code to reliably generate an image.

To customize this: swap out the link to the tweet with the link to any other tweet you want. Take note of the extra parameters set for this example. We set a selector and ms_delay. This helps with cropping and gives the Twitter JavaScript plenty of time to render.

Preview of Twitter Tweet Screenshot

Interactive example

Edit and render this image

Change the HTML, CSS, or API options. The browser preview updates as you edit; select Image to render the final output through the API.

Loading playground…

Example code

Use this request in your app

Choose a language and copy a complete request using this example's HTML, CSS, and API options.

const apiId = process.env.HCTI_API_ID;
const apiKey = process.env.HCTI_API_KEY;
if (!apiId || !apiKey) {
    throw new Error("Missing HCTI_API_ID or HCTI_API_KEY");
}
const authorization = `Basic ${Buffer.from(`${apiId}:${apiKey}`).toString("base64")}`;

const request = {
    html: `<blockquote class="twitter-tweet" style="width: 400px;" data-dnt="true">
<p lang="en" dir="ltr"></p>

<a href="https://twitter.com/fortnitegame/status/1253524351376330752"></a>

</blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>`,
    ms_delay: 1500,
    selector: ".twitter-tweet",
};

const response = await fetch("https://hcti.io/v1/image", {
    method: "POST",
    headers: {
        Authorization: authorization,
        "Content-Type": "application/json",
    },
    body: JSON.stringify(request),
});
if (!response.ok) {
    throw new Error(`HCTI request failed: ${response.status} ${await response.text()}`);
}
const result = await response.json() as { url: string };
console.log(result.url);

Request configuration

Options used in this example

These values are sent with the HTML and CSS to control how HCTI loads the page and captures the final screenshot. You can change each one in the editor above.

Selector

selector
.twitter-tweet
A CSS selector for an element in the HTML. We’ll crop the image to this specific element.
Learn about this option

Ms Delay

ms_delay
1500
Adds extra time in milliseconds before taking the screenshot so JavaScript can execute.
Learn about this option

From code to screenshot

How HCTI examples work

HTML/CSS to Image renders HTML and CSS in hosted Chrome, then captures the result as an image or PDF. Each example combines real source code with the API options used for the render, so it is both a working demo and a starting point for your own request.

Edit the source and options above to test changes in the browser, then render through the API to see the production result.

Common questions

Can I use the HTML and CSS from these examples?

Yes. Open an example to inspect and edit its complete HTML, CSS, and render options. Use it as a starting point, then send the resulting request from your application.

What is the difference between an example and a template?

Examples are complete API requests built from HTML and CSS. Templates are saved, reusable designs created in the visual editor and rendered repeatedly with different variable values.

Does HCTI create screenshots and PDFs?

Yes. HCTI can render HTML and CSS or capture a public webpage as an image, and it can generate PDFs when you need document output instead of a screenshot.

Keep exploring

Related examples

View all examples