Fetching your Neocities data [Updated]
What to do when you want to show your cool Neocities profile stats on your website? Why, ask the API of course! Neocities has an interesting end point that will let us do just that.
The other day I was browsing the Neocities Developer API page and I came across an endpoint called GET /api/info. This endpoint allows us to get information about a certain user by supplying the username to the request. In return we get data like views, creation date and others.
Naturally I wanted to try and make it work from my Neocities page, and of course, I couldn't. I figured it could be the same issue I encountered working on the Discord article, so I tried to create a middleman server that would format the requested data in a way that our sites could get access to it.
Of course my hosting wouldn't work either, so I had to use Heroku. I swear, I'll move it to permanent hosting later.
Update: I have now put up the script on a paid server, so it should run better!
Anyway, boring stuff aside, lets get our data. Here's what you gotta do:
- We'll need jQuery to send our requests, so import it if you haven't already. You can either download it from the official site, or copy a CDN link. Typically it goes in the <head> part of your website.
Now lets add some code to connect to our server. It will look just like our Discord example, but with a different link. Add this code somewhere before your </body> tag, or in your existing <script> tag:
Hint: If you don't plan on using a particular line of code, you can comment it out by placing // in front of it.
Now, all you have to do is include a <span> with the appropriate class where you want your data shown! For example:
Update: here is a Bitbucket repo for those of you wishing to see the code or to host your own instance. Enjoy!
And you're done! Of course you aren't limited to just printing your code, you could do all kinds of cool stuff with it. For example, response.tags returns an array, which you could use to create lists.
In conclusion, I'll come back to this article to update it when I have moved the server to permanent hosting, because Heroku enjoys lasting downtimes for free users. And I should probably add some sort of caching to prevent spam. Anyway, I hope you enjoyed! If you find any errors or you need help making it work, let me know! I'll be glad to help. Thank you to everyone who followed me recently, I reply the same to everybody, but I swear I am not a bot, *beep boop*. Hope this article was of use.