A Minecraft Struggle Wiki
Advertisement
A Minecraft Struggle Wiki

Notability

I don't really think too many people know about my API for A Minecraft Struggle. Well, let's dive right into it!

Feel at Home with the A Minecraft Struggle API

There are two links that you really need to know:

  1. https://glitch.com/edit/#!/a-minecraft-struggle, for learning more about what's up with everything sent from the API
  2. https://a-minecraft-struggle.glitch.me/, the beginning of the URL to get data from the API

Go ahead and click the 1st link above. Keep it open in another tab while we do these things, we'll be using it later to understand the data sent back from the API.

Now, before we begin, I'll tell you that there are 2 types of complete URLs to receive actually-useful data from the API:

  1. https://a-minecraft-struggle.glitch.me/<type>/<name>
  2. https://a-minecraft-struggle.glitch.me/<page>

For now, let's try using the 1st type of complete URLs!

Go ahead and finally click on the 2nd link all the way above. Yay, we got some actually-useful data! ... Right? Of course not!

This is because we did not supply any information, thus the API yields us an empty page titled, well, "Untitled"!

As of writing this, there are two <type>s that are available:

  • players
  • mobs

Let's choose the <type> of players. With the "s"! I'll tell you why later (here's a hint: "<3").

Currently, we have the following semi-complete URL:

https://a-minecraft-struggle.glitch.me/players/<name>

As of writing this, there are two <name>s under the <type> of players that are available:

  • speedothreesixty
  • imegahh

Just because I like myself and you should like me too <3, let's choose the <name> of speedothreesixty. Not just "speedo", and without the ".json"!

Great! Go ahead, in that page that sadly just says "Untitled", click on the address bar, then type /players/speedothreesixty to the end of it and press "Enter". Optionally, you could also close that "Untitled" page and go to this link:

https://a-minecraft-struggle.glitch.me/players/speedothreesixty

Please note that these URL types are not case-sensitive. This link would yield the same results:

HTTPS://A-Minecraft-Struggle.Glitch.Me/Players/SpeedoThreeSixty

That's the beauty of all this!

So, now, you have something called "JSON GET Response Data". You probably don't understand some of it. Go back to the Glitch link you opened before that I said explained more about this and give it a read!

Let's go on to how to use this "JSON GET Response Data" within your own Node.js code!

Implementing the A Minecraft Struggle API into your Node.js Code


I Neither Understand nor Have Node.js! Help?

  1. You can download Node.js at https://nodejs.org/en/download/current/.
  2. You can check if Node.js was successfully installed by executing node -v and npm --version in your terminal.
  3. You can read more docs about Node.js at https://nodejs.org/dist/latest-v15.x/docs/api/.


Setup & Installing the node-fetch npm Package

Execute these commands in your terminal if you haven't already:

  1. mkdir <your new folder name>
  2. cd <your new folder name>
  3. <your favorite text editor open command> .

Now, you should be in the folder inside of your favorite text editor.

Create a new file named index.js. Your text editor should automatically go to that new file.

Copy and paste the code below into that file:

   // const fetch = require("node-fetch")
   async function getJSON(url, property) {
       // const responseJSON = await fetch(url.toString()).then(res => res.json())
       if (property) {
           // return responseJSON[property]
       } else {
           // return responseJSON
       }
   }
   const BASE_AMS_API_URL = "https://a-minecraft-struggle.glitch.me/" // *
   // * "AMS" stands for "A Minecraft Struggle"

Now we have a function that will get "JSON GET Response Data" from a website, typically an API, and a constant variable that we can use to get data from the API.

Go back to your terminal and execute the following commands:

  1. npm init -y
  2. npm i node-fetch

Note: You can instead run npm init if you wish to specify your own package.json details for your project.

Wait for npm to say that the node-fetch npm package was successfully installed, and then you can change the following lines of code in that file as follows:

  • // const fetch = require("node-fetch") to const fetch = require("node-fetch")
  • // const responseJSON = await fetch(url.toString()).then(res => res.json()) to const responseJSON = await fetch(url.toString()).then(res => res.json())
  • // return responseJSON[property] to return responseJSON[property]
  • // return responseJSON to return responseJSON

To wrap it all up, you can use this new function by using this code:

   const myData = await getJSON(`${BASE_AMS_API_URL}<type>/<name>`, myProperty)

OR:

   const myData = await getJSON(`${BASE_AMS_API_URL}<page>`, myProperty)

Note: <page> is essentially useless as an HTML webpage cannot be transferred into "JSON GET Response Data".

Advertisement