Using the FBI wanted API with JavaScript

The FBI wanted API allows developers to access information about their most wanted lists. In this tutorial, we'll show you how to use this API with JavaScript to retrieve data from the FBI wanted list.

API Description

The FBI wanted API provides publicly accessible information about their most wanted lists. There are several collections of data available through this API, including:

  • Wanted - This collection contains information about wanted persons on the FBI's most wanted lists
  • Missing - This collection contains information about missing persons on the FBI's most wanted lists
  • Unknown - This collection contains information about unidentified individuals on the FBI's most wanted lists

Each collection has specific endpoints that allow developers to retrieve data from that collection.

JavaScript Example Code

Let's look at an example of how to use the FBI wanted API with JavaScript to retrieve data from the 'Wanted' collection.

First, we need to create a function that we will use to send a request to the FBI API and retrieve data. We will be using the fetch method to send a GET request to the FBI API URL.

function getFBIWantedList() {
  fetch('https://www.fbi.gov/wanted/api/wanted')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));
}

This function sends an HTTP GET request to the https://www.fbi.gov/wanted/api/wanted URL, which is the endpoint for the 'Wanted' collection. The fetch method returns a Promise object, which we then use to handle the response data.

We use the response.json() method to convert the response into a JSON object, which we then log to the console using console.log(data). If there is an error with the request, we handle it using the catch method and log the error message to the console using console.error(error).

Now that we have our function set up, we need to call it to retrieve data from the FBI wanted list.

getFBIWantedList();

Running this code will send a GET request to the FBI wanted API and retrieve the data from the 'Wanted' collection. You can modify this code to retrieve data from the 'Missing' or 'Unknown' collections by changing the API endpoint URL.

Conclusion

In this tutorial, we learned how to use JavaScript to access the FBI wanted API and retrieve data from their 'Wanted' collection. With the help of the fetch method and some basic JavaScript, you can easily retrieve data from any of the FBI wanted collections.

Related APIs