---
title: "How to Screenshot a Twitter Embed"
description: "Capture a live Twitter or X embed as a PNG using the HTML API. Code, ms_delay timing and the caching pitfalls."
url: "https://html2img.com/docs/examples/twitter-embed/"
---

# How to Screenshot a Twitter Embed via API

Render a Twitter (X) embed as a PNG using the HTML to Image HTML endpoint. Useful for newsletters, blog inserts and archives where the live embed cannot run.

> **Note**
>
> If you want a pre-built version of this, use the [Tweet mockup card template](https://html2img.com/templates/tweet-mockup-card/) or the [Twitter post template](https://html2img.com/templates/twitter-post/) and skip the embed widget entirely. Send the username, handle, body and timestamp as JSON. To build one in your browser, open the [Twitter card tool](https://html2img.com/tools/twitter-card/).

## HTML and CSS

```html
<blockquote class="twitter-tweet">
  <a href="https://twitter.com/username/status/1298730289737293824"></a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js"></script>
```

```css
body { margin: 0; padding: 16px; background: white; }
.twitter-tweet { margin: 0 !important; }
```

## ms_delay

Add a 1 second delay so the embed widget has time to render before capture. The Twitter widget script does not expose a stable selector, so [ms_delay](https://html2img.com/docs/parameters/ms_delay/) is the right tool here. See also [wait_for_selector](https://html2img.com/docs/parameters/wait_for_selector/) for cases where you control the markup.

## Code examples

### cURL

```bash
curl -X POST https://app.html2img.com/api/html \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<blockquote class=\"twitter-tweet\"><a href=\"https://twitter.com/username/status/1298730289737293824\"></a></blockquote><script async src=\"https://platform.twitter.com/widgets.js\"></script>",
    "css": "body { margin: 0; padding: 16px; background: white; } .twitter-tweet { margin: 0 !important; }",
    "width": 550,
    "height": 321,
    "ms_delay": 1000
  }'
```

### PHP

```php
$response = file_get_contents('https://app.html2img.com/api/html', false, stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => "X-API-Key: " . getenv('HTML2IMG_API_KEY') . "\r\nContent-Type: application/json\r\n",
        'content' => json_encode([
            'html' => '<blockquote class="twitter-tweet"><a href="https://twitter.com/username/status/1298730289737293824"></a></blockquote><script async src="https://platform.twitter.com/widgets.js"></script>',
            'css' => 'body { margin: 0; padding: 16px; background: white; } .twitter-tweet { margin: 0 !important; }',
            'width' => 550,
            'height' => 321,
            'ms_delay' => 1000,
        ]),
    ],
]));

$url = json_decode($response, true)['url'];
```

### Node.js

```javascript
const response = await fetch('https://app.html2img.com/api/html', {
  method: 'POST',
  headers: {
    'X-API-Key': process.env.HTML2IMG_API_KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    html: '<blockquote class="twitter-tweet"><a href="https://twitter.com/username/status/1298730289737293824"></a></blockquote><script async src="https://platform.twitter.com/widgets.js"></script>',
    css: 'body { margin: 0; padding: 16px; background: white; } .twitter-tweet { margin: 0 !important; }',
    width: 550,
    height: 321,
    ms_delay: 1000,
  }),
});

const { url } = await response.json();
```

### Python

```python
import os
import requests

response = requests.post(
    'https://app.html2img.com/api/html',
    headers={'X-API-Key': os.environ['HTML2IMG_API_KEY']},
    json={
        'html': '<blockquote class="twitter-tweet"><a href="https://twitter.com/username/status/1298730289737293824"></a></blockquote><script async src="https://platform.twitter.com/widgets.js"></script>',
        'css': 'body { margin: 0; padding: 16px; background: white; } .twitter-tweet { margin: 0 !important; }',
        'width': 550,
        'height': 321,
        'ms_delay': 1000,
    },
)

url = response.json()['url']
```

## How it works

1. We use Twitter's official embed code to render the tweet.
2. The CSS sets a white background and clears the default margin.
3. The API request includes both HTML and CSS plus an `ms_delay` of 1 second.
4. A fixed `width` of 550 matches Twitter's preferred embed width.

## Common pitfalls

- **Embed widget never loads.** Twitter's widget script can be blocked by ad blockers or rate limited. Use the [Tweet Mockup Card template](https://html2img.com/templates/tweet-mockup-card/) for stable, reproducible output.
- **Tweet is from a private or deleted account.** The widget renders an empty box. Verify the URL is publicly accessible before rendering.
- **`ms_delay` is too short.** A flaky network can push the widget past 1 second. Bump to 2000ms if your renders fail intermittently.

## See also

- [JavaScript integration guide](https://html2img.com/integrations/javascript/)
- [PHP integration guide](https://html2img.com/integrations/php/)
- [css parameter](https://html2img.com/docs/parameters/css/), [ms_delay parameter](https://html2img.com/docs/parameters/ms_delay/)

<script type="application/ld+json" set:html={JSON.stringify({
  "@context": "https://schema.org",
  "@type": "HowTo",
  "name": "How to Screenshot a Twitter Embed via API",
  "description": "Render a Twitter embed as a PNG using the HTML to Image HTML API.",
  "step": [
    { "@type": "HowToStep", "name": "Build the embed HTML", "text": "Wrap the tweet permalink in Twitter's embed blockquote and include widgets.js." },
    { "@type": "HowToStep", "name": "POST to the API", "text": "Send the HTML, CSS, width, height and ms_delay to the HTML to Image HTML endpoint." },
    { "@type": "HowToStep", "name": "Use the returned URL", "text": "Embed the i.html2img.com URL from the response in your newsletter or page." }
  ]
})}></script>
