API stands for Application Programming Interface, and it refers to a
mechanism of communication between two software systems. For example,
an API may be used to connect a web application’s back-end server to
the front-end client. Communication between these systems involves
requests and responses between the server and the client. The
endpoints used can be obtained from the API’s documentation.
API Protocols
There are three main API protocols: representational state transfer
(REST) APIs, remote procedural call (RPC) APIs, and simple object
access protocol (SOAP) APIs. Of these, REST APIs are the most popular
due to its flexibility and implementation, ease of integration,
support for various data formats including JSON, XML, and plain text,
and high scalability.
REST APIs
REST APIs use HTTP requests (GET, PUT, POST, PATCH, DELETE) to access
and use data. The response data is typically sent in the body of the
HTTP response. Check out this
GeeksforGeeks article
for information on each of these HTTP requests. The specific endpoints
used to make these requests vary between software systems and are
defined in the API documentation. For example, the Coinbase exchange
rates API can be used to find out the current exchange rates for
different currencies. From the
API Docs, the HTTP Request (GET https://api.coinbase.com/v2/exchange-rates)
takes one optional argument, the currency, as a query parameter.
Practical Example
Specify currencies and press the
Get Exchange Rate button
to get exchange rates using the Coinbase API
Output:
This example uses the JavaScript's
fetch to make a GET
request to the Coinbase API as shown:
var apiUrl = `https://api.coinbase.com/v2/exchange-rates?currency=${baseCurrency}`;
// Make the API call
fetch(apiUrl)
.then((response) => response.json())
.then((data) => {
// Handle the API response and display the result
var rate = data.data.rates[convertedCurrency];
var result = amount * rate;
document.getElementById("result").innerHTML =
`Output: ${amount} ${baseCurrency} equals ${result.toFixed(2)} ${convertedCurrency}`;
})
API Authentication: Securing Connections
REST APIs are stateless, meaning they store no data between requests.
Therefore, for APIs that require authentication, authentication must
be done with every request.
API authentication
typically involves either authentication tokens or API keys. Due to
the simplicity in use, API keys are commonly employed for paid APIs,
and are either sent as query parameters or in the request header.
Free APIs: Exploring Without Authentication
APIs like the Coinbase API used for the demo do not require
authentication. Other commonly used free APIs include:
…and many more. You can find a list of over
200 free APIs here.
Conclusion
In conclusion, understanding APIs is pivotal in modern software
development, where seamless communication between different systems is
essential. APIs, or Application Programming Interfaces, act as
bridges, facilitating the exchange of data between software
components. Whether for enabling data exchange between web
applications or accessing diverse functionalities through free APIs,
the understanding of APIs is a valuable asset for developers
navigating the intricate web of software systems.
Happy coding!