Imagine if you could easily communicate with anyone in the world, even if you didn´t share a common language! From a web development point of view, that is one of the problems Web APIs aim to solve.

Introduction

An API acts as an interpreter between two different systems or applications, for example a website and a server. What if you needed some complex work done, let´s say an advanced electronics installation. In general, it is usually much quicker and easier to simply pick up the phone and call an electrician, rather than to get the education you would need to do the work yourself.

That is another thing APIs do; they let you make that phone call (request) to the electrician (server), who then do that complex work they are trained (programmed) to do and finally responds back to you when the work is done. As with the case of the electrician, it is often quicker and/or safer to let the servers code handle some logic and functionality. Especially since such resources, if located on a server, can be shared between multiple websites and applications. But remember, you most likely wouldn´t call an electrician to change a broken light bulb.

Another example would be if you walked into a restaurant and, instead of a menu, you were handed a list of all the ingredients the kitchen had to offer. And you would have to specify which ingredients the chef should use and exactly how to prepare them. This is yet another problem an API solves; it takes a lot of ingredients (functions and content) and packages them in a way that is easy to use – like a menu of the functionality the server provides. You don’t have to know exactly what is there and how it works, you only need to know how to ask for what you want. In more general terms an API (acronym for Application Programming Interface) is used to let one system, application or website communicate with another system, without really knowing – or caring – how that other system works. A Web API is an API used by a server to make it easy for websites to talk to the server.

A practical example

So, now when we have a rough idea of what an API is, let's see how we can use it in an application.

Let´s say you are building a website and want to add some information or functionality that would be quite time consuming to add on your own. Or maybe you want to save some information in a way which lets you get it again from some other place or device. That is when you would use an API.

Instead of having to manually add a lot of information that is already available somewhere else or figuring out complex logic or functionality that someone else has already written, you can reuse those existing resources.

You do this by making a request to an API. Which API to use and what resources you want from it is specified with an URL, just like when you are navigating to a web page. (Sometimes you will also have to send data with your request, for example when you want to save some information to a server. But for now, let us focus on the URL.)

The API receives your request, asks the server for the information you requested and then responds back to you with that information packaged in a somewhat readable way

In our example code we use an open API (open APIs are free for anyone to use, it is good to know that many APIs are not publicly available) found at https://swapi.co/api, which is the first part in our request URL. We then specify what resources we want the API to respond with by adding to that URL. Here we are asking the API for /people and we also specify that we want a single, specific entry from the list of people (that is our random number). So, our full request URL could be https://swapi.co/api/people/3, change the number 3 to another number to get another entry. Once we have sent our request to the API, we wait for it to respond back with the requested data. And upon receiving the APIs response we process that data and display it in a readable way on our webpage.

Check out the full example and try it on CodePen.

What more can you do with an API?

So now you can also go ahead and try to fetch other data from the Star Wars API. You can use the following URLs to fetch:

  • Planets: get all https://swapi.co/api/planets/ or one https://swapi.co/api/planets/(any number 1-61)
  • Starships: get all https://swapi.co/api/startships/ or one https://swapi.co/api/planets/(any number 1-37)
  • Films: all https://swapi.co/api/films/ or one https://swapi.co/api/films/(any number 1-7)

There are other famous API’s that you can use to fetch data for your projects. Here are just some of them:

Twitter

Makes requests to Twitter for specific data, e.g. the users home timeline or their own statuses, or a request to post a tweet for a specific user. Example: One use would be to show tweets and notifications in real time in your application.

Facebook

There are a few Facebook API but the Graph API is primary way for apps to read and write from/to Facebook. With this API you can access pages, users, posts, groups, events and more. Example: Write to your own Facebook wall or list your Facebook friends from within you application.

Google Maps

A basic use case of the Google Maps API is plotting places, such as local search results, as markers on a map. You might also add paths for multiple points. More advanced usage creates interactions between frontend code and the map – for example, click a search result and highlight the place on the map. Example: Render a simple google map in your application

YouTube

You can use the YouTube API to retrieve and manipulate YouTube resources like videos, channels, and playlists. Example: Let the users watch YouTube videos in your application.

Nasa

NASAs API provides some incredible data, including its spectacular imagery, data about near asteroids, images made by the Rover vehicle. Example: Display NASAs photo of the day on your website or application.

Yahoo weather

Returns basic weather information such as current conditions, current temperature and forecasts. Example: A simple weather app that fetches daily weather forecast

Authorisation

In most cases you will be asked to perform authorisation or authentication before you can use the API. First, a short definition of terms:

  •  Authentication: Proving correct identity 
  • API Key: A long string that you include in the request as a way to identify yourself (authenticating you to use the API)
  • Authorisation: Allowing a certain action (ex. allowing you to fetch the data)

Authentication and authorisation is used mainly for security reasons, but also to keep track who is using the API. The owners of the API need to make sure that:

  • Users can not make unlimited amounts of API calls without any kind of registration
  • The wrong person could intercept or access private information and steal it
  • Track who is making the requests, and for what purpose are the APIs used
  •  

Always check the APIs documentation for the authorisation process. APIs have usually good step by step instructions.

Free/Premium

APIs It is also important to know that some APIs can be free of charge or you will need to pay for their usage. Most common cases is where the API creators provides a freemium version with limited usage (for ex. number of API calls in a period of time) and a premium plan with unlimited usage and possibilities for developers to access more functionalities.

Bodil, Sandra & Frida

 

 

Om Tech

Tech-bloggen har vi satt upp för att visa vad vi gör på teknik-sidan inom Svenska Spel. Du kommer att kunna läsa inlägg skrivna av oss som brinner för utveckling, arkitektur och DevOps.  

Mer om bloggen och oss som skriver

Senaste inläggen