Update Contact Unable to update addresses

I have a script that updates contacts information in InfusionSoft via the REST API. Everything works perfectly except for the addresses.

I’ll include how I update email and addresses since email works but address does not even though they should work in the same way.

elif value == 'email':
            fields_to_update['email_addresses'] = contact['email_addresses']
            for email in fields_to_update['email_addresses']:
                if email['field'] == "EMAIL1":
                    email['email'] = updated_values['email']
elif value == 'billing_address_line_1':
            fields_to_update['addresses'] = contact['addresses']
            for address in fields_to_update['addresses']:
                if address['field'] == "BILLING":
                    address['line1'] = updated_values['billing_address_line_1']

With these two functions I create an “updated_fields” object and send it like this.

new_contact = update_infusionsoft_primary_sandbox_contact_data(current_contact, updated_fields)
                patch_parameters = {"access_token": current_company_access_token}
                request_url = "https://api.infusionsoft.com/crm/rest/v1/contacts/" + str(current_contact['id'])
                headers = {'content-type': 'application/json'}
                contact_update = requests.patch(request_url, json=new_contact, params=patch_parameters, headers=headers)

For some reason email works. It grabs all of the emails as they are and changes just the field that has changed. The addresses should work the same way but for some reason when address is included I get a 400 response and it doesn’t update any of the fields.

Here is what I am currently passing in that is failing.

{'email_addresses': [{'email': 'apu9@kwikimart.com', 'field': 'EMAIL1'}], 'addresses': [{'line1': '106 Street Lane', 'line2': 'Apt 1', 'locality': 'Denver', 'region': 'Co', 'field': 'BILLING', 'postal_code': '12345', 'zip_code': '12345', 'zip_four': '', 'country_code': 'USA'}, {'line1': '400 Clearbrooke Terrace', 'line2': '', 'locality': 'Cottage Grove', 'region': 'Wi', 'field': 'SHIPPING', 'postal_code': '53527', 'zip_code': '53527', 'zip_four': '', 'country_code': 'USA'}]}

@Nick_Wortley Let me take a look. Is there any error message that you see with 400 Bad Request?

Looking at the data, my guess is that you are getting:

{
    "message": "BILLING Region is invalid, SHIPPING Region is invalid"
}

Try changing the region to Wisconsin instead of Wi and Colorado instead of Co.

That worked! Do you know how to do the same with “company_name”? Since it’s an “optional_field” I am unable to update it currently.

For updating company details, such as name you will have to use the company endpoint:

https://developer.infusionsoft.com/docs/rest/#!/Company/updateCompanyUsingPATCH

But if you are trying to update the company a contact belongs to, not the company details itself, then try sending in the company id inside the company object, with whatever contact details you have:

  "company": {
    "id": 0
  }

In this case you would have to know the new company id that you want this contact to be associated with.

Thank you for your answer. There are, however, two different company fields on a contact model.
Company

I am referring to the top one, which would be the name of company that the contact owns. This is shows up in “optional_properties” like this.
Company_Name

It looks like it is separate from any Company Model. Do you know how to access optional_fields like this one?