Back

Step by Step Guide to Building a BambooHR API Integration in Python

Aug 14, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of BambooHR API integration? You're in the right place. BambooHR is a powerful HR management platform, and its API opens up a world of possibilities for automation and data integration. We'll be using the PyBambooHR package to make our lives easier. Let's get cracking!

Prerequisites

Before we jump in, make sure you've got:

  • A Python environment set up (I know you've got this!)
  • A BambooHR API key (if you don't have one, hit up your friendly neighborhood HR team)

Installation

First things first, let's get PyBambooHR installed:

pip install PyBambooHR

Easy peasy, right?

Authentication

Now, let's authenticate with BambooHR:

from PyBambooHR import PyBambooHR bamboo = PyBambooHR(subdomain='your_subdomain', api_key='your_api_key')

Replace 'your_subdomain' and 'your_api_key' with your actual credentials, and you're good to go!

Basic API Operations

Retrieving Employee Data

Let's grab some employee data:

employee = bamboo.get_employee(123) # Replace 123 with an actual employee ID print(employee['firstName'], employee['lastName'])

Updating Employee Information

Need to update some info? No sweat:

bamboo.update_employee(123, {'department': 'Engineering'})

Handling Custom Fields

BambooHR lets you create custom fields. Here's how to work with them:

custom_fields = bamboo.get_custom_fields() print(custom_fields)

Advanced Operations

Fetching Reports

Want to pull a report? Here's how:

report = bamboo.request_company_report(1) # Replace 1 with the report ID print(report)

Managing Time Off Requests

Let's check out some time off requests:

time_off = bamboo.get_time_off_requests() print(time_off)

Handling Webhooks

Webhooks are a bit more complex, but super useful. Here's a basic setup:

from flask import Flask, request app = Flask(__name__) @app.route('/webhook', methods=['POST']) def handle_webhook(): data = request.json # Process the webhook data return '', 200 if __name__ == '__main__': app.run()

Error Handling and Best Practices

Always handle those pesky rate limits:

import time try: # Your API call here except PyBambooHR.PyBambooHRError as e: if 'rate limit exceeded' in str(e).lower(): time.sleep(60) # Wait for a minute before retrying # Retry your API call else: raise

Example Use Case: Simple Employee Directory

Let's put it all together with a basic employee directory:

def get_employee_directory(): directory = [] employees = bamboo.get_employee_directory() for employee in employees: directory.append({ 'name': f"{employee['firstName']} {employee['lastName']}", 'job_title': employee.get('jobTitle', 'N/A'), 'department': employee.get('department', 'N/A') }) return directory print(get_employee_directory())

Testing and Debugging

Always test your code! Here's a simple unit test:

import unittest from unittest.mock import patch class TestBambooHRIntegration(unittest.TestCase): @patch('PyBambooHR.PyBambooHR.get_employee') def test_get_employee(self, mock_get_employee): mock_get_employee.return_value = {'firstName': 'John', 'lastName': 'Doe'} employee = bamboo.get_employee(123) self.assertEqual(employee['firstName'], 'John') self.assertEqual(employee['lastName'], 'Doe') if __name__ == '__main__': unittest.main()

Conclusion

And there you have it! You're now equipped to build some awesome integrations with BambooHR. Remember, the PyBambooHR documentation is your friend if you need more details. Now go forth and code, you magnificent developer, you!