How to get the Twitter followers count using Twitter API (in 2022)?
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
}
}
}