Skip to content

PYTHON – Create organizations

This script uses Python3 to create new organizations in Cloudmore from a CSV file.

Edit the variables under “Define constants” and run the script with Python3.

This is just an example with limited error handling and should not be used for production.

You can easily change the input data from a CSV file to an external system API to create a full automation of creating organizations.

Please update the list of properties you want to submit. The mandatory ones are:

  • Name
  • registrationNumber
  • contact
  • notificationEmail
  • address
  • primaryDomain
import requests
import json
import csv
import base64
# Define constants
RESELLER_ID = '11111111-2222-3333-4444-555555555555' # Replace with you Broker ID
CLIENT_ID = 'ro.customer.client'
API_SECRET = 'MySecret' # Replace with your API Secret
USERNAME = 'username@broker.com' # Replace with your username
PASSWORD = 'MyPassword' # Replace with your password
CSV_FILE_PATH = 'organizations.csv'  # Replace with your CSV file path
# Base64 encode the API secret
client_decoded = f'{CLIENT_ID}:{API_SECRET}'
b64_secret = base64.b64encode(client_decoded.encode()).decode()
# Get Access Token using username and password
token_url = 'https://api.cloudmore.com/connect/token'
token_headers = {'Authorization': f'Basic {b64_secret}'}
token_data = {
    'grant_type': 'password',
    'username': USERNAME,
    'password': PASSWORD,
    'scope': 'api'
}
token_response = requests.post(token_url, headers=token_headers, data=token_data)
token = token_response.json().get("access_token")
print('Access Token received:', token)
# Read organizations data from CSV and create organizations
org_creation_url = f'https://api.cloudmore.com/api/resellers/{RESELLER_ID}/Organizations'
org_headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {token}'
}
with open(CSV_FILE_PATH, 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        organization_data = {
            "name": row['orgName'],
            "registrationNumber": row['registrationNumber'],
            "vat": row['vat'],
            "homePage": row['homePage'],
            "contact": {
                "name": row['contact_name'],
                "email": row['contact_email'],
                "phone": row['contact_phone'],
                "cellPhone": row['contact_cellPhone']
            },
            "customerNumber": row['customerNumber'],
            "notificationEmail": row['notificationEmail'],
            "address": {
                "street": row['address_street'],
                "postalCode": row['address_postalCode'],
                "city": row['address_city'],
                "countryCode": row['address_countryCode']
            },
            "approvalTriggeredOn": int(row['approvalTriggeredOn']),
            "approvalOnQuantityChange": bool(row['approvalOnQuantityChange']),
            "primaryDomain": row['primaryDomain'],
            "customProperties": [
                {
                    "id": row['property_id'],           # Make sure the custom property exists
                    "value": row['property_value']
                }
                # Include other properties as needed...
            ]
        }
        
        response = requests.post(org_creation_url, headers=org_headers, json=organization_data)
        if response.status_code == 201:
            print(f"Organization '{row['orgName']}' created successfully.")
        else:
            print(f"Failed to create organization '{row['orgName']}'.")
            print("Response content:", response.content)
print('Organization creation process completed.')

Example CSV file:

orgName,registrationNumber,vat,homePage,contact_name,contact_email,contact_phone,contact_cellPhone,customerNumber,notificationEmail,address_street,address_postalCode,address_city,address_countryCode,approvalTriggeredOn,approvalOnQuantityChange,primaryDomain,property_id,property_value
Org Name 1,123456,VAT123,https://www.example.com,John Doe,john@example.com,123-456-7890,987-654-3210,Cust123,notify@example.com,123 Main St,12345,Cityville,US,0,TRUE,customerDomain1.org,026a22c2-4639-4e96-a200-24ff368038bb,value1
Org Name 2,789012,VAT789,https://www.example.org,Jane Smith,jane@example.org,987-654-3210,123-456-7890,Cust456,notify2@example.org,456 Elm St,67890,Townsville,GB,1,FALSE,customerDomain2.org,026a22c2-4639-4e96-a200-24ff368038bb,value2