Web APIs – Introduction

A Web API has below features:

  • It can be used to extend the functionality of the browser
  • It can be used to simplify the complex functions
  • It can provide easy syntax to complex code

What is Web API?

  • API stands for Application Programming Interface.
  • A Web API is an application programming interface for the Web only.
  • A Browser API can be used to extend the functionality of a web browser.
  • A Server API can be usd to extend the functionality of a web server.

Browser APIs

All browsers have a set of built-in or pre-default  Web APIs to support complex operations, and to help accessing and parsing the data.

For example, the Geolocation API used to return the coordinates of where the user’s browser is located.

Example :-
<!DOCTYPE html>
<html>
<body>

<p>Click on the button to get your geolocation coordinates.</p>

<button onclick="getYourLocation()">Click me</button>

<p><strong>Note:</strong> This geolocation property is not supported by IE8 and earlier versions.</p>

<p id="result"></p>

<script>
var p = document.getElementById("result");

function getYourLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    p.innerHTML = "Geolocation is not supported by your browser.";
  }
}

function showPosition(position) {
  p.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

Third Party APIs

Third party APIs are not built in to your browser.

To use these APIs, you will have to download the code from the Web.

Examples:

  • Google API – Allows user to use google api like login with google in your web site.
  • Map API – Allows user to display Maps on a web site.
  • Facebook API – Allows user to display Facebook info on a web site.