Note: There's a PR on the Forem repo open for this. Check out if it has been implemented before trying this!
Fortunately, Forem ships with a REST API. At the time of writing this article, it did not support the follower count for the active user natively. For you as a developer, this should be no obstacle to calculating them yourself.
Setting up the project
Prepare the project directory
This project will consist of a single "index.js" file that you can deploy anywhere. I recommend using an Nginx reverse proxy and pm2 to keep the service running.
- Change into a directory of your choice and initialize a new NPM project.
- Install "express" and "axios"
cd /path/to/project
touch index.js
npm init -y
npm i express axios
Generate a dev.to API token
Head over to your dev.to profile and create an API key. The option is at the bottom of the page.
- Enter a name for your key
- Click on "Generate API key"
- Copy it to the clipboard
- Head over to val.town secrets.
- Create an entry with the name "DEVTO_API_KEY"
- Paste your API key into the value field
Add some boilerplate
Add the following code to your index.js file. Make sure to update the API_TOKEN with your secret. Even better: Use the dotenv NPM package to keep your code free of secret keys.
const express = require('express');
const axios = require('axios');
// You can request a token here: https://dev.to/settings/extensions (scroll down to bottom)
const API_TOKEN = 'super-secret';
const CACHE_LIFETIME = 1000 * 60 * 60 * 4; /* 4 hours */
const PORT = 3000;
const app = express();
let cache = {
followers: {
count: 0,
entries: [],
},
};
const gatherFollowerCount = async () => {
cache.followers.count += 1;
};
// Interval to update cache
setInterval(() => {
console.log('Updating cache');
gatherFollowerCount();
}, CACHE_LIFETIME);
app.get('/', (req, res) => {
res.send({ followers: cache.followers.count });
});
app.listen(PORT, () => {
gatherFollowerCount();
console.log('Listening on port ' + PORT);
});
Then, run it:
node index.js
This piece of code
- Create an express server on port 3000
- Maintains an in-memory cache for your followers
- Updates this cache every 4 hours using the "gatherFollowerCount" function
- Sends the cache to a client requesting it on the root path
⚠️ Stay fair, don't overuse the API!
Every API call you make causes traffic, which in turn costs money. If you have a lot of followers, the sum of your requests might be huge. In this case, please adjust your cache lifetime to a higher value.
If you're beyond the 10k, please also consider implementing a queue for your API requests with a generous delay in between
Visit http://localhost:3000 in your browser to check if everything works as expected.
Calculate your follower count
Add the following to the "gatherFollowerCount" function.
const gatherFollowerCount = async () => {
const perPage = 100;
let page = 0;
const fetchFollowers = async () => {
const url = `https://dev.to/api/followers/users?page=${page}&per_page=${perPage}`;
const response = await axios.get(url, {
headers: {
'api-key': API_TOKEN || '',
},
});
cache.followers.entries = [...response.data, ...cache.followers.entries];
cache.followers.count = cache.followers.entries.length;
if (response.data.length === perPage) {
page++;
await fetchFollowers();
}
};
await fetchFollowers();
};
This code
- recursively gathers all followers from the dev.to API
- adds them to your cache
- and updates the follower count
Restart the server and check http://localhost:3000. You should see your follower count in the browser now, ready to be implemented for your personal blog or portfolio.
PS: You can apply the same practice to other dev.to stats that might not yet be part of the official API.