This weekend I have found myself in the way-too-common situation of wanting to code, but not knowing what to code. So, being away from home with my laptop, and I still can't bring myself to rewrite my website's theme (I got bored with the current one), I decided to write a very simple sideproject. A trivia API.
The idea
What if you could have an open-source API for simple trivia questions? One GET request for getting the question and answer options, 1 correct answer, an explanation of the answer after you answered.
I have decided to add 3 GET requests (only 2 implemented at the time of writing this), and one POST:
GET /daily: Get the API's dailyQuestionObject. A random question from the JSON pile is selected every 24hrs, or every time you restart the service.GET /random: Get a randomQuestionObjectevery time you call it.GET /question/:ID: Not implemented. It should allow you to get a specificQuestionObjectevery time.POST /answer/:ID: Allows you to send aAnswerObjectfor a specific question. It will check the answer against the correct one, and reply with a Status 200 for correct answer and Status 400 for the wrong one, plus aResponseObjectwith the correct answer, and the explanation.
Implementing it
I wrote it in node and Express.js. It's what I first learned for web backend, and what I am more comfortable with. The code itself is not really that impressive. You read stuff from a JSON. You serve it through Express. In fact, you can find the code on my Codeberg profile.
I wrote the code in maybe 2-4 hours. It's not that impressive or hard. But it was fun. It was also weirdly fun populating the initial questions with generated code from Copilot. After I wrote the JSON structure, it just started recommending more and more questions, so I just tabbed them in. There's even a funny one where the GenAI hinted that the Amazon rainforest is in Asia (and I kept it in for the lols).
I really like day-projects like this. Random things that you can just write in one sitting, and post them on the internet, in case anyone else needs it or wants it. And "when the work is done, it is forgotten. That's why it lasts forever."
Public API, for your fun:
I filtered the API through my blog app, until I get my api. subdomain up. So here are some public endpoints that you can use in your projects, if you want:
https://critte.ro/api/trivia/question: routes to openTrivia's/randomhttps://critte.ro/api/trivia/answer/:ID: routes to openTrivia's/answer
You can find an example frontend in the Codeberg repo, if you want to use it.
Or you can try it here!
The question at the end of this post (or any post on my blog), is basically the frontend that I wrote for it. Answer the question! (if you don't see it, the service is probably down). K thx byeee