Complete 2024 Web Development Bootcamp

Dr. Angela Yu

Back to API Index


API Requests
Intro

In order to use an API you essentially build a get request, which is very similar to the get requests that we have already been utilizing. But instead or "requesting" a page to be rendered by the server, we're requesting "data" of some form that we will then manipulate and format for our use, and render out to a page.

To demonstrate the concept of API's, we're going to use a "Random Activity API". All API's will have a "Documentation Page" detailing the utilization of their API, and the documentation we need for this API can be found here.

API requests are sent to a specific web address, called a base url. The request(s) then contain Endpoints, Path parameters, and/or Query parameters:

  • Endpoints: are a routing tool used to route your request for a specific response. For example if we use the /random endpoint for our request we will get a response with a random activity.
  • Path parameters: provide a way of returning a specific item. For example, /activity/3943506 would return the specific activity where key = 3943506.
  • Query parameter: are a way to refine your API request for a specific response. For example, /filter?type=education would return all of the activities where type = education.
    • usually, you can add multiple parameters to the query string, by using the "&" for separation, such as queryParam1&queryParam2
  • - As a general rule, API's have many options on how to access and utilize their specific data.
  • - In the examples we just cited, the responses are singularly specific to the "Random Activity API" that we are using.
  • - ALWAYS read the documentation on any API that you wish to utilize, so that you can see how to pull out the specific data you wish to retrieve.
Requests

Ok, let's try making some requests. We're going to be using POSTMAN to test our API requests. If you don't have it installed on your system, it can be downloaded here.

Go ahead and open up Postman and set it up to send GET requests. the base url for our api is:

  • https://bored-api.appbrewery.com

So we'll type that into the address field of our GET request.

Generate Our Requests
  • The first query we'll run is for a random activity, so we'll enter the base url followed by a specific Endpoint:
    • https://bored-api.appbrewery.com/random
  • and when we hit Send in Postman, it should return a random activity.

  • The next query we'll run is for a specific activity, so we'll enter the base url followed by a specific Path parameter:
    • https://bored-api.appbrewery.com/activity/3943506
  • and when we hit Send in Postman, it should return the specific activity related to key = 3943506. We know it's going to return this specific key because we read the documentation.

  • The last query we'll run is a search for all social activities for 2 participants, so we'll enter the base url followed by a specific Query parameter:
    • https://bored-api.appbrewery.com/filter?type=social&participants=2
  • and when we hit Send in Postman, it should return all of the activities where type = social AND participants = 2.


Back to Top