Embedded Tweet JavaScript Factory Function

Twitter’s widget JavaScript library supports dynamic insertion of Embedded Tweets using the twttr.widgets.createTweet function. Pass a Tweet ID, target parent element, and any custom options.

The code snippets on this page assume Twitter’s widgets.js has successfully loaded on your page. Include an asynchronous script loader on your page while initializing window.twttr as described in our JavaScript loader documentation. All JavaScript code depending on widgets.js should execute on or after twttr.ready.

Arguments

tweetID required

The numerical ID of the desired Tweet.

Example Values: '20'

targetEl required

DOM node of the desired insertion point.

Example Values: document.getElementById('container')

options optional

Override default widget options. See Embedded Tweet parameter reference for details.

Example Values: { theme: 'dark' }

Example

An element with a DOM ID of container exists on the page.

<div id="container"></div>

The code snippet below will insert Tweet ID 20 into a page inside an element with a unique ID of container. The options object specifies a dark theme customization.

twttr.widgets.createTweet(
  '20',
  document.getElementById('container'),
  {
    theme: 'dark'
  }
);

Promises

The twttr.widgets.createTweet returns a Promise. You can execute code after a widget has been inserted onto your page by passing a callback to the resulting promise’s then function.

twttr.widgets.createTweet(...)
.then( function( el ) {
  console.log('Tweet added.');
});