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
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):In case you want to get followers count not by user id but using user name
Also you will need to create app in Twitter Developer Portal, receive auth token and use it in your request’s authorization header