Track Twitter Follower Growth Over Time Using A Serverless Node.js API on AWS Amplify
In March 2021 I started to use FeedHive to help me grow an audience on Twitter. Recently, I wanted to check how my Twitter followers have grown over time. Unfortunately, Twitter Analytics only provides data from the last 30 days. So I decided to develop a simple serverless API to fetch and store my follower count each month.
Tech Stack
As I already use AWS Amplify for some private APIs, I wanted to reuse this framework for this new project.
For this new project I need the following components:
- React for the frontend web application which will fetch the data from my serverless API
- AWS API Gateway which provides traffic management, CORS support, authorization and access control, throttling, monitoring, and API version management for the new API
- AWS Lambda with Node.js that fetches the follower count from Twitter API
- AWS DynamoDB which is a NoSQL database and which will store the follower count
Fetching follower count from backend
The first step is to add a new Node.js REST API to our Amplify application that provides a /twitter
endpoint which is triggered on a recurring schedule. In my case, it will be on every 1st day of the month. The official documentation will help you to set up such a new REST API.
To be able to fetch the follower count from Twitter API I decided to use FeedHive's Twitter Client. This library needs four secrets to be able to access Twitter API. We will store them in the AWS Secret Manager, my article "How to Use Environment Variables to Store Secrets in AWS Amplify Backend" will guide you through this process.
After the API is created and pushed to the cloud, we can write the basic functionality to fetch the Twitter followers inside our AWS Lambda function:
The next step is to add DynamoDB support to be able to store a new follower count and get a list of the stored data.
Therefore, we need to add a new storage to our AWS Amplify application, see "Adding a NoSQL database" for detailed instructions.
We are adding a NoSQL table that has the following columns:
id
: A unique string identifier for each row as a stringfollower_count
: the current follower count as numberdata
: an ISO timestamp string that represents the time when the follower count was fetched
Now, we need to allow our Lambda function to access this storage:
Finally, we can use the AWS SDK to store and read from DynamoDB:
A successful API response will have a similar JSON array in its body:
Show data in frontend
To be able to show the data in the React frontend I use the Recharts library which is "a composable charting library built on React components".
The React component is quite simple and uses the AWS Amplify REST API library to fetch the data from our API endpoint:
which results in such a graph:
Conclusion
Using serverless functions it is quite easy and cheap to build a custom solution to track Twitter follower growth over time.
What do you use to track your follower growth? Leave a comment and tell me about your solution.
If you liked this article, follow me on Twitter to get notified about new blog posts and more content from me.
Use Git Bisect to Find the Commit That Introduced a Bug
As a developer you know that situation: the code worked like a charm and suddenly there is a bug but you have no idea where and when it was introduced.
Why I Picked Vue.js as My Freelancer Niche
I have professional experience with the three big players in web development: Angular, Vue.js and React. I've reached the point in my career where I need to choose one of the three frameworks/libraries that I will use for my future freelancing career.