0

Q: How should one update the customFieldValues in SynchroTeam using a Python POST request?

https://api.synchroteam.com/#custom-field-values

I spent the past day working on this getting a Error 500 response, and no clue to tell me what I was doing wrong. With a bit of help from the SynchroTeam support, I was able to figure this out.

Steve L
  • 1,523
  • 3
  • 17
  • 24

1 Answers1

0

Here's the code I used to accomplish this task:

import base64
import requests

# in case you don't know the ID of your custom field(s)...
def get_customer_customfields():
    url = f"https://ws.synchroteam.com/Api/v3/customfield/list"
    payload = {"type": "customer"}
    headers = get_headers()
    try:
        response = requests.get(url=url, params=payload, headers=headers)
        return response.json()
    except Exception as e:
        print(f"Error {e}")

# how to get the headers you'll need for your POST request...
def get_headers() -> dict:
    domain = "YOUR SYNCHROTEAM DOMAIN"
    api_key = "YOUR SYNCHROTEAM API_KEY"
    auth_key = str(base64.b64encode(bytes(f"{domain}:{api_key}", 'utf-8')))
    auth_key = auth_key[2:][:-1]  # remove 1st 2 chars and last char before using it
    return {
        'accept': 'text/json',
        'authorization': f"Basic {auth_key}",
        'cache-control': 'no-cache',
        'content-type': 'application/json'
    }

# how to update the customFieldValue(s)...
def update_customer():
        url = "https://ws.synchroteam.com/Api/v3/customer/send"
        # in this example, I'm updating customer 3530636 with a 
        # new value for `Our Customer ID`
        updates = {
            'id': 3530636, 
            'customFieldValues': [
            {
                'id': 221313,  # find this id by calling get_customer_customfields()
                'label': 'Our Customer ID', 
                'value': 'CID-00016813'
            }
            ]
        }
        headers = get_headers()  
        try:
            response = requests.post(url=url, json=updates, headers=headers)
            if (response.status_code==200):
                return response.json()  # all is well
            else:
                raise RuntimeError(response.text)
        except Exception as e:
            print(f"Error {e}")
Steve L
  • 1,523
  • 3
  • 17
  • 24