media expansion works with the tweetid from the docs page:

endpoint:
api_twitter_com/2/tweets
parameters:
ids=1260294888811347969&tweet.fields=attachments,author_id,created_at,public_metrics&expansions=attachments.media_keys&media.fields=public_metrics,preview_image_url

but when I switch to another tweet id does not expand to give metrics or url:
endpoint:
api_twitter_com/2/tweets
parameters: ids=1420972569855934464&tweet.fields=attachments,author_id,created_at,public_metrics&expansions=attachments.media_keys&media.fields=public_metrics,preview_image_url

Appreciate if anyone can help me.

How are you making these calls? Using what library / language?

i tried:

twurl "/2/tweets?ids=1260294888811347969&tweet.fields=attachments,author_id,created_at,public_metrics&expansions=attachments.media_keys&media.fields=public_metrics,preview_image_url"

and got:

{
  "data": [
    {
      "author_id": "783214",
      "id": "1260294888811347969",
      "attachments": {
        "media_keys": [
          "13_1260294804770041858"
        ]
      },
      "text": "Don’t miss the Tweets about your Tweet. \n\nNow on iOS, you can see Retweets with comments all in one place. https://t.co/oanjZfzC6y",
      "public_metrics": {
        "retweet_count": 4867,
        "reply_count": 1691,
        "like_count": 15862,
        "quote_count": 3210
      },
      "created_at": "2020-05-12T19:44:51.000Z"
    }
  ],
  "includes": {
    "media": [
      {
        "media_key": "13_1260294804770041858",
        "preview_image_url": "https://pbs.twimg.com/media/EX13hQKXQAIPJZc.jpg",
        "public_metrics": {
          "view_count": 1590078
        },
        "type": "video"
      }
    ]
  }
}
twurl "/2/tweets?ids=1420972569855934464&tweet.fields=attachments,author_id,created_at,public_metrics&expansions=attachments.media_keys&media.fields=public_metrics,preview_image_url"

and i got back:

{
  "data": [
    {
      "created_at": "2021-07-30T05:00:35.000Z",
      "author_id": "263874598",
      "public_metrics": {
        "retweet_count": 325,
        "reply_count": 21,
        "like_count": 571,
        "quote_count": 55
      },
      "text": "After looking over the @CDCgov slide set released by @washingtonpost, this slide really sets the stage. We are dealing with a strain that is in a class of its own now. It’s one of the most transmissible respiratory viruses we have ever seen…in history. 🧵 https://t.co/MvggUZCVmZ",
      "attachments": {
        "media_keys": [
          "3_1420972565204439044"
        ]
      },
      "id": "1420972569855934464"
    }
  ],
  "includes": {
    "media": [
      {
        "media_key": "3_1420972565204439044",
        "type": "photo"
      }
    ]
  }
}

which is all the expected fields - which ones are you missing? (there are no urls returned for videos btw, this is an API limitation currently)

thanks for the quick reply. I’m using postman.

The second query for id: 1420972569855934464

Is not expanding the preview url and that is working as expected? It definitely is an image and not a video.

“includes”: {
“media”: [
{
“media_key”: “3_1420972565204439044”,
“type”: “photo”
}

ah ok - i think i see what you mean - preview_image_url only works for videos as far as i can tell.

To get the Image url, it’s url in media.fields too:

twurl "/2/tweets?ids=1420972569855934464&tweet.fields=attachments,author_id,created_at,public_metrics&expansions=attachments.media_keys&media.fields=public_metrics,preview_image_url,url" | jq
...

  "includes": {
    "media": [
      {
        "media_key": "3_1420972565204439044",
        "type": "photo",
        "url": "https://pbs.twimg.com/media/E7hPBPiWEAQQFQa.jpg"
      }
    ]
  }
...
1 Like

Thank you! Appreciate your help.

I did not see the “url” field as an option in the api docs.

1 Like

Yeah, now that you mention it, it is missing in the docs Media object | Docs | Twitter Developer Platform there was a previous older version of the docs that listed them all, and i think i had everything here: twarc/expansions.py at main · DocNow/twarc · GitHub (the private ones will only work with the media uploader’s access token)