API Troubleshooting; Pipedrive Organization Address and Country Update Issue

Hi everyone! I wanted to share an interesting challenge I recently encountered while working on a project using the Pipedrive API. The goal was simple: update the full address and the country for multiple organizations via the API, using data from a CSV file. While the address field updates without any issues, the address_country field remains stubbornly empty after the API call. I’m hoping the community can help shed some light on the problem.

The Problem

The function works great for updating the full address of an organization in Pipedrive. I can confirm that the full address field gets updated correctly. However, the country field (address_country) is not being populated, despite passing it in the API request body.

The issue is specific to the address_country field not being updated, even though it’s passed as part of the payload in the same request where the address is successfully updated.

Things I’ve Checked So Far:

  1. Correct API Endpoint: I’ve confirmed that I’m using the correct endpoint for updating an organization’s details (PUT /organizations/{id}).
  2. Valid Parameters: The field names in the data object seem to match the API documentation. The full address is updating, but the address_country is not.
  3. Request Structure: I’ve experimented with the order of fields in the payload, as well as trying to update just the country field, but the issue persists.

Possible Hypotheses:

  • Could there be a conflict between the address_country field and how the address field is handled in Pipedrive?
  • Is it possible that address_country only gets automatically populated if the full address is parsed by Pipedrive’s geolocation or address validation systems? This would be very unuseful as I would need to touch each organization manually I guess
  • Should I be formatting the request payload differently to ensure the country field is populated correctly?

Request for Ideas:

  • Has anyone else faced a similar issue where the address_country field remains unpopulated after an API update, even when passed explicitly?
  • Should I be using a different field or approach to handle this, or could it be a bug in how the API processes address_country?
  • Any other ideas on how to ensure that both the address and address_country fields are updated together successfully?

I did not find a similar issue I’m reaching out to the Pipedrive dev community to get some fresh ideas on how to resolve this issue. Any insights or suggestions would be highly appreciated! Thanks in advance for your help.

Best regards,
Adam

The file with the information is formated like this:
1099;Park Innovaare AG;Parkstrasse 1, 5234 Villigen, Switzerland;Switzerland

Here’s the code I’ve been working with in python; it does not generate any errors:

# -*- coding: utf-8 -*-

import os
import requests
import csv

# Retrieve the API token from an environment variable
API_TOKEN = os.getenv('PIPEDRIVE_API_TOKEN')
BASE_URL = 'https://api.pipedrive.com/v1'

if not API_TOKEN:
    raise ValueError("API token not found. Set the PIPEDRIVE_API_TOKEN environment variable.")

# Function to update the organization's address and country
def update_organization_address_and_country(org_id, full_address, country):
    url = f'{BASE_URL}/organizations/{org_id}'
    params = {
        'api_token': API_TOKEN
    }
    # Update the full address (without the country) and country separately
    data = {
        'address_country': country,  # Only the country
        'address': full_address    # Full address without the country
    }

    response = requests.put(url, params=params, json=data)

    if response.status_code == 200:
        print(f'Successfully updated organization {org_id} with address: {full_address} and country: {country}')
    else:
        print(f'Failed to update organization {org_id}: {response.status_code} - {response.text}')

# Function to remove the country from the address (assuming the last part after the final comma is the country)
def remove_country_from_address(address):
    if ',' in address:
        # Split the address by the last comma and return the address without the country
        address_parts = address.rsplit(',', 1)
        return address_parts[0].strip()  # Return the address part without the country
    return address  # If no comma is found, return the address as-is

# Function to read the CSV file with organization data, addresses, and countries
def read_organizations_file(filename):
    organizations = []
    with open(filename, "r", encoding="utf-8") as file:
        reader = csv.reader(file, delimiter=';')
        for row in reader:
            if len(row) >= 4:
                organizations.append({
                    'id': row[0],         # Organization ID
                    'address': row[2],    # Full address (from the 3rd column)
                    'country': row[3]     # Country (from the 4th column)
                })
    return organizations

if __name__ == '__main__':
    # Input file containing organization IDs, names, addresses, and countries
    input_file = 'organizations_with_separate_country.csv'
    
    # Read organizations from the file
    organizations = read_organizations_file(input_file)

    # Quick stop for testing (limit to the first 10 organizations)
    organizations = organizations[:10]  # Remove this line after testing

    # Loop through organizations and update the full address (without country) and country in Pipedrive
    for org in organizations:
        org_id = org['id']
        full_address = remove_country_from_address(org['address'])  # Remove country from the address
        country = org['country']
        
        # Update the organization with the full address (without the country) and the country separately
        update_organization_address_and_country(org_id, full_address, country)

Edit: For proper code markup

Your hypotheses are essentially correct. If the address is added by the API, it seems only older Pipedrive accounts automatically Google enrich the address and populate the fields.

When the integration does work, Pipedrive v1 often sends 2 webhooks when events are triggered. If address fields are Google enriched, they’re only present in the second webhook. So, just toss the first one out.

After chatting with Pipedrive, they say older Pipedrive accounts automatically Google enrich the address data, but new Pipedrive accounts must be individually enabled - otherwise, the fields are left empty. This is kind of a bummer if your Marketplace App relies on parsed address data…

Perhaps things are different with v2, but I’m reluctant to change until it’s in official release.

In the mean time, I’ve been using Geoapify to enrich address when the second webhook turns up empty.

Thank you for your reply.
I also opened a ticket with the Pipedrive help; it turned out that there was a bug or it had to be activated.
They fixed it, I reran my code and now I have all the countries updated properly. :slight_smile:

I am just starting. I am curious in what limitations I will run with the next steps. :wink: