Hey there, fellow developer! Ready to dive into the world of UKG Pro API integration? You're in for a treat. This guide will walk you through the process of building a robust integration using Python. UKG Pro's API is a powerful tool that allows you to tap into a wealth of workforce management data. Whether you're looking to streamline your HR processes or build some cool automation, you're in the right place.
Before we jump in, let's make sure you've got everything you need:
requests
, json
Got all that? Great! Let's get coding.
First things first: we need to get you authenticated. UKG Pro uses OAuth 2.0, so we'll need to obtain an access token.
import requests import json def get_access_token(client_id, client_secret): url = "https://api.ultipro.com/oauth/token" payload = { "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret } response = requests.post(url, data=payload) return response.json()["access_token"] access_token = get_access_token("your_client_id", "your_client_secret")
Pro tip: Don't forget to handle token expiration and refresh. You don't want your integration to break mid-operation!
Now that we're authenticated, let's start making some requests. Here's a basic structure:
base_url = "https://api.ultipro.com" headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } def make_request(endpoint, method="GET", data=None): url = f"{base_url}/{endpoint}" response = requests.request(method, url, headers=headers, json=data) response.raise_for_status() return response.json()
This function will handle GET, POST, PUT, and DELETE requests. Neat, right?
UKG Pro returns data in JSON format. Python's json
library makes parsing a breeze:
employee_data = make_request("personnel/v1/employees") for employee in employee_data: print(f"Employee: {employee['firstName']} {employee['lastName']}")
Don't forget to handle those pesky errors:
try: data = make_request("some/endpoint") except requests.HTTPError as e: print(f"Oops! API request failed: {e}")
Let's look at some common operations you might want to perform:
# Get employee data employee = make_request(f"personnel/v1/employees/{employee_id}") # Update employee information updated_data = {"firstName": "John", "lastName": "Doe"} make_request(f"personnel/v1/employees/{employee_id}", method="PUT", data=updated_data) # Fetch payroll data payroll_data = make_request("payroll/v1/payroll-details") # Manage time off requests time_off_request = { "employeeId": "12345", "startDate": "2023-07-01", "endDate": "2023-07-05", "timeOffTypeId": "VAC" } make_request("time-management/v1/time-off-requests", method="POST", data=time_off_request)
Remember these golden rules:
Let's put it all together with a simple integration that fetches all employees and their current positions:
def get_employees_with_positions(): employees = make_request("personnel/v1/employees") for employee in employees: employee_id = employee["employeeId"] position = make_request(f"personnel/v1/employees/{employee_id}/job") print(f"{employee['firstName']} {employee['lastName']}: {position['jobTitle']}") get_employees_with_positions()
Always test your integration thoroughly. Here's a simple unit test to get you started:
import unittest from unittest.mock import patch class TestUKGIntegration(unittest.TestCase): @patch('requests.request') def test_make_request(self, mock_request): mock_request.return_value.json.return_value = {"test": "data"} result = make_request("test/endpoint") self.assertEqual(result, {"test": "data"}) if __name__ == '__main__': unittest.main()
And there you have it! You're now equipped to build a robust UKG Pro API integration in Python. Remember, the key to a great integration is clean code, proper error handling, and thorough testing.
Keep exploring the UKG Pro API documentation for more endpoints and features. The possibilities are endless! Happy coding, and may your integrations always run smoothly!