Written by KomoDeFi Editorial Team on June 4, 2019

Using Insomnia to Get API Data

Getting Started with API tools:

When working with CEX and DEX data we will most likely interact with REST APIs. To GET simple REST API response data in JSON format we will use a simple tool called “Insomnia”. This is a free tool that is used to Debug REST (Representational State Transfer) APIs.

https://insomnia.rest/

 

Why Insomnia?

Because it is free and really easy to use tool. We can get all the details on every response. Advanced features allow us to view status code, body, headers, cookies, etc. Custom headers and queries, API authentication is an easy approach. The intuitive UI lets us GET JSON data by simply entering URLs, which then returns data in JSON format.

 

API Endpoint: https://jsonplaceholder.typicode.com/todos/1
Response Example:
{

 "userId": 1,

 "id": 1,

 "title": "delectus aut autem",

 "completed": false

}

 

The API endpoint is the URL for GET data. I am writing GET in the capital because there is GET, POST, PUT, PATCH, OPTIONS, DELETE, HEAD are methods associated when working with REST APIs. An endpoint can be edited to get more specific data. These alterations are called parameters at the end of the URL after a question mark.

 

API Endpoint with Parameter: https://api.crex24.com/CryptoExchangeService/BotPublic/ReturnTicker?request=[NamePairs=BTC_LTC]

 

Response Example:
{

 "Error": null,

 "Tickers": [

   {

     "PairId": 3,

     "PairName": "BTC_LTC",

     "Last": 0.015164000000000,

     "LowPrice": 0.015154000000000,

     "HighPrice": 0.015682000000000,

     "PercentChange": -2.4384,

     "BaseVolume": 0.79338914208023000000000,

     "QuoteVolume": 51.49890682,

     "VolumeInBtc": 0.79338914208023000000000,

     "VolumeInUsd": 4140.1636078974496700000000000

   }

 ]

}

This API endpoint specifically returns BTC/LTC pair data as URL parameters have restricted it to do so. Data can be displayed in browser but Insomnia is faster and more formal. It provides us a wide range of options to make it easier to work with APIs. To know more about REST APIs and JSON please read the following article.

https://raygun.com/blog/soap-vs-rest-vs-json/

Advanced Usage (Headers, Queries)

When we work with public API endpoints it is just a matter of send and go, but if we work with secure APIs that use authentication via API keys there are a few steps associated with Insomnia. Using authenticated APIs is really easy with insomnia. Most APIs provide API keys within the URL. In some cases, we have to manually enter the URL in headers or a query string parameter name. Doing this in insomnia is really easy we will go through coinmarketcap API as an example for this.

 

Authenticating API with Headers

  • Navigate to the Header Menu this should be under the URL bar.
  • Paste the header name first in the “New Header” text field and the API key in the “New Value” text field.



Authenticating API with Body

  • Navigate to the Query Menu.
  • Paste the query string parameter name in the “New Name” text field and the API key in the “New Value” text field.

Authenticating APIs with headers is the most preferred method. While authentication using query is also convenient.

Back to Top
Experience Web3
© 2024 komodefi.com