Browsers are tools specifically designed to access web resources. Web APIs, the types of APIs that are your focus in this course, provide guided access to applications through web requests. That's why your browser is a perfectly capable tool for accessing resources via a Web API. In this lesson you'll learn what a Web API is and access one with your web browser. Let's give it a go!
What is a Web API?
In the previous lesson, you were introduced to APIs. While the basic premise of all APIs remains the same, they can take on many different forms based on the type of interaction they intend to provide.
A Web API defines how different software applications can communicate over the internet. It serves as a bridge between systems, enabling data exchange and the ability to perform operations without knowing how the other system works internally. It doesn't matter what language each system is written in or where they are hosted; they can communicate in a standardized way defined by the API.
Web APIs are generally built using standard web technologies like HTTP, which allows them to be accessed by a variety of different clients, including web browsers, mobile apps, and of course other web apps! They play a crucial role in modern web development, powering interactions between applications, services, and users across the web.
World Country Information
The REST Countries API is a great example of a well-documented, free, and stable API. You're gonna use your browser to explore it a bit.
Basic Documentation Structure
In the website's sidebar click on Name to scroll to an example request based on a country name:
You can see that on the right the website always provides a generalized view on how to use that endpoint, as well as a concrete example.
Info: You can think of the endpoint of a Web API as the URL you are using to interact with it
Let's first look at the generalized example, where you can see that the part of the URL you are meant to replace with your own input, is wrapped into curly braces {}:
https://restcountries.com/v3.1/name/{name}
Just below that, the site also gives you an example request with the {name} part filled out, e.g.:
https://restcountries.com/v3.1/name/united
Notice that this specific endpoint doesn't require you to use a full country name, but is able to handle inputs that will match multiple resources.
Make An API Request
Now what can you do with this URL? Let's do with it what you usually do with a URL: pop it into your browser's address bar and hit Enter. You will see something scary looking like this below:
Lots of text, right?! This is a JSON response. JSON is a common data format for delivering data over the web via an API. The format is a de-facto standard that makes it easy to interact with if you're an application yourself. But... as a human it's kind of hard to see what's going on here without a little bit of formatting.
Format Your JSON Response
So go ahead and copy all that text and paste it into an online formatter. This will reveal the structure of the data you received, and makes it much easier to read also for our fleshy human brains. As you can see now, JSON is actually quite readable for humans as well, which is one of the reasons it has established itself so well as a format over the past years.
There are also browser-extensions that automatically format a JSON response for you in the window you opened it in, which makes your explorations even easier. If you give it a try and install e.g. this extension, your browser response will look like this:
Info: If you are using Mozilla Firefox for your browser, it has a built-in JSON formatting so you won't have to worry about getting an extension or pasting the raw data into a formatter.
Keep in mind that this is the same data as the one in the screenshot further up, it's just been formatted in a more readable way for your eyes. A program that goes and reads this data doesn't care for whitespace in JSON.
Try More Endpoints
Now that you've seen how to make an API request to the REST Countries API, go ahead and try some of its other endpoints. You can access their example request structure via the docs in the same way you did for the /name/ endpoint.
Try Another API
Once you feel like you understand what's going on in this API, try to make some requests in your browser to a different API.
You can search for another API via this curated list or try some fun ones mentioned in this article and explore some of the API's content through your browser, just like you did above.
On the following page you will download a professional tool built for working with APIs and use it to do the same thing we just did with your browser. Once you're ready, head to the next page to download and install Postman.
Summary: What is a Web API?
- A web API provides guided access to applications through web requests
- An endpoint for a web API is the URL you use it interact with it
- Endpoints typically provide response data in a JSON format
- JSON has become the de-facto standard as it is easy for both machines and humans (once properly formatted) to interact with