Skip to content

Python – Get organization prices

This script uses Python3 to fetch the price list for every organization and compare the sales price to the product recommended price, and write all products with a changed sales price to 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.

import requests
import base64
import json
import csv
# 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
BASE_URL = 'https://api.cloudmore.com/api'
CSV_FILE = 'discrepancy_report.csv'
# 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)
HEADERS = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {token}' # token fetched from previous steps
}
# Fetch all organizations
def get_organizations():
    url = f"{BASE_URL}/resellers/{RESELLER_ID}/Organizations"
    response = requests.get(url, headers=HEADERS)
    return response.json()
# Fetch organization services
def get_organization_services(org_id):
    url = f"{BASE_URL}/resellers/{RESELLER_ID}/organizations/{org_id}/services"
    response = requests.get(url, headers=HEADERS)
    return response.json()
# Fetch prices per service
def get_prices_per_service(org_id, service_id):
    url = f"{BASE_URL}/resellers/{RESELLER_ID}/organizations/{org_id}/services/{service_id}/pricelist"
    response = requests.get(url, headers=HEADERS)
    return response.json()
def main():
    organizations = get_organizations()
    total_organizations = len(organizations)
    
    # Open the CSV file for writing
    with open(CSV_FILE, mode='w', newline='') as file:
        writer = csv.writer(file)
        # Write the header
        writer.writerow(['Org Name', 'Service', 'ProductName', 'ItemCode', 'RecommendedPrice', 'SalesPrice', 'Margin'])
        
        for index, org in enumerate(organizations, start=1):
            print(f"Processing organization {index}/{total_organizations}: {org['name']}...")
            services = get_organization_services(org["id"])
            for service in services:
                prices = get_prices_per_service(org["id"], service["id"])
                for price in prices:
                    if price["salesPrice"] != price["recommendedPrice"]:
                        # Write to CSV
                        writer.writerow([
                            org['name'],
                            service['name'],
                            price['name'],
                            price['itemCode'],
                            price['recommendedPrice'],
                            price['salesPrice'],
                            price['margin']
                        ])
        print(f"Finished processing organization {index}/{total_organizations}: {org['name']}.\n")
    print("Processing complete.")
if __name__ == '__main__':
    main()