In the UK we have a chef called Jamie Oliver and over the years he has created lots of TV episodes and also a book on how to make a complete family meal in 30 Minutes – The premise of this, is that time doesn’t have to be an excuse not to make healthy food for the family.
Now, if you’ve ever seen these episodes or tried to follow this, it takes a lot longer than 30 minutes!! It’s more about a “mindset”, which is a lie, it’s about having everything prepped and ready to go so all you’re doing is cooking the food and not chopping things up and getting the pots and pans ready.
So what does this have to do with APIs?!
In my current context, making requests to a RESTful API and analysing the response data is something I’m perfectly comfortable with doing but that’s only because I’ve been working within that space for a couple of years now. I truly take for granted many of the skills that I’ve learnt over the last couple of years and I forget, that you need to start somewhere, in order to build up your knowledge of a certain area.
There are loads of public APIs out there that you can use but these can be tricky to set up. In my experience, most of the documentation that comes with these is confusing and sometimes it’s lacking vital information. Another way forward is to create your own basic API but this can be a daunting task coding it yourself, although a very rewarding once you get it working!
What if there was an even easier way that would allow you to have control over the requests that you make and the data that you want to see in the response?!
I’ve found this node module called json-server that is very handy and super quick to get you started. It’s only a couple of commands from a terminal and you are up and running and making requests within seconds….seriously!
The 30 second API mindset…
So following Jamie’s lead, before starting we need to do our Mise en place…We require a couple of things in place before we get cooking.
- Download and Install Node.js
- Download and Install Postman
- Create a new Folder on your machine
- Open a Terminal in the new directory
Your starting point should look something like this – depending on your terminal/Postman colour preferences…
First of all, we need to globally install the json-server module. In the open terminal, type the following command:
npm install -g json-server
Once this has been installed you’re basically ready to make requests. Quick right?! As you have installed the module globally (using the -g flag), the server can be started from any directory but we will be using our newly created one.
In the terminal, type:
As you do not have a db.json file in that directory, it will conveniently create one for you and populate this with some default values. These are good for now, just to get you started and making basic requests. Later in the post, we will talk through how to create data files containing the data that is more relevant to your context.
We’ll be using Postman to request the data from the db.json file but it’s entirely up to you, use something that you feel comfortable using. You can now send a request and get a response containing data from the file. The available routes are displayed in the terminal, you can use either of these routes in your first request.
Copy one of the routes below and paste this straight into Postman, you will get the data returned from that Endpoint.
Sending a GET request is easy right, the best thing about the json-server module is that it gives you the ability to also practice sending POSTs, PUTs and DELETEs. I would attempt go through each one of these methods but the documentation on the json-server GitHub page explains these methods better than I ever could. This also covers a number of awesome features that you can make use of when using the application.
I don’t want to use the Default data file…
Fear not, as this is essentially just a JSON file that you’re using, you can have any data you desire. You’ll need to stay within the applications format for the way it parses the routes/endpoints from the .json file but apart from that, it’s up to you!
I’ve used mockaroo to create some random data. You can grab this from here and save the .json file to the same directory. Stop the current server instance (Ctrl+C) and spin up a new one using the new dummyUsers.json file:
If like me, you have applications running on certain ports on your local machine, you will need to either stop that process or give json-server a different port to use. This can be done from the command line before you start the server:
json-server <file.json> -p <port number>
When making changes to the .json file manually in a text editor you will need to stop and start the server instance that you have running, for the changes to take effect, which obviously sucks because you want to be able to see your changes straight away. Luckily, there’s a command line flag to get over this:
json-server –watch <file.json>
Any manual file changes made will be automatically detected and the server will be restarted for you and you’ll be able to continue making those lovely requests.
To Finish Off…
So that’s it, you now have a platform to practice making requests to an API that you have control over and all without writing any code. I’ve only covered a small part of what json-server offers during this post, I would advise you to have a look through the GitHub page once you’re comfortable using the basic function of the application because it offers so much more.
Good luck and let me know if you find this useful. If you have any questions just drop me a message!!
An Extra Helping…
I’ve added some more information about using a collection within Postman to make different types of requests and interact with the dummyUsers.json data. Hope it helps!!