Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
Application Programming Interfaces (APIs) allows different software applications to communicate with each other.
APIs are commonly used in software development to facilitate integration between different software components or services. This allows developers to build on top of existing functionality without needing to understand the inner workings of the systems they are integrating with.APIs provide a simplified interface for developers to interact with. APIs define the methods and data formats that applications can use to request and exchange information, enabling seamless interaction between various systems. Usually APIs return data as a JSON object.
A web service is a resource that is available over the internet. An API that transfers information over the internet it is called a web service.
When using APIs, it is useful to investigate the structure of the JSON object that is returned.
Example of a dictionary API (Run Example). The API is from https://dictionaryapi.dev/
<!DOCTYPE html> <html> <head> <title>English dictionary API example</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <meta http-equiv="Content-Language" content="en" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> window.addEventListener('load', getMeaning) function getMeaning() { const word = `money` const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}` fetch(url) .then(response => response.json()) .then(jsonData => { console.log(jsonData[0]) }) } </script> </head> <body> <h2>Press F12 to view the JSON structure in the console</h2> </body> </html>
It can be useful to display the keys in a JSON object. Write code to display the keys from the dictionay example code above, as shown here. Note: you should write generic code that will show the keys for any given JSON array.
Adjust the code above to allow a user to provide a definition for any English word, as shown here.
Use your generic code that shows the keys for any given JSON to look at the keys returned by calling https://www.themealdb.com/api/json/v1/1/categories.php, as shown here.
Using the API at https://www.themealdb.com/api.php, write code to allow a user to select meals, as shown here.
Using the API at https://www.frankfurter.app/docs/, write code to allow a user to convert from any of the available currencies to euro, as shown here.
Some APIs need a key. Using the Google Cloud Translation API, write code to allow a user to translate from English to French, as shown here.
APIs do no have to return a JSON object. Using the API at https://flagsapi.com, write code to display country flags, as shown here. To get the country codes, use the dataset from this link, which is based on the dataset from https://restcountries.com/v3.1/all).
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.