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:
- Correct API Endpoint: I’ve confirmed that I’m using the correct endpoint for updating an organization’s details (
PUT /organizations/{id}
). - Valid Parameters: The field names in the
data
object seem to match the API documentation. The full address is updating, but theaddress_country
is not. - 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 theaddress
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
andaddress_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