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!
Before we jump in, make sure you've got:
First things first, let's get PyBambooHR installed:
pip install PyBambooHR
Easy peasy, right?
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!
Let's grab some employee data:
employee = bamboo.get_employee(123) # Replace 123 with an actual employee ID print(employee['firstName'], employee['lastName'])
Need to update some info? No sweat:
bamboo.update_employee(123, {'department': 'Engineering'})
BambooHR lets you create custom fields. Here's how to work with them:
custom_fields = bamboo.get_custom_fields() print(custom_fields)
Want to pull a report? Here's how:
report = bamboo.request_company_report(1) # Replace 1 with the report ID print(report)
Let's check out some time off requests:
time_off = bamboo.get_time_off_requests() print(time_off)
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()
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
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())
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()
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!