What is an API and API first principle?
Application Programming Interface (API) is a software mediator that allows two applications to interact with one another. It is an interface that allows one application to interact with another application.
Example
Following is an example of a public API being called through javascript that generates random cat facts.
We can customize this API using this link.
Explanation
In the javascript code, we make a request to an API with the URL https://catfact.ninja/fact?max_length=100
The API returns a random fact about cats with a maximum length of 100 characters.
- Line 1: We create a function named
CatInfo. - Line 2: We create an
XMLHttpRequestobject. - Line 6: We open a
GETrequest on https://catfact.ninja/fact?max_length=100. - Line 9: We call a function when the state of the request is changed.
- Line 10: We change the text of the button with ID
btn. - Lines 12-16: We Check if the request is successful and convert the response text to
JSONand set the element with IDresultwith the cat fact. - Line 20: We send the request.
- Line 22: We call the
CatInfoon button click.
API first principle
API first principle is the development approach in which our APIs are given the utmost importance. Whatever we want to develop is developed with API consumption as the end goal.
API First Principle helps to fulfill the needs of the consumer because in this principle we identify our Key actors, determine what they need, build our APIs according to the feedback we receive, and then build our services. So before writing any code we first make sure our APIs are built according to our consumers. This principle is ideal for microservices
Free Resources