When I get a tweet via eg. timeline API, how can I tell what kind of card it links to?



As far as I can tell, if a tweet links to a twitter card, the only way I can discover that is by parsing the expanded_url contained in the urls in the tweet data and seeing if “cards.twitter.com” is in the domain of the expanded_url. (Correct me if I’m wrong!)

Those are clickable URLs though. I think I can parse out the card ID from the URL, but it seems in the ads API there are separate endpoints for fetching each type of card. Eg. for an app download card it would be GET accounts/:account_id/cards/app_download/:card_id as described at https://dev.twitter.com/ads/reference/get/accounts/%3Aaccount_id/cards/app_download/%3Acard_id

So if I have a tweet, and I want to construct a request to the right ads API endpoint to fetch the card linked to by that tweet, how would I do that?



(updated to correct how I get the card URL … sorry, original post contained several errors)


There’s no direct support for Cards in the public API, the only place they are supported is in the Ads API, so not all card types are easy to detect or display. At the moment there’s no supported way for you to achieve what you are doing outside of the Ads API piece (I’ve moved this topic to the Ads section for the moment).


Thanks Andy. Let me phrase it a different way then. Is there any way in the ads API to find out what card is associated with a tweet? (We are developing a product that is using the ads API to create Cards anyway. The difficulty comes with integrating tweets or cards that were created by the same user but outside this product, eg. directly on Twitter.)


For any card hosted by Twitter (cards.twitter.com), the Ads Account ID and the Card ID are contained in the URL. With access to the Ads API and the advertiser’s ads account, you can get the meta data of the card and then performance data from the stats endpoints (this is trickier) via the API.

Unfortunately, there is no direct card metadata lookup based on the URL in the Card back to a Tweet. That’s a useful feature, and we’ll bring it up to the team.


Thanks @rchoi! Re. your last sentence, I was trying to go from tweet to card, not card to tweet.

I figured we could parse the card ID out of the URL as you say, but I don’t see an endpoint at https://dev.twitter.com/ads/creative that would allow getting a card without knowing its type, which doesn’t seem to be in the card URL contained in a tweet … the endpoints are all of the form GET accounts/:account_id/:card_type/:id . I think we may end up scraping the card URL and getting the embedded card info that way.


From Tweet to Card, can you get the URL, HTTP get the HTML and parse out the card info?

Certainly not ideal, but again we don’t have that lookup from URL function.


Yep, that should work. Thanks.


Hi @slinkp23! You could also get all types of cards in an array, then index the array by card ids, and then you could just do $cards[$cardIdFromTweet]->card_type. I’m doing it this way because I’m doing something like an import and I need all cards anyway. If you think about doing it this way too, let me know and I’ll share my code.

Actually, @rchoi, as far as I can see, if I make an HTTP GET on the card url of the form https://cards.twitter.com/cards/:account_id/:card_id, it redirect instantly for all card types, without any content, so I don’t see how this could work. At least this is how it behaves for me.


Thanks @majoritasdev, I’ll take a look at that option when we get to ingesting cards data.