Skip to content

Media ​

Upload images, GIFs, and videos, then attach them to tweets.

media.create(source, opts?) ​

Upload media to Twitter. Returns a media_id you can pass to tweets.create().

ParamTypeDescription
sourcestring | Buffer | Uint8Array | ArrayBuffer | BlobA file path, Buffer, typed array, or Blob containing the media
opts.alt_textstringAlt text for accessibility (applied automatically after upload)
opts.mediaTypestringExplicit MIME type (e.g. "image/png"). Auto-detected when omitted
opts.type"tweet" | "dm"Upload context - defaults to "tweet". Use "dm" for direct messages

Returns: { media_id, ... } - use media_id with tweets.create().

Auto-detected types: JPEG, PNG, GIF, WebP, MP4, WebM. If detection fails, pass opts.mediaType explicitly.

Size limits ​

TypeMax size
Image5 MB
GIF15 MB
Video512 MB

Examples ​

js
import { readFileSync } from "fs";

// Upload from a file path
const img = await client.media.create("./photo.jpg");
const tweet = await client.tweets.create("Check this out!", {
  mediaIds: [img.media_id],
});

// Upload a Buffer with alt text
const buf = readFileSync("./chart.png");
const chart = await client.media.create(buf, {
  alt_text: "A bar chart showing monthly revenue growth",
});
await client.tweets.create("Q4 results 📊", {
  mediaIds: [chart.media_id],
});

// Upload a video
const video = await client.media.create("./clip.mp4");
await client.tweets.create("Watch this!", {
  mediaIds: [video.media_id],
});

// Upload a Blob (e.g. from fetch)
const response = await fetch("https://example.com/image.png");
const blob = await response.blob();
const uploaded = await client.media.create(blob);

// Upload for a DM
const dm = await client.media.create("./sticker.gif", { type: "dm" });

// Explicit MIME type
const raw = await client.media.create(someBuffer, {
  mediaType: "image/webp",
});

Upload multiple media ​

js
const [a, b, c] = await Promise.all([
  client.media.create("./pic1.jpg", { alt_text: "First photo" }),
  client.media.create("./pic2.jpg", { alt_text: "Second photo" }),
  client.media.create("./pic3.jpg", { alt_text: "Third photo" }),
]);

await client.tweets.create("Photo dump 🧵", {
  mediaIds: [a.media_id, b.media_id, c.media_id],
});

Tip

Always add alt_text! It makes your media accessible to users who rely on screen readers - and it only takes one extra option.

media.createFromUrl(url, opts?) ​

Upload a GIF (or other media) from a URL. Twitter fetches the file directly from the URL - no chunked upload needed. Returns a media_id you can pass to tweets.create().

This is used internally by tweets.create({ gif }) but can also be called directly for more control.

ParamTypeDescription
urlstringThe URL of the media to upload (e.g. a Giphy/Tenor GIF URL)
opts.mediaTypestringMIME type (default "image/gif")
opts.mediaCategorystringUpload category (default auto-detected, e.g. "tweet_gif")
opts.type"tweet" | "dm"Upload context - defaults to "tweet". Use "dm" for direct messages
opts.altTextstringAlt text for accessibility
opts.originobjectProvider origin info: { provider, id } (e.g. { provider: "giphy", id: "xSM46ernAUN3y" })

Returns: { media_id, ... } - use media_id with tweets.create().

Examples ​

js
// Upload a GIF from a Giphy URL
const result = await client.media.createFromUrl(
  "https://media4.giphy.com/media/xSM46ernAUN3y/giphy.gif",
  {
    altText: "Happy If You Say So GIF",
    origin: { provider: "giphy", id: "xSM46ernAUN3y" },
  },
);
await client.tweets.create("Check this out!", {
  mediaIds: [result.media_id],
});

// Upload a Tenor GIF
const tenor = await client.media.createFromUrl(
  "https://media.tenor.com/zlRxesGf5u4AAAAC/charlie-kirk-charlie.gif",
  {
    altText: "Charlie Kirk Aura GIF",
    origin: { provider: "riffsy", id: "14867633041905280750" },
  },
);

// Use with a GIF search result
const { items } = await client.search.gifs("celebration");
const uploaded = await client.media.createFromUrl(items[0].original_image.url, {
  altText: items[0].alt_text,
  origin: items[0].found_media_origin,
});
await client.tweets.create("🎉", { mediaIds: [uploaded.media_id] });

Tip

We recommend using the gif option on tweets.create() instead as it handles the upload automatically.

media.createMetadata(mediaId, altText, opts?) ​

Set alt text and metadata for an already-uploaded media item. Uses the v1.1 media/metadata/create endpoint.

Tip

media.create() will do this for you when passing an alt_text

ParamTypeDescription
mediaIdstringThe media ID returned from upload
altTextstringAlt text description for the media
optsobjectAdditional metadata fields to merge
js
await client.media.createMetadata("1234567890", "A photo of a sunset over the ocean");

media.createSubtitles(mediaId, subtitles) ​

Attach subtitles / captions to a video. Uses the v1.1 media/subtitles/create endpoint.

ParamTypeDescription
mediaIdstringThe video media ID
subtitlesobject | arrayA subtitle object or array of subtitle objects

Each subtitle object:

FieldTypeDescription
media_idstringMedia ID of the subtitle file
language_codestringLanguage code (e.g. "en")
display_namestringDisplay name (e.g. "English")
js
// Single subtitle track
await client.media.createSubtitles("1234567890", {
  media_id: "9876543210",
  language_code: "en",
  display_name: "English",
});

// Multiple subtitle tracks
await client.media.createSubtitles("1234567890", [
  { media_id: "9876543210", language_code: "en", display_name: "English" },
  { media_id: "1111111111", language_code: "es", display_name: "Español" },
]);

media.byKeys(mediaKeys) ​

Get uploaded media by media key, such as "3_2126816346893697024". Uses the GraphQL mediaUploadFromMediaIdQuery query.

js
const [media] = await client.media.byKeys("3_2126816346893697024");
console.log(media.media_info.original_img_url);

media.altTextPreference() / media.setAltTextPreference(prompt?) ​

Read or change whether x.com reminds you to add alt text. Use accessibilityAltTextPreferenceQuery and accessibilityAltTextPreferenceMutation. prompt is true for "Prompt" and false for "None".

media.videoPreferences(deviceId?) / media.setVideoPreferences(opts?) ​

Read or change autoplay and data saver for a device. Use videoPreferencesQuery and autoplaySettingsMutation. The device id defaults to "Web". x.com derives its own id per browser, so the stored value may differ.

OptionTypeDescription
opts.deviceIdstringDevice id (default "Web")
opts.dataSaverbooleanTurn data saver on
opts.autoplaystring"Always" (default), "WiFiOnly" or "Never"

not affiliated with X Corp.