It has been quite some time since I began thinking about learning programming, but two months actively into the actual learning. Over time I had been wondering how some websites I visit have access to a large amount of data, some of which is data that could not be their own. My worries were demystified about 2 weeks ago when I came across Application Programming Interfaces (APIs).
Simply put, APIs are an interconnection between different computer programs. This includes how one interacts with social media apps like Facebook to log in and transmit messages, checking weather parameters online, looking up hotels online and doing booking. APIs that have been published on the internet for anyone to use freely are called open or public APIs.
There are 4 main API methods which correspond to the CRUD operations of persistence storage. These include:
- GET: Retrieve information about the API resource
- POST: Create an API resource
- PUT: Update an API resource
- DELETE:Delete an API resource or related component
In modern JavaScript, with the introduction of promises, fetch command is used to send a GET request to an API. It returns a promise in itself, hence the json() method is used to return the actual json data from the response object.
POSTMAN is a great tool as it is used to do a mock up of the API methods without writing any code. Once familiar with the operations, one can actually implement them into javascript or any language they are familiar with. API data is in JavaScript Object Notation (JSON) format, which is more like javascript objects with slight differences as below.
{
"monsters": [
{
"name": "Chronos",
"age": 4005.302453418598,
"description": "Effulgence eldritch shunned foetid. Ululate gibbering tenebrous foetid iridescence daemoniac. Stench nameless gambrel. Amorphous furtive iridescence noisome. Foetid mortal nameless.",
"id": 1
},
{
"name": "Tartarus",
"age": 1874.4913565609456,
"description": "Cyclopean swarthy amorphous singular accursed furtive non-euclidean stygian. Swarthy gibbering charnel eldritch daemoniac gibbous. Cyclopean lurk hideous tentacles squamous immemorial tenebrous mortal. Madness tentacles furtive mortal foetid decadent. Foetid immemorial comprehension.",
"id": 2
}
]
}
It is therefore important to convert data to JSON when updating an API resource using POST method. This is done using the JSON.stringify() method.
The json server is used to mock an actual database, and can be helpful during learning. One has to start the json server by running the following command on the terminal:
json-server --watch db.json
APIs are therefore a great resource in accessing data that could be otherwise difficult to create.
Top comments (0)