Introduction
This article is a part of a series of articles covering the Zenefits API in depth, and covers the specific use case of using the Zenefits API to Get employee details from Zenefits API.
You can find all the other use cases we have covered for the Zenefits API along with a comprehensive deep dive on its various aspects like authentication, rate limits etc here.
Get Employee Details from Zenefits API
Overview
The Zenefits API allows you to retrieve detailed information about employees within a company. To get the first name, last name, manager name, and date of joining for each employee, you will need to use multiple API endpoints. Below is a step-by-step guide with Python code snippets to achieve this.
Step-by-Step Guide
1. Get All Employees
First, you need to retrieve the list of all employees in the company using the /core/companies/{company_id}/people
endpoint.
import requests
def get_employees(company_id, access_token):
url = f"https://api.zenefits.com/core/companies/{company_id}/people"
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
return response.json()["data"]["data"]
company_id = "your_company_id"
access_token = "your_access_token"
employees = get_employees(company_id, access_token)
2. Get Employee Details
For each employee, retrieve detailed information using the /core/people/{id}
endpoint.
def get_employee_details(employee_id, access_token):
url = f"https://api.zenefits.com/core/people/{employee_id}"
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
return response.json()["data"]
employee_details = [get_employee_details(emp["id"], access_token) for emp in employees]
3. Extract Required Information
Extract the first name, last name, manager name, and date of joining from the employee details.
def extract_employee_info(employee):
first_name = employee.get("first_name")
last_name = employee.get("last_name")
manager_url = employee.get("manager", {}).get("url")
date_of_joining = employee.get("employments", {}).get("data", [{}])[0].get("hire_date")
manager_name = None
if manager_url:
manager_id = manager_url.split("/")[-1]
manager_details = get_employee_details(manager_id, access_token)
manager_name = f"{manager_details.get('first_name')} {manager_details.get('last_name')}"
return {
"first_name": first_name,
"last_name": last_name,
"manager_name": manager_name,
"date_of_joining": date_of_joining
}
employee_info_list = [extract_employee_info(emp) for emp in employee_details]
4. Display the Information
Finally, display the extracted information.
for info in employee_info_list:
print(f"First Name: {info['first_name']}, Last Name: {info['last_name']}, Manager: {info['manager_name']}, Date of Joining: {info['date_of_joining']}")
Knit for Zenefits API Integration
For quick and seamless access to Zenefits API, Knit API offers a convenient solution. By integrating with Knit just once, you can streamline the entire process. Knit takes care of all the authentication, authorization, and ongoing integration maintenance, this approach not only saves time but also ensures a smooth and reliable connection to your Zenefits API.