Use our APIs with Python

Learn how to effectively use our APIs in Python and leverage the power of our workforce solutions in your applications. This guide provides step-by-step instructions on installing the necessary libraries, making API requests, handling response data, and error handling.

Python is a popular programming language known for its simplicity and readability, making it an excellent choice for integrating with our APIs. This page provides a guide on how to use our APIs effectively in Python.

Installing the Required Libraries

To get started, you'll need to install the necessary libraries to interact with our APIs. We recommend using the requests library, which simplifies the process of sending HTTP requests. You can install it using pip, the package installer for Python, with the following command:

pip install requests

Making API Requests

Once you have the requests library installed, you can start making requests to our APIs. Here's an example of a basic GET request using the Skills API to retrieve a list of skills:

import requests

# Define the API endpoint
url = 'https://example.com/di/v1/skills'

# Add any required headers or parameters
params = {
    'limit': 50,
    'cursor_key': ''
}

# Send the GET request
response = requests.get(url, params=params)

# Check the response status code
if response.status_code == 200:
    # Success! The request was successful
    data = response.json()
    # Process the data as needed
    skills = data['skills']
    count = data['count']
    # ...
else:
    # Oops! Something went wrong
    print(f'Request failed with status code: {response.status_code}')

In this example, we're sending a GET request to the Skills API endpoint /di/v1/skills. We include the limit parameter to specify the maximum number of skills to return, and the cursor_key parameter to paginate through the results. If the request is successful (status code 200), we can access the response data and process it accordingly.

Handling Response Data

The response from our APIs is typically in JSON format, which is easy to work with in Python. You can use the json() method provided by the requests library to parse the response into a Python dictionary. From there, you can access the data and extract the information you need. Here's an example:

# Assuming the response contains a JSON object
data = response.json()

# Accessing specific data elements
skills = data['skills']
count = data['count']

# Process the data as needed
for skill in skills:
    print(skill['name'])
    # ...

Error Handling

It's important to handle errors that may occur during API requests. Besides checking the response status code, you can also handle specific HTTP errors using the response.raise_for_status() method. This method raises an exception if the response status code indicates an error. Here's an example:

# Send the GET request
response = requests.get(url, params=params)

# Check for HTTP errors
response.raise_for_status()

# Process the response data if no errors occurred
data = response.json()

By using response.raise_for_status(), you can ensure that any errors are caught and handled appropriately in your code.