Exploring the EPA Developer API using JavaScript

APIs have grown increasingly popular over the past years making work easier for developers as they are now able to quickly get data from other websites and integrate it into their own. The Environmental Protection Agency (EPA) provides API access to its data that enables developers to access its extensive data for use in their applications. In this blog, we will explore how to access and use the EPA Developer API using JavaScript.

Getting Started

To get started, we need to obtain an API key from the EPA Developer Central website. After signing up for an account, login to the dashboard, click on the API Keys menu then Generate a New Key to get your unique API key.

Making Requests

The EPA API uses endpoints to retrieve information from its database. You can search by any keyword to get different sets of data as per your requirement. You can use JavaScript to requesting information from the EPA API using fetch or XMLHttpRequest.

Using Fetch API

const apiKey = 'your_api_key';
const endpoint = 'https://enviro.epa.gov/enviro/efservice';

function fetchData(query) {
  return fetch(`${endpoint}/${query}/JSON?${apiKey}`).then(response => response.json());
}

fetchData('TRI_YEARS/REPORTING_YEAR/2018/SUBJECT/ALL_TRANSACTIONS/STATE/VA/COUNTY/LOUDOUN').then(response => {
  console.log(response);
}).catch(error => {
  console.error(error);
});

Using XMLHttpRequest API

const apiKey = 'your_api_key';
const endpoint = 'https://enviro.epa.gov/enviro/efservice';

function fetchData(query) {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', `${endpoint}/${query}/JSON?${apiKey}`);
  xhr.send();
  
  xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      console.log(this.responseText);
    }
  };
}

fetchData('TRI_YEARS/REPORTING_YEAR/2018/SUBJECT/ALL_TRANSACTIONS/STATE/VA/COUNTY/LOUDOUN');

Conclusion

The EPA Developer API is an excellent tool that enables developers to retrieve up-to-date information and integrate it into their objectives by using API requests. Understanding how to retrieve information from the EPA API using JavaScript via the fetch and XMLHttpRequest APIs is a critical prerequisite for developers in the world of APIs and opens up endless possibilities for application creation.

Now that you have understood the fundamentals of accessing the EPA Developer API you can get started with building your own API app. Happy coding!

Related APIs in Government