API, How It Helps To Build Applications

Photo by Kevin Ku on Unsplash

 

API stands for Application Programming Interface. Imagine the following scenario, where a web and mobile application shares the same data. For a web application, you can access the database layer by writing server-side code in PHP or ASP.net, etc. But what about a mobile application? Here arises the need for a well-defined API that provides access to data to any requesting application. An API avoids the complexity of interacting with database layer coding for each platform. The usage of API is now very common.

How API Helps?

Weather API is one of the best basic examples one can use to understand the benefits of API. This API [https://openweathermap.org/api] provides real-time data from a huge weather database containing data from around the world. Using this API, we can show the weather update in our application. This is one such example where API is used. Another example I can think about is the Products API provided by Amazon. Using this Products API, a developer can integrate thousands of Amazon products in a Web or Smartphone application.

Parsing API

Every programming language has its own set of libraries to parse data from the API. Using these libraries, a developer can parse the data and rendered it in a meaningful way as required by the end-user. Recently, I used a library called Retrofit in my latest Android application. It handles does all the heavy lifting API parsing, that frees a developer from writing code for efficiently parsing the API.

How API is Requested and parsed.

The following API request shows an API request and its response. Note that the request from the browser includes q=London, which tells the API to return the weather details of London. Next step is to parse the API response and render the output to appropriate html.

API Request: https://samples.openweathermap.org/data/2.5/weather?q=London,uk
API Response:
{“coord”:{“lon”:-0.13,”lat”:51.51},”weather”:
[{“id”:300,”main”:”Drizzle”,”description”:”light intensity drizzle”,”icon”:”09d”}],”base”:”stations”,
“main”:{“temp”:280.32,”pressure”:1012,”humidity”:81,
“temp_min”:279.15,”temp_max”:281.15},
“visibility”:10000,”wind”:{“speed”:4.1,”deg”:80},
“clouds”:{“all”:90},”dt”:1485789600,”sys”:
{“type”:1,”id”:5091,”message”:0.0103,”country”:
“GB”,”sunrise”:1485762037,”sunset”:1485794875},
“id”:2643743,”name”:”London”,”cod”:200}

We can parse the above JSON data to display data in the UI elements. The main advantage of an API is that it can bring data to any platform, like iOS App, Android App or a Web site.

Read our app development experience with an external API.