Skip to main content

This tutorial demonstrates how to automate the import of contractors from a CSV file into IC Project using Python and their API.

Step 1: Prerequisites

  • Python installed on your machine.
  • A CSV file with contractor data (e.g., name, email, phone number).
  • Your IC Project API Key and unique ICP Slug.

Step 2: Example CSV Format

Below is an example structure for the CSV file:

name,email,phone
John Doe,john.doe@example.com
Jane Smith,jane.smith@example.com,+48223456789

Step 3: Breaking Down the Python Code

1. Importing Required Libraries

import csv
import requests

This imports the csv module for reading the CSV file and requests for making HTTP requests to the IC Project API.

2. Define API and File Details

CSV_FILE_PATH = 'contractors.csv'  # Path to your CSV file
ICP_SLUG = 'your-icp-slug'  # Your IC Project slug
API_ENDPOINT = f'https://app.icproject.com/api/instance/{ICP_SLUG}/crm/contractors'
API_KEY = 'your-api-key'  # Your API key

Here, you specify the path to the CSV file, your IC Project slug, and API key for authentication.

3. Set Up Headers

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

These headers include the authorization token, content type for the request, and expected response format (JSON).

4. Read CSV and Send Data

def main(csv_file):
    with open(csv_file, newline='', encoding='utf-8') as file:
        reader = csv.DictReader(file)
        for row in reader:
            contractor_data = {
                'name': row['name'],  # Required field: contractor's name
                # Optional fields below, uncomment if needed:
                # 'email': row['email'],  # Optional: contractor's email
                # 'phoneNumber': row['phone'],  # Optional: contractor's phone number
                # "fullName": "string",  # Optional: full name
                # "vatId": "string",  # Optional: VAT ID
                # "paymentDays": 0,  # Optional: payment terms in days
                # "description": "string",  # Optional: description
                # "tags": [ "497f6eca-6276-4993-bfeb-53cbbbba6f08" ],  # Optional: tag IDs
                # "industryBranches": [ "497f6eca-6276-4993-bfeb-53cbbbba6f08" ],  # Optional: industry branch IDs
                # "contactInfo": [
                #     { "type": "string", "value": "string", "isDefault": True }  # Optional: contact information
                # ],
            }
            # Sending POST request to the API
            response = requests.post(API_ENDPOINT, json=contractor_data, headers=headers)
            if response.status_code == 201:
                print(f"Contractor {row['name']} added successfully.")
            else:
                print(f"Error adding contractor {row['name']}: {response.status_code}, {response.text}")

This function reads the CSV file and creates a contractor data dictionary for each row. The script then sends a POST request to the API to add the contractor.

5. Run the Script

if __name__ == "__main__":
    main(CSV_FILE_PATH)

This runs the function when the script is executed, passing the CSV file path as an argument.

Step 4: Running the Script

To execute the script:

  • Replace the placeholders (your-icp-slug, your-api-key) with your actual IC Project credentials.
  • Ensure your CSV file is formatted as described in Step 2.
  • Run the script using python your_script.py to upload your contractors.

Optional Fields

The script includes several optional fields (such as email, phone number, VAT ID, and tags). You can uncomment these fields in the contractor_data dictionary if you need to include them during the import process.

This method simplifies bulk uploads of contractors to IC Project, saving time and reducing manual data entry.

You can find full source code of this example on GitHub

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

No Comments yet!

Your Email address will not be published.