Exploring the Root API using JavaScript

Are you interested in exploring the Root API to access data related to public holidays, employee leaves, and working days? You've come to the right place! In this blog, we will show you how to easily interact with the Root API using JavaScript.

First, let's get started by understanding a few basics. The Root API is RESTful and makes use of HTTP methods like GET, POST, PUT, DELETE, and PATCH to retrieve and manipulate data. We can leverage JavaScript to interact with the API endpoints and get the data we need.

Set up

Before we can start interacting with the Root API, let's get our development environment set up. We will need to install the appropriate libraries using npm.

const axios = require('axios');
const qs = require('querystring');

// Define the Root API endpoint
const ROOT_API_ENDPOINT = 'https://api.roote.io/';

Making requests

Example 1: Get all the public holidays in the US for the year 2022.

const getHolidays = async () => {
  const params = {
    year: 2022,
    country: 'us',
    type: 'public'
  };

  const url = `${ROOT_API_ENDPOINT}holidays?` + qs.stringify(params);
  const response = await axios.get(url);

  console.log(response.data);
};

getHolidays();

Example 2: Get the number of working days in the US in the month of August 2022.

const getWorkingDays = async () => {
  const params = {
    year: 2022,
    month: 8,
    country: 'us',
    type: 'working',
    count: true
  };

  const url = `${ROOT_API_ENDPOINT}calendar?` + qs.stringify(params);
  const response = await axios.get(url);

  console.log(response.data);
};

getWorkingDays();

Example 3: Get all the leave requests for an employee.

const getEmployeeLeaves = async (employeeId) => {
  const url = `${ROOT_API_ENDPOINT}employees/${employeeId}/leaves`;
  const response = await axios.get(url);

  console.log(response.data);
};

getEmployeeLeaves('98765432');

Conclusion

We hope that you have found this blog post helpful in leveraging the Root API using JavaScript. The examples given here are just a few of the many possible ways in which you can interact with the API. Feel free to refer to the official Root API documentation to learn more about the API and explore additional endpoints. Happy coding!

Related APIs in Transportation