I wrote code to do this, and import from a .CSV file formatted in my structure.
We should definitely have an API endpoint to ADD clients, like you do with contacts. i think this would be the way, and then perhaps you would just need a way to map the fields. Anyway, here is my code, i tested this successfully with contacts, just need to adjust the API endpoint.
import csv
import requests
# File path to your CSV
filepath = '<path.to.CSV>'
# API endpoint
api_endpoint = 'https://yourserver/api/v1/clients/create.php'
# Your API key
api_key = '<YOUR.API.KEY>'
# Function to send data to the API
def send_to_api(contact):
payload = {
"api_key": api_key,
"contact_name": contact['Name'],
"contact_title": "",
"contact_department": "",
"contact_email": contact['Email'],
"contact_phone": contact['Phone'],
"contact_extension": "",
"contact_mobile": "",
"contact_notes": "",
"contact_auth_method": "local",
"contact_important": "1",
"contact_billing": "1",
"contact_technical": "0",
"contact_location_id": "0",
"client_id": "1"
}
# Uncomment the line below to send the request
response = requests.post(api_endpoint, json=payload)
if response.status_code == 200:
print(f"Successfully uploaded contact: {contact['Name']}")
else:
print(f"Failed to upload contact: {contact['Name']}. Response: {response.text}")
# Read the CSV file and process each row
with open(file_path, mode='r', encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
send_to_api(row)