I am trying to use the Recent Search API to get tweets and any images associated with them. When I try to make a request that has tweets with images I only get the “media_keys” in the attachments

Here is an example query url with what I am trying to do

recent?query=from:TwitterDev&tweet.fields=created_at&expansions=attachments.media_keys&media.fields=preview_image_url,type

I am trying to get the preview_image_url and the type but the preview image never shows in the response.

How do I get the image urls of the attached media items?

2 Likes

Thanks for reaching out. This is for video not currently available at this time. However, do you have the full version of the request you are making?

You can check out our product roadmap here and follow us at @TwitterDev for product updates. If you have any feature requests or feedback please let us know via our feedback channel.

It would not let me post the full url so I just gave the query part.

https://api.twitter.com/2/tweets/search/recent?query=from:TwitterDev&tweet.fields=created_at&expansions=author_id,attachments.media_keys&media.fields=media_key,type,url&user.fields=profile_image_url

In any event I think I found what I am looking for but it seems to be undocumented since I cant find it anywhere in the media object documentation.

To get the url of a photo I need the media.fields=url in the request

1 Like

Thanks for the clarification here.

I was also able to get this to work using this example:

https://api.twitter.com/2/tweets/search/recent?query=nyc&tweet.fields=lang&expansions=attachments.media_keys&media.fields=preview_image_url,url

Thanks for your feedback and I’m glad you got what you need. I’ll pass on your feedback on the documentation.

3 Likes

Hey @jessicagarson ! I was trying to get this to work and couldn’t figure out why my recent search isn’t including the media URLs.

Requesting:
https://api.twitter.com/2/tweets/search/recent?tweet.fields=lang&expansions=attachments.media_keys&query=nyc has:images&media.fields=preview_image_url,url

The request is returning this data, but no media fields :thinking_face::thinking_face::thinking_face:

