In this guide, we will demonstrate how to export orders from Apilo and create tasks in IC Project using Python. This integration allows for the automated transfer of order data from Apilo to IC Project, simplifying task management. We’ll utilize the requests
library to interact with the APIs of both systems.
Prerequisites
- Python: Make sure Python is installed on your system.
- Requests Library: Install the
requests
library if it’s not already installed. Use pip to install it:
pip install requests
- API Credentials: You will need the following API credentials:
- Apilo Instance Slug: The unique identifier for your Apilo instance.
- Apilo Access Token: An authorization token for accessing the Apilo API – To get this token generate new auth key on Apilo and go on page to make access token from auth key
- IC Project Instance Slug: The unique identifier for your IC Project instance.
- IC Project API Key: An authorization key for accessing the IC Project API.
Python Code for Integration
The script starts by defining configuration variables for the Apilo and IC Project APIs. These variables include the necessary instance slugs, access tokens, and board links, which are used throughout the script to authenticate and interact with the APIs.
# Configuration for Apilo API
APILO_INSTANCE_SLUG = "YOUR_APILO_INSTANCE_SLUG"
APILO_ACCESS_TOKEN = "YOUR_APILO_ACCESS_TOKEN"
# Configuration for IC Project API
IC_PROJECT_INSTANCE_SLUG = "YOUR_IC_PROJECT_INSTANCE_SLUG"
IC_PROJECT_API_KEY = "YOUR_IC_PROJECT_API_KEY"
IC_PROJECT_BOARD_LINK = "YOUR_IC_PROJECT_BOARD_LINK_WHERE_WE_WILL_ADD_TASKS"
The get_orders_from_apilo
function is designed to fetch orders from the Apilo API. It constructs a request URL using the Apilo instance slug and sends a GET request with the appropriate headers, including the authorization token. If the request is successful, it returns the JSON response containing the orders. If an error occurs during the request, it catches the exception, prints an error message, and returns an empty dictionary.
def get_orders_from_apilo():
url = f"https://{APILO_INSTANCE_SLUG}.apilo.com/rest/api/orders"
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f"Bearer {APILO_ACCESS_TOKEN}",
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
return response.json()
except requests.RequestException as e:
print(f"Error retrieving orders: {e}")
return {}
You can also add filters like this
url = f"https://{APILO_INSTANCE_SLUG}.apilo.com/rest/api/orders&createdAfter=2022-03-01T14%3A40%3A33%2B0200
More you will find here
The get_board_slug
function extracts the board slug from a given IC Project board URL. It removes any trailing slashes and splits the URL by ‘/’, returning the last segment of the URL which represents the board slug.
def get_board_slug(url):
parts = url.rstrip('/').split('/')
return parts[-1]
The get_board_column_id
function retrieves the column ID for a specific board within IC Project. It first constructs a URL to get the board’s details using the board slug and sends a GET request with the API key. Upon successfully obtaining the board ID, it then constructs another URL to get the board columns and retrieves the ID of the first column. It handles potential errors by catching exceptions and printing error messages.
def get_board_column_id(board_slug):
url = f"https://app.icproject.com/api/instance/{IC_PROJECT_INSTANCE_SLUG}/project/boards/s/{board_slug}/get-kanban-board"
headers = {
'X-Auth-Token': IC_PROJECT_API_KEY,
'Content-Type': 'application/json',
'Accept': 'application/json',
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
board_id = response.json().get('id')
if not board_id:
raise ValueError("Board ID not found in the response")
except requests.RequestException as e:
print(f"Error retrieving board ID: {e}")
return None
except ValueError as e:
print(e)
return None
url = f"https://app.icproject.com/api/instance/{IC_PROJECT_INSTANCE_SLUG}/project/boards/{board_id}/board-columns"
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
return response.json()[0].get('id') # Assuming you want the first column's ID
except requests.RequestException as e:
print(f"Error retrieving board columns: {e}")
return None
The create_task_in_ic_project
function creates a new task in IC Project based on the order information retrieved from Apilo. It starts by fetching task templates from IC Project (though this data is not directly used in the task creation). It then gets the board column ID where the task will be placed. If the column ID is successfully retrieved, it constructs a task with the order details and posts it to the IC Project API. The function handles any errors that may occur during the API requests and prints relevant error messages. If you want to do a more complex task, edit the code, APIs of both platforms are available to make your task easier.
def create_task_in_ic_project(order):
url = f"https://app.icproject.com/api/instance/{IC_PROJECT_INSTANCE_SLUG}/project/task-templates"
headers = {
'X-Auth-Token': IC_PROJECT_API_KEY,
'Content-Type': 'application/json',
'Accept': 'application/json',
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
except requests.RequestException as e:
print(f"Error retrieving task templates: {e}")
return
board_slug = get_board_slug(IC_PROJECT_BOARD_LINK)
column_id = get_board_column_id(board_slug)
if not column_id:
print(f"Error: Unable to find column for board_slug {board_slug}")
return
url = f"https://app.icproject.com/api/instance/{IC_PROJECT_INSTANCE_SLUG}/project/tasks"
headers = {
'X-Auth-Token': IC_PROJECT_API_KEY,
'Accept': 'application/json',
}
task_data = {
"identifier": str(uuid.uuid4()),
"boardColumn": column_id,
"name": f"Order ID: {order['idExternal']}",
"description": f"Order from customer: {order['addressCustomer']['name']}",
"dateStart": f"{order['createdAt']}",
"dateEnd": "DATE END",
"priority": "normal"
}
try:
response = requests.post(url, json=task_data, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
if response.status_code == 201:
print(f"Task for order {order['idExternal']} created successfully.")
else:
print(f"Error creating task for order {order['idExternal']}: {response.status_code}")
print(response.json())
except requests.RequestException as e:
print(f"Error creating task: {e}")
Finally, the integrate_apilo_with_ic_project
function orchestrates the integration process by fetching orders from Apilo and then creating tasks in IC Project for each order.
def integrate_apilo_with_ic_project():
orders = get_orders_from_apilo()
for order in orders.get('orders', []):
create_task_in_ic_project(order)
The script execution is initiated by calling this function, which performs the full integration.
# Run the integration
integrate_apilo_with_ic_project()
Conclusion
This Python script automates the process of transferring orders from Apilo to IC Project and creating tasks. Customize the script with your API credentials and URLs to fit your specific setup. This integration helps streamline task management by ensuring your IC Project system is updated with the latest orders from Apilo.
You can find full source code of this example on GitHub
No Comments yet!