This is how I set up a twitter feed API endpoint in NodeJS with NGINX, Ruby and T etc. This can be used to integrate SEO or automate Twitter post analytics or automate posts to twitter.
This guide assumes you have an Ubuntu server, nodejs (nginx) and setup ruby, rails and t twitter command line utility setup. Also, you will need to have an API setup and working
Ensure your API is configured in NGINX /etc/nginx/sites-available/default
I set up a placeholder GET routine in my node process that we will be used to communicate with the Twitter command line utility.
app.get('/api/appname/v1/feed/twitter/',function(req,res){ var data = { "Feed":"" }; data["Feed"] = "Working v0.1"; res.json(data); });
I started 1 instance of the API on a server with pm2 linked to https://keymetrics.io/.
View API Status with PM2
You can restart the API manually with PM2 by typing
I like to use https://paw.cloud/ software to test API’s over say Postman, I can easily see the test placeholder API is working.
The browser works for now but as soon as I add authentication the browser will fail, Paw FTW.
A simple way to authenticate the API is for the caller to pass a known token when requesting the page via an HTTP POST packet.
app.post('/api/appname/v1/feed/twitter/',function(req,res){ var data = { "Status":"" }; var wwxapi = req.headers['x-access-token']; var data = { "Feed":"" }; var errData = ""; if (wwxapi == undefined) { console.log(" 499 token required"); errData = {"Status": "499", "Error": "valid token required"}; res.statusCode = 499; res.send(errData); res.end(); } else { var xpikeypass = false; // Check the API Key against known API Keys if (wwxapi == "appname-##############################") { xpikeypass = true;} // Debug API Key if (xpikeypass == true) { // app request validated data["Feed"] = "Twitter Feed API Working v0.2"; res.json(data); } else { console.log("499 token required"); errData = {"Status": "499", "Error": "valid token required"}; res.statusCode = 409; res.send(errData); res.end(); } } });
Don’t forget to restart the API after changes
Now we can change the API endpoint to a POST (instead of GET) and paste the code above and test it. If we pass an invalid “x-access-token” value the API will return error 409 as designed.
Passing the right “x-access-token” will return data (200 OK).
Now we can link the Twitter command line utility with the NodeJS API.
Now we can test the NodeJS API and have is spawn a subprocess and hit Twitter. In this case, we are finding the latest posts on Twitter with NodeJS in them.
Works a treat 🙂
Up next
- Schedule getting data from the Twitter command line utility in crontab and save to Redis
- Process the CSV and save new Twitter changes to Redis
- Query Redis and send returned data back to the API.
- Change the API to read from Redis in NodeJ.S
- Secure.
Donate and make this blog better
Ask a question or recommend an article
[contact-form-7 id=”30″ title=”Ask a Question”]
v1.2 Works