{
    "data": [
        {
            "id": "1317996486660100106",
            "text": "RT @aloeblacc: In just a few hours, I'm premiering my full @ARTEen  Concert on Facebook. 🎙 Grab your popcorn and tune into https://t.co/XyS…",
            "lang": "en"
        },
        {
            "id": "1317996483489169408",
            "text": "RT @ggukbear: oh??? my god???? genshin impact ads are on nyc trains https://t.co/RIuMlIEMR3",
            "lang": "en",
            "attachments": {
                "media_keys": [
                    "3_1317590532680486912",
                    "3_1317590532680548352",
                    "3_1317590532839833608"
                ]
            }
        },
        {
            "id": "1317996466741350401",
            "text": "RT @CAWBBBB: King of NYC. https://t.co/OFgtP7Jn4B",
            "lang": "en",
            "attachments": {
                "media_keys": [
                    "3_1317966505426874370"
                ]
            }
        }
}

Media keys are expanded in the includes.media payload of the response, where available. Do you see an includes object in your response?

2 Likes

That’s what I was missing :man_facepalming: I didn’t scroll all the way down and expected it to be merged into the orignal tweet :sweat_smile:

Thank you!

1 Like

Does this work on streams as well? I can’t seem to get it working.

For example:

https://api.twitter.com/2/tweets/search/stream?tweet.fields=lang&expansions=attachments.media_keys&media.fields=preview_image_url,url

Note: I’ve added my rules and they work fine. I’ve got back my tweets based on my rules for the various tweet.fields so, I know it’s working. I also get back expansions too. media.fields, on the other hand, don’t seem to return.

Also note: I’ve tested with my own tweets, based on my own rules and have included image attachments.

@jessicagarson I kinda have the same issue as @natetronn - It works for photos, but not for videos.

I can get any other field documented here but not the undocumented url. What should I do?

I’m testing using videos, by the way (rather than photos)

My full request:

https://api.twitter.com/2/tweets/search/stream?tweet.fields=attachments&expansions=attachments.media_keys&media.fields=height,url



Do you see an includes object in your response?

@i_am_daniele There is an includes object in my response, but it shows every field except for url when working with videos.

@editvideobot, Thanks for reaching out. Unfortunately, this is not available for video at this time. I’ll also pass on your feedback about the URL field to our documentation team.

Thanks again!

@natetronn I’m unable to replicate it seems.

Just tested this with the following curl command:

curl https://api.twitter.com/2/tweets/search/stream?tweet.fields=attachments&expansions=attachments.media_keys&media.fields=height,url -H "Authorization: Bearer $BEARER_TOKEN"

And I did get back the URL field inside the includes object for the Tweets containing media.

Hi @jessicagarson, thanks for looking into this for me!

You’re right, it is working fine. My mistake…

Hindsight being 20/20 and all, I think it may be an issue with the Hunter’s Twitter-v2 library. I saw this thread after googling the issue and saw others might have been experiencing something similar, so I jumped in on this one a bit too quick, thinking I might be on to something, I think.

Everything else with url params was working fine with that lib so, it didn’t dawn on me that it could be the issue but, I’ll take it up with him now.

Anyway, sorry for your troubles and thanks again!

1 Like

Interesting! Let me know if we can be helpful in troubleshooting here.

Hi, I’ve just tested your link, I dont recieve any image urls at all?

https://api.twitter.com/2/tweets/search/recent?query=nyc&tweet.fields=lang&expansions=attachments.media_keys&media.fields=preview_image_url,url
  {
    id: '1420751558904631296',
    text: 'President Biden expected to visit NYC’s 9/11 memorial for 20th anniversary of the 9/11 attac
ks.\n' +
      'https://t.co/TJVsGKZrd9',
    lang: 'en'
  },
  {
    id: '1420751557923008512',
    text: 'Are any creative coworking in NYC? I don’t want to go back to working from home full-time an
d would be really, really interested in working out of an office with some lovely folks. Even consideri
ng WeWork Day Passes and doing it more adhoc, so if you’re interested let me know.',
    lang: 'en'
  },

While that link will at least provide some Media Keys, still without the image url that i need.

https://api.twitter.com/2/users/1407591434660761602/tweets?tweet.fields=lang&expansions=attachments.media_keys&media.fields=preview_image_url,url

Thanks for your support.

In the response there is the data part, and then the includes part - the expansions like media keys, are included in includes.media

eg, for

twurl "/2/users/1407591434660761602/tweets?tweet.fields=lang&expansions=attachments.media_keys&media.fields=preview_image_url,url"

The result is:

{
  "data": [
    {
      "id": "1407735945403772928",
      "lang": "de",
      "text": "Juni Release ist veröffentlicht. Wir haben PatientStrength mit neuen Funktionen für den VAD der Schweizer Versicherungen ergänzt. \nKostengutsprachen können so schneller und vor allem für den #Patienten rechtzeitig genehmigt werden. \n\n#Versicherungen #Kostengutsprache #VAD"
    },
    {
      "id": "1407694247751475203",
      "lang": "de",
      "text": "Komplexe Krankheitsbilder benötigen intensive und manchmal aussergewöhnliche #Therapiemöglichkeiten. Mit #PatientStrength digitalisieren wir den #Kostengutspracheprozess und generieren für alle Beteiligten erhebliche Vorteile.\n\nhttps://t.co/ORzopGRiCW \n\n#offlabel #Krebstherapie https://t.co/fgOvDOXudE",
      "attachments": {
        "media_keys": [
          "3_1407694179480768512"
        ]
      }
    },
    {
      "id": "1407598604303441920",
      "lang": "de",
      "text": "PatientStrength ist seit Mai 2021 live und konnte bereits #Spitäler in der Schweiz dabei unterstützen, #Kostengutsprachen für #Medikamente schnell, einfach und sicher zu erstellen.\n\nhttps://t.co/vUknMH2PzQ\n\n#innovation #gesundheitsmanagement #digitaletransformation #oncology https://t.co/JklrHvZepB",
      "attachments": {
        "media_keys": [
          "3_1407598495909953536"
        ]
      }
    },
    {
      "id": "1407592435958616064",
      "lang": "de",
      "text": "Q_PERIOR AG gründet mit #PatientStrength eine eigene Marke die die Vernetzung im #Gesundheitswesen unterstützt. Zum Start wird die Optimierung der #Kostengutsprache umgesetzt, Start im Mai 2021."
    }
  ],
  "includes": {
    "media": [
      {
        "media_key": "3_1407694179480768512",
        "type": "photo",
        "url": "https://pbs.twimg.com/media/E4kiZeBWEAA-rsF.jpg"
      },
      {
        "media_key": "3_1407598495909953536",
        "type": "photo",
        "url": "https://pbs.twimg.com/media/E4jLX88WQAAGHVR.png"
      }
    ]
  },
  "meta": {
    "oldest_id": "1407592435958616064",
    "newest_id": "1407735945403772928",
    "result_count": 4
  }
}
1 Like

Ok, confused now - I dont got this part. “includes

'{"data":[{"created_at":"2021-06-23T16:22:58.000Z","text":"Juni Release ist veröffentlicht. Wir haben P
atientStrength mit neuen Funktionen für den VAD der Schweizer Versicherungen ergänzt. \\nKostengutsprac
hen können so schneller und vor allem für den #Patienten rechtzeitig genehmigt werden. \\n\\n#Versicher
ungen #Kostengutsprache #VAD","lang":"de","id":"1407735945403772928"},{"created_at":"2021-06-23T13:37:1
7.000Z","text":"Komplexe Krankheitsbilder benötigen intensive und manchmal aussergewöhnliche #Therapiem
öglichkeiten. Mit #PatientStrength digitalisieren wir den #Kostengutspracheprozess und generieren für a
lle Beteiligten erhebliche Vorteile.\\n\\nhttps://t.co/ORzopGRiCW \\n\\n#offlabel #Krebstherapie https:
//t.co/fgOvDOXudE","lang":"de","attachments":{"media_keys":["3_1407694179480768512"]},"id":"14076942477
51475203"},{"created_at":"2021-06-23T07:17:13.000Z","text":"PatientStrength ist seit Mai 2021 live und
konnte bereits #Spitäler in der Schweiz dabei unterstützen, #Kostengutsprachen für #Medikamente schnell
, einfach und sicher zu erstellen.\\n\\nhttps://t.co/vUknMH2PzQ\\n\\n#innovation #gesundheitsmanagement
 #digitaletransformation #oncology https://t.co/JklrHvZepB","lang":"de","attachments":{"media_keys":["3
_1407598495909953536"]},"id":"1407598604303441920"},{"created_at":"2021-06-23T06:52:43.000Z","text":"Q_
PERIOR AG gründet mit #PatientStrength eine eigene Marke die die Vernetzung im #Gesundheitswesen unters
tützt. Zum Start wird die Optimierung der #Kostengutsprache umgesetzt, Start im Mai 2021.","lang":"de",
"id":"1407592435958616064"}],"meta":{"oldest_id":"1407592435958616064","newest_id":"1407735945403772928
","result_count":4}}'

Are there some restrictions on the user-accounts? I use a simple node_fetch call. No magic there.

        console.log(url.toString())
        const json = await node_fetch_1.default(url.toString(), {
            headers: {
                Authorization: await this.credentials.authorizationHeader(url, {
                    method: 'GET',
                }),
            },
        }).then((response) => response.json())
        const error = TwitterError_js_1.default.fromJson(json);
        if (error) {
            throw error;
        }
        console.log(util.inspect(json, false, null, true /* enable colors */))
        return json;

Something else is going on because your output does not have includes which usually means that expansions and media.fields were not specified in the URL parameters.

The full url should be:

https://api.twitter.com/2/users/1407591434660761602/tweets?tweet.fields=lang&expansions=attachments.media_keys&media.fields=preview_image_url,url

(i used twurl above GitHub - twitter/twurl: OAuth-enabled curl for the Twitter API )

1 Like

Thanks a lot, that it was :slight_smile:

2 Likes