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

2 Answers 2

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
    }
  }
}
3
  • Indeed, I completely passed near the additional user fields. developer.twitter.com/en/docs/twitter-api/data-dictionary/… Thank you! Jan 20, 2022 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 5, 2022 at 6: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 5, 2022 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}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.