Skip to main content

Integration between external systems is key for automating processes, and IC Project offers a flexible API for such tasks. This guide shows how to automate adding users to IC Project using a CSV file.

Prerequisites

You will need:

  1. A CSV file containing user data (e.g., email, firstName, lastName).
  1. An API key for your IC Project instance.
  1. Knowledge of Python, including pandas, requests, and json.

Example CSV Structure

Example CSV file (users.csv):

email,firstName,lastName,canLogIn,phoneNumber,jobPosition,department,roleSets,hourlyRate
john.doe@example.com,John,Doe,true,123-456-7890,3fa85f64-5717-4562-b3fc-2c963f66afa6,3fa85f64-5717-4562-b3fc-2c963f66afa6,3fa85f64-5717-4562-b3fc-2c963f66afa6,30
jane.smith@example.com,Jane,Smith,true,987-654-3210,3fa85f64-5717-4562-b3fc-2c963f66afa6,3fa85f64-5717-4562-b3fc-2c963f66afa6,3fa85f64-5717-4562-b3fc-2c963f66afa6,25
alice.johnson@example.com,Alice,Johnson,false,555-123-4567,3fa85f64-5717-4562-b3fc-2c963f66afa6,3fa85f64-5717-4562-b3fc-2c963f66afa6,3fa85f64-5717-4562-b3fc-2c963f66afa6,20
bob.brown@example.com,Bob,Brown,true,555-765-4321,,3fa85f64-5717-4562-b3fc-2c963f66afa6,,40

Python Script Breakdown

  1. Importing Libraries
import pandas as pd
import requests
import json
  • pandas: For reading and processing the CSV file.
  • requests: For making API requests.
  • json: For working with JSON data.
  1. Setting Up Configurations
CSV_FILE_PATH = './users.csv'
ICP_SLUG = 'your_ic_project_slug' # Replace with actual slug
API_ENDPOINT = f'https://app.icproject.com/api/instance/{ICP_SLUG}/user/users'
API_KEY = 'your_api_key' # Replace with actual API key
  • Specifies the CSV file path, IC Project instance slug, API endpoint, and your API key (keep this private).
  1. Transforming CSV Row to JSON
def transform_row(row):
return {
"email": row['email'],
"firstName": row['firstName'],
"lastName": row['lastName'],
"canLogIn": row.get('canLogIn', 'true').lower() == 'true',
"phoneNumber": row.get('phoneNumber', ''),
"jobPosition": row.get('jobPosition', 'default-job-position-id'),
"department": row.get('department', 'default-department-id'),
"roleSets": [row.get('roleSets', 'default-role-set-id')],
"hourlyRate": float(row.get('hourlyRate', 0))
}
  • Converts each row in the CSV to the JSON format required by the IC Project API.
  1. Reading CSV and Sending API Requests
df = pd.read_csv(CSV_FILE_PATH)

for index, row in df.iterrows():
user_data = transform_row(row)
print(f"Preparing data for user: {user_data['email']}")

headers = {
'X-Auth-Token': API_KEY,
'Content-Type': 'application/json',
'Accept': 'application/json',
}

response = requests.post(API_ENDPOINT, headers=headers, data=json.dumps(user_data))

if response.status_code == 201:
print(f"User {user_data['email']} has been successfully added.")
else:
print(f"Error adding user {user_data['email']}: {response.status_code} - {response.text}")

print("Import process completed.")
  • Reads the CSV, transforms each row into JSON, and sends it to the IC Project API.
  • Prints whether the user was successfully added or if there was an error.

Conclusion

This script automates the process of adding users from a CSV file to IC Project using its API. By following these steps, you can streamline user management and ensure consistency across your IC Project instance.

You can find full source code of this example on GitHub

https://github.com/ngroup-pl/icp-api-examples/blob/master/python-import-users-from-csv/import-users-from-csv.py

No Comments yet!

Your Email address will not be published.