Sovattha Sok
Sovattha Sok

Reputation: 725

How to get the Twitter followers count using Twitter API (in 2022)?

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

Upvotes: 7

Views: 8968

Answers (2)

moonvader
moonvader

Reputation: 21091

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}

Upvotes: 3

anon
anon

Reputation:

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
    }
  }
}

Upvotes: 10

Related Questions