37

I looked through the API documentation but couldn't find it. It would be nice to grab that number to see how popular a url is. Engadget uses the twitter share button on articles if you're looking for an example. I'm attempting to do this through javascript. Any help is appreciated.

Xavier
  • 8,828
  • 13
  • 64
  • 98

12 Answers12

78

You can use the following API endpoint,

http://cdn.api.twitter.com/1/urls/count.json?url=http://stackoverflow.com

Note that the http://urls.api.twitter.com/ endpoint is not public.)

The endpoint will return a JSON string similar to,

{"count":27438,"url":"http:\/\/stackoverflow.com\/"}

On the client, if you are making a request to get the URL share count for your own domain (the one the script is running from), then an AJAX request will work (e.g. jQuery.getJSON). Otherwise, issue a JSONP request by appending callback=?:

jQuery.getJSON('https://cdn.api.twitter.com/1/urls/count.json?url=http://stackoverflow.com/&callback=?', function (data) {
    jQuery('#so-url-shares').text(data.count);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="so-url-shares">Calculating...</div>

Update:

As of 21st November 2015, this way of getting twitter share count, does not work anymore. Read more at: https://blog.twitter.com/2015/hard-decisions-for-a-sustainable-platform

Goran Jakovljevic
  • 2,714
  • 1
  • 31
  • 27
user799188
  • 13,965
  • 5
  • 35
  • 37
  • 15
    it supports jsonp, just add &callback=my_callback – Davorin Nov 20 '12 at 13:04
  • 1
    Both `http://cdn.api.twitter.com/` and `http://urls.api.twitter.com/` are not officially public or documented, but they are the same and they work fine. There's a new endpoint the official widget is using now, I posted it below, just for sharing. – mcont Sep 08 '15 at 09:50
  • 9
    As of October 2015, Twitter will be deprecating this endpoint https://twittercommunity.com/t/a-new-design-for-tweet-and-follow-buttons/52791 – jimbo2087 Sep 29 '15 at 08:17
  • 1
    This (Twitter private) API endpoint will be shut down on November 20, 2015. – Ville Laurikari Oct 14 '15 at 15:53
  • Hahaha, it was working until few hours ago, then it stopped and I wondered why. Now I know ;-) – Eduardo Nov 21 '15 at 00:40
56

This is not possible anymore as from today, you can read more here:

https://twitter.com/twitterdev/status/667836799897591808

And no plans to implement it back, unfortunately.

Up vote so users do not lose time trying out.

Update: It is however possible via http://opensharecount.com, they provide a drop-in replacement for the old private JSON URL based on searches made via the API (so you don't need to do all that work).

It's based on the REST API Search endpoints. Its still new system, so we should see how it goes. In the future we can expect more of similar systems, because there is huge demand.

Goran Jakovljevic
  • 2,714
  • 1
  • 31
  • 27
  • 1
    Well, it's not sure yet that they won't implement it again. In their latest blogs posts, they were asking for some feedbacks and were mentioning in could happen again. A guy asked them directly on the twitter forum: https://twittercommunity.com/t/equivalent-of-url-count-in-tweets-in-the-near-future/57454 – Anselme Dec 03 '15 at 23:32
5

this is for url with https (for Brodie) https://cdn.api.twitter.com/1/urls/count.json?url=YOUR_URL

Mada Aryakusumah
  • 611
  • 11
  • 16
  • 2
    This actually is the correct url to query for tweet counts, https or not. http://urls.api.twitter.com/1/urls/count.json?url= is not a public API and shouldn't be used. – Tom Tom Aug 22 '14 at 10:54
4

Yes,

https://share.yandex.ru/gpp.xml?url=http://www.web-technology-experts-notes.in

Replace "http://www.web-technology-experts-notes.in" with "your full web page URL".

Check the Sharing count of Facebook, Twitter, LinkedIn and Pinterest

http://www.web-technology-experts-notes.in/2015/04/share-count-and-share-url-of-facebook-twitter-linkedin-and-pininterest.html

Update: As of 21st November 2015, Twitter has removed the "Tweet count endpoint" API.

Read More: https://twitter.com/twitterdev/status/667836799897591808

Poonam Gupta
  • 416
  • 6
  • 8
4

No.

How do I access the count API to find out how many Tweets my URL has had?

In this early stage of the Tweet Button the count API is private. This means you need to use either our javascript or iframe Tweet Button to be able to render the count. As our systems scale we will look to make the count API public for developers to use.

http://dev.twitter.com/pages/tweet_button_faq#custom-shortener-count

Community
  • 1
  • 1
André Paramés
  • 934
  • 9
  • 23
2

The approved reply is the right one. There are other versions of the same endpoint, used internally by Twitter.

For example, the official share button with count uses this one:

https://cdn.syndication.twitter.com/widgets/tweetbutton/count.json?url=[URL]

JSONP support is there adding &callback=func.

mcont
  • 1,749
  • 1
  • 22
  • 33
2

I know that is an old question but for me the url http://cdn.api.twitter.com/1/urls/count.json?url=http://stackoverflow.com did not work in ajax calls due to Cross-origin issues.

I solved using PHP CURL, I made a custom route and called it through ajax.

  /* Other Code */
  $options = array(
    CURLOPT_RETURNTRANSFER => true,   // return web page
    CURLOPT_HEADER         => false,  // don't return headers
    CURLOPT_FOLLOWLOCATION => true,   // follow redirects
    CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
    CURLOPT_ENCODING       => "",     // handle compressed
    CURLOPT_USERAGENT      => "test", // name of client
    CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
    CURLOPT_TIMEOUT        => 120,    // time-out on response
);
$url = $_POST["url"]; //whatever you need
if($url !== ""){
    $curl = curl_init("http://urls.api.twitter.com/1/urls/count.json?url=".$url);
    curl_setopt_array($curl, $options);
    $result = curl_exec($curl);
    curl_close($curl);
    echo json_encode(json_decode($result)); //whatever response you need
}

It is important to use a POST because passsing url in GET request cause issues.

Hope it helped.

steo
  • 4,586
  • 2
  • 33
  • 64
1

This way you can get it with jquery. The div id="twitterCount" will be populated automatic when the page is loaded.


     function getTwitterCount(url){
         var tweets;
         $.getJSON('http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=?',             function(data){
         tweets = data.count;
         $('#twitterCount').html(tweets);
     });
     }
     var urlBase='http://http://stackoverflow.com';
     getTwitterCount(urlBase); 

Cheers!

Cassio Seffrin
  • 7,293
  • 1
  • 54
  • 54
  • The urls.api.twitter.com endpoint is [forbidden](http://stackoverflow.com/a/9031032/1269037) from public use. – Dan Dascalescu Mar 20 '15 at 21:52
  • 1
    Thks @DanDascalescu, sorry for my later feed back, when I wrote the answer this was possible but now it's not anymore. Please disregard my answer above, here is the twitter justification: https://twitter.com/twitterdev/status/667836799897591808 – Cassio Seffrin Sep 06 '16 at 03:52
1

Yes, there is. As long as you do the following:

  1. Issue a JSONP request to one of the urls:

    http://cdn.api.twitter.com/1/urls/count.json?url=[URL_IN_REQUEST]&callback=[YOUR_CALLBACK]

    http://urls.api.twitter.com/1/urls/count.json?url=[URL_IN_REQUEST]&callback=[YOUR_CALLBACK]

  2. Make sure that the request you are making is from the same domain as the [URL_IN_REQUEST]. Otherwise, it will not work.

    Example:

    Making requests from example.com to request the count of example.com/page/1. Should work.

    Making requests from another-example.com to request the count of example.com/page/1. Will NOT work.

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
1

This comment https://stackoverflow.com/a/8641185/1118419 proposes to use Topsy API. I am not sure that API is correct:

Twitter response for www.e-conomic.dk:

http://urls.api.twitter.com/1/urls/count.json?url=http://www.e-conomic.dk

shows 10 count

Topsy response fro www.e-conomic.dk:

http://otter.topsy.com/stats.json?url=http://www.e-conomic.dk

18 count

Community
  • 1
  • 1
Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
  • 1
    topsy has in-house twitter feed analyzer, i think they load twitter data in realtime and have different aggregation strategy than twitter. – panchicore Jul 29 '12 at 21:16
  • but they have some really small cap for requests - no ? – mireille raad May 09 '13 at 06:50
  • @Andriy Kuba: It's not showing any result for http://otter.topsy.com/stats.json?url=http://www.e-conomic.dk in Google Chrome. Do you know why? – Mona Jalal Oct 19 '13 at 01:31
0

I just read the contents into a json object via php, then parse it out..

<script>
<?php
$tweet_count_url = 'http://urls.api.twitter.com/1/urls/count.json?url='.$post_link;
$tweet_count_open = fopen($tweet_count_url,"r");
$tweet_count_read = fread($tweet_count_open,2048);
fclose($tweet_count_open);
?>
var obj = jQuery.parseJSON('<?=$tweet_count_read;?>'); 
jQuery("#tweet-count").html("("+obj.count+") "); 
</script>

Simple enough, and it serves my purposes perfectly.

TiKi
  • 17
  • 1
0

This Javascript class will let you fetch share information from Facebook, Twitter and LinkedIn.

Example of usage

<p>Facebook count: <span id="facebook_count"></span>.</p>
<p>Twitter count: <span id="twitter_count"></span>.</p>
<p>LinkedIn count: <span id="linkedin_count"></span>.</p>
<script type="text/javascript">
    var smStats=new SocialMediaStats('https://google.com/'); // Replace with your desired URL
    smStats.facebookCount('facebook_count'); // 'facebook_count' refers to the ID of the HTML tag where the result will be placed.
    smStats.twitterCount('twitter_count');
    smStats.linkedinCount('linkedin_count');    
</script>

Download

https://404it.no/js/blog/SocialMediaStats.js

More examples and documentation

Javascript Class For Getting URL Shares On Facebook, Twitter And LinkedIn

Per Kristian
  • 785
  • 8
  • 10