What is API?

API is one of the many three-letter acronyms that you encounter early on as a developer, but which many who do not deal with programming also come into contact with. API stands for Application Programming Interface, on Abbreviationfinder website, approximately application programming interface, which, however, does not reveal as much about what API actually is.

Simply explained, one can say that an API is a set of rules for how different programs can communicate with each other. You can liken it to a menu in a restaurant, which is a specification of what can be ordered by the restaurant. When you have said what you want, the restaurant will fix the cooking and will come back with your order when the food is ready. Similarly, an API can provide a description of what data can be requested and how to do it.

The family concept of being able to enter a URL in the browser and get to the right website is made possible by an API. When you type, for example, https://www.gradinmath.se/ in their browser and click on Enter, a so-called HTTP request is sent, whereupon answers come in the form of gradinmath’s website.

API

Another everyday example that illustrates how APIs make life easier for both developers and users is Mobile BankID. Let’s say that an association develops an app and wants Mobilt BankID to be used for secure login. To integrate Mobile BankID, an API is used, which is a framework for how the association app communicates with Mobile BankID. The communication transformed into an everyday conversation could look something like this:

The association app: “Hi Mobile BankID, now we have a user in our app here who would like to log in with Mobile BankID. The person has the social security number yyyymmdd-nnnn. Can you help with that? ”

Mobile BankID. “Hey! We will solve that. Tell the user to open their Mobile BankID app so they can legitimize themselves. ”

[The user identifies and the API verifies that it is the right person]

Mobile BankID: “So there, now the user has identified himself and everything is green!”

The association app: “Brilliant, then I send the user to the logged in view. Thanks for the help!”

At gradinmath, APIs are widely used when it comes to system integrations. When information contained in a company’s business system is to be used in another system in the company’s IT support, an API is used to send requests between the systems.

Practical example with SMHI

At the beginning of this text, I used an open API that allows to retrieve data from SMHI’s weather forecasts. The concept of API may sound complicated, but in this case it’s just a few lines of code. All it takes to accomplish this yourself is basic knowledge of HTML and JavaScript and a little googling about how the SMHI API works.

In the case of HTML, a paragraph with a span tag suffices to insert the temperature (though without line breaks):

<p>
At gradinmath’s Linköping office, there
are currently
<span id = “temperatureSpan”> </span>
degrees.
</ p>

I’ve put an ID on the span tag to be able to access it smoothly in the JavaScript code. How exactly this API works is unknown in advance, but you simply find information about it. With a simple googling of the “SMHI API” you come to this page . At the bottom of the page it says: “Information about the services for developers can be found at opendata.smhi.se/apidocs
/metfcst/index.html ” On this page you can, for example, click on “Get Forecast” for information on how to retrieve weather forecast data . In a prominent box with the heading “Syntax” it says exactly how the call should be designed, namely this (though without line breaks):

/ api / category / {category} / version
/ {version} / geotype / point /
lon / {longitude}
/lat/{latitude}/data.json

The words in seagulls should be replaced with other text depending on the data you are looking for. According to the instructions on the SMHI website, {category} should be replaced with pmp3g and {version} with which version of the API you want to use (for example 2). {longitude} and {latitude} should be replaced by the coordinates of the point where you want a weather forecast. Before the whole harangue should https://opendata-download-metfcst.smhi.se written. gradinmath’s office in Linköping has the coordinates 15.629488, 58.409064. This gives an API call according to (but without line breaks):

https://opendata-download-metfcst.smhi.se
/ api / category / pmp3g / version / 2
/ geotype / point
/lon/15.629488/lat/58.409064/data.json

If you type that address into a browser, you get a lot of data in a format called JSON. The information is related to the weather at the specified coordinates in the coming hours. JavaScript uses the “fetch” function with exactly the same URL to retrieve the information. The next question will be how to get information about the temperature right now. The current temperature is found in the JSON structure by adding “.timeSeries [2] .parameters [11] .values ​​[0]”. Depending on the number you type in the bracket after timeSeries, you get a forecast for a given hour, the number for parameters determines what value you want to retrieve (everything from air pressure to precipitation) and 0 for values ​​you only write a value for temperature. Finally, the span tag is located where we want to insert the temperature and the current temperature is inserted into the span tag. The full JavaScript code looks like this (though without line breaks):

fetch (“https: // opendata-download
-metfcst.smhi.se/api/category/pmp3g/
version / 2 / geotype / point / lon / 15.629488
/lat/58.409064/data.json”)

.then (response => response.json ())
.then (data => {
const temperatureSpan =
document.querySelector (“# temperatureSpan”);
temperatureSpan.innerHTML =
data.timeSeries [2] .parameters [11]
.values ​​[0];
});

It was that simple!

Here is an example of a slightly extended functionality where you can choose which city with gradinmath office you want to see the temperature for and also which city where it is hottest and coldest right now.

At the office in Linköping it is currently: 16.8 degrees

It is hottest at the office in Stockholm and coldest at the office in Borlänge .

Contact us if your company wants help developing an API. We are happy to help!