6

I am surprised to see that there is no way to simply get the number of followers for one Twitter account.

I found many answers (Follower count number in Twitter, How to obtain follower count in Twitter API 1.1?, How to get followers count from twitter, Twitter follower count number) which are way too old as they either use services that do not exist anymore, or make use of the Twitter API v1.1 (instead of the current v2 API).

I found an interesting way (but unsupported) to get the number of followers, using the code of their follow button.

https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=elonmusk

However, I am looking for an official way to retrieve this what I think a simple data from Twitter.

Also note that the official Twitter v2 API does not allow to fetch a count but only to list the followers page per page, which is far from what I try to achieve.

https://api.twitter.com/2/users/44196397/followers
Sovattha Sok
  • 675
  • 1
  • 7
  • 18

2 Answers2

9

Yes, there is a simple way to do this using the Twitter API v2!

The follower and following counts for a user are part of the public_metrics fields in the User object.

The API endpoint and parameters are in the format

https://api.twitter.com/2/users/[ID]?user.fields=public_metrics,[any other fields]

Here's an example of the output, using the twurl command line tool (which handles the authentication etc, you may need to use a library to help with OAuth):

$ twurl -j "/2/users/786491?user.fields=public_metrics,created_at"
{
  "data": {
    "id": "786491",
    "username": "andypiper",
    "created_at": "2007-02-21T15:14:48.000Z",
    "name": "andypiper.xyz",
    "public_metrics": {
      "followers_count": 16570,
      "following_count": 3247,
      "tweet_count": 134651,
      "listed_count": 826
    }
  }
}
Andy Piper
  • 11,422
  • 2
  • 26
  • 49
  • Indeed, I completely passed near the additional user fields. https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/user Thank you! – Sovattha Sok Jan 20 '22 at 21:37
  • Is it possible to get followers count by user name? Or additional request to fetch user Id by user name is needed? – moonvader Feb 05 '22 at 06:24
  • 1
    @moonvader Yes, you can use the twitter username: `https://api.twitter.com/2/users/by/username/TwitterDev?user.fields=public_metrics` – aimfeld Mar 05 '22 at 18:11
3

In case you want to get followers count not by user id but using user name

https://api.twitter.com/2/users/by/username/{username}?user.fields=public_metrics

Also you will need to create app in Twitter Developer Portal, receive auth token and use it in your request's authorization header

Authorization: Bearer {token}
moonvader
  • 19,761
  • 18
  • 67
  • 116
  • I am trying to get the followers list for a specific Twitter user, can you please guide me, here is the question link: https://stackoverflow.com/questions/76726633/get-twitter-account-followers-using-api-and-google-apps-script – EagleEye Jul 20 '23 at 13:14