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.