Fun with APIs
APIs are all around you. API stands for Application Programming Interface, but I like to think of APIs as an entirely new world hidden behind what you can readily see. For example, look at the world of Harry Potter. To enter a magical world behind the city of London, Harry Potter must learn to run through a wall at Platform Nine and Three-Quarters at King's Cross Station. In the middle of the bustling Muggle (non-magical) crowd, he only sees a brick wall between Platforms Nine and Ten. At first, it appears to be an ordinary solid brick wall, but as he approaches the barrier, he astonishingly discovers access to the magical side of London, which is concealed from the eyes of the Muggles. APIs are like that. They are a world within the regular world that only people who know about them can see. If you know how to go through the door, new possibilities are open to you.
You've probably interacted with APIs without even realizing it. For instance, when you check the weather on your smartphone, you're accessing a weather API that provides up-to-date forecasts and conditions. The same goes for mapping and navigation apps, like Google Maps, which rely on APIs to fetch location data, directions, and real-time traffic updates. APIs are behind the scenes of countless everyday activities, from shopping online to video streaming. Let's look closer at the hidden world that makes data integration possible.
Improve your Wizardry Skills
In this section, we will explore the Harry Potter DB, an API with data from the Harry Potter Universe.
Harry Potter API
API documentation typically covers authentication methods, such as API keys or tokens, and security considerations. As you can see from the Harry Potter API documentation, no authentication is required. The documentation also gives us the base URL. Let's look at how to use this to make requests.
API Endpoints are specific URLs that serve as gateways to access resources provided by the API. Each endpoint represents a data set within the API. The API documentation lets us know what endpoints are available. Here are some example endpoints for Characters, Potions, and Spells from the Harry Potter API.
Let's try one out! Start with the base URL, then the version number and the word "potions," followed by a slug, which is a human-readable unique identifier. For this example, our slug will be "ageing-potion." Try pasting this endpoint into your browser: https://api.potterdb.com/v1/potions/ageing-potion
Did you see something completely unintelligible like this?
Sorry, I should have warned you. But don't worry, there's a reason.
JSON: API Format
The lack of formatting that makes this information hard for humans to read simplifies the application task. The JSON format is intended to be universally compatible with various applications. Let's explore how an application could put this data to use.
JSON organizes data into key-value pairs, making it easy for applications to understand. To display a potion's image, name, and effect, our app selects the relevant data by referencing the keys within the JSON. It finds the "image," "name," and "effect" keys to get the data it needs to display.
Our application can quickly access specific information because data is formatted for simplicity and consistency. But maybe you aren't into potions. Do you like pirates? Let's look at another example.
Talk Like a Pirate
This section will explore Fun Translations, an API that converts English to fictional languages.
The GET Request
To interact with an API, you send HTTP requests. These requests are like messages you use to ask the API to do something, like fetching data. We used a URL to target a specific endpoint for the potion example. We will still use an endpoint, but now we will add a query string. A query is a name and value pair sent in the URL of a GET request.
In this example, you can see the get request, "text=" followed by the value. In this case, the value can be any word or phrase you want to translate into "pirate." Try this example:
You should see something like this in your browser window. You receive structured information in XML format in this response, with clear tags and values. The tag "translated" contains the word "Ahoy," which it sent in response to our request.
Like the JSON format we saw earlier, this is a convenient way for an application to understand and process the data provided by the API. In this case, our application reads the "translated" tag in XML to display the user's words or phrases to the endpoint.
In the GET request, you send the words and phrases you want to translate. You can substitute any word or phrase after the query for anything you want to translate. Fun translations API has lots of endpoints, from Pig Latin to Minion. Change the endpoint to change your preferred translation, then use the query to send a word or phrase to translate. Let's try the endpoint for Mandalorian.
To try more endpoints and languages, you can visit the website here: https://funtranslations.com/
But maybe you 'aren't into pirates or pretend languages. Do you like dogs?
Find Your New Best Friend
In this section, we will explore Dog Breeds API. This API is the gateway to over 20,000 open-source dog images, accessible by over 120 breeds.
API in an Application
Our application uses the Dog API to populate a drop-down list of all dog breeds from the database. When a user selects a breed from the list, the app pulls a random image for that breed to populate the frame.
Here is a section of the JSON data that populates the drop-down menu.
As you can see, the list is extensive, and we've only made it to "bulldog." Imagine how hard it would be to populate a drop-down menu manually. We can seamlessly integrate their extensive collection of dog breed information and images into our application using APIs like Dog API. This saves us the effort of manually curating and updating this data and ensures its reliability, as the API's data source maintains it. Because I didn't have to go around taking random dog photos, you can have a better experience. It's a win-win.
Sorry, here is one more random dog because I couldn't resist. But enough about dogs. Perhaps we should try something a little more serious. How do you feel about space exploration?
Discover Life on Mars
In this section, we will explore NASA Open API. This website offers user-friendly APIs designed to facilitate seamless access to NASA data and imagery.
NASA Open API
API Keys and Authentication
The APIs we have been using don't require a key or authentication. While NASA APIs are open and available for public use, they require an API key for access. By requiring an API key, NASA can track who uses their API, manage usage limits, and ensure responsible access to their data and resources.
NASA APIs allow you to send the API key as a query parameter in the URL. For example, to view an astronomy picture of the day (APOD), you would type the base URL, followed by the endpoint, and then the API key:
In our example, we are using the "mars-photos" endpoint to pull information about current rovers on Mars and the most recent pictures taken by their various cameras. Here is an example of a call to the API endpoint for the Perseverance rover.
The API will respond with data in JSON format. This data will include information about the most recent photos taken by cameras on Perseverance.
Our app will then parse the JSON data to extract the URLs of the images.
The advantage of using an API in your application to access data is that it allows your application to receive automatic updates. APIs often provide access to real-time or near-real-time data, meaning your application can stay updated with the latest information without manual intervention. Data updates automatically, which reduces the need for manual maintenance and data management, saving time and resources.
APIs are all around us, enabling data access and functionalities in everyday apps and services. They're the silent enablers that power services like weather forecasts, navigation, and data integration, making our digital world look seamless.
Documentation acts as your guide, providing access methods and essential details. Endpoints are like gateways to specific data sets, each serving a distinct purpose. Data formats like JSON and XML may seem complex but are ideal for applications to retrieve and process data efficiently. APIs enhance data accuracy and keep applications current. APIs are the tools that seamlessly integrate services and data, making our applications work effortlessly.
Congratulations. You've made it beyond Platform Nine and Three-Quarters. Just as Harry transcends a seemingly ordinary brick wall to enter the wizarding world, APIs offer a passage to a concealed digital universe, accessible to those who know how to open the door.
But wait! There's More…
Please take a few minutes to play with some of the applications we've developed through open APIs. You can learn to talk like a pirate, find a furry friend, explore life on Mars, and browse through the enchanting array of potions from the wizarding world of Harry Potter.
About Lisa Stringer
Lisa Stringer is a digital learning designer at Award Solutions, where she creates stories to help people learn more about technology topics like Python, cyber security, and 5G.
About Award Solutions, Inc.
Award Solutions is the trusted training partner to the world's best networks. We help companies tackle new technologies by equipping their teams with knowledge and skills. Award Solutions invests heavily in technology, research, engineering, and labs to ensure our customers make the most of their resource and network investments.
Award’s expertise extends across many technologies including 5G/LTE Access 5G/4G Core, VoNR/VoLTE, Transport Networks, Telco Cloud, Virtualization and Orchestration, Data Automation, and more.
Check Out Our Public Webinars On This and Other Subjects.
Award Solutions’ Subject Matter Experts (SMEs) offer public, LIVE webinars each month for you to enjoy.