API stating invalid fields, not sure what I'm doing wrong

I’m using Pipedrive as a CRM on top of a CRM we already have inside our business. We are an ISP that already has a crm that manages existing customers but does not do a very good job for the sales side of the house. The existing system does have webhooks that post JSON strings wherever I’d like. I have them going to a “middle man” script that will translate and push between them.

For whatever reason, I created custom fields in the deals section but my API call still fails. It gives an error of:
“Error posting data to Pipedrive: Invalid field(s) in the payload: mobile, address, city, state, postal_code, note”

Here’s my code:

$data = json_decode($jsonString, true);
    
    $event = $data['event'];
$customerID = $data['data']['customerID'];
$customerType = $data['data']['customerType'];
$firstName = $data['data']['firstName'];
$lastName = $data['data']['lastName'];
$emailAddress = $data['data']['emailAddress'];




$phoneNumberHome = $data['data']['numbers']['Home']['Number'];

$phoneNumberMobile = $data['data']['numbers'][0]['Number'];

$pipedriveData = array(
    'name' => $firstName . ' ' . $lastName,
    'email' => array('value' => $data['data']['emailAddress'], 'label' => 'work'),
    'phone' => array('value' => $phoneNumberHome, 'label' => 'home'),
    'mobile' => array('value' => $phoneNumberMobile, 'label' => 'mobile'),
    'address' => $data['data']['physicalStreet'],
    'city' => $data['data']['physicalCity'],
    'state' => $data['data']['physicalState'],
    'postal_code' => $data['data']['physicalZip'],
    'note' => 'Customer ID: ' . $data['data']['CustomerID']
);
$ch = curl_init($api_url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($pipedriveData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

// Execute the request
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Decode and print the response from Pipedrive
$responseData = json_decode($response, true);

Here’s a screenshot of Pipedrive for my custom fields:

Hi @xgrewellx,
Welcome to the community :wave:

In order to access custom fields via Pipedrive API, you need to use the field API key of the custom field, not its name. It looks like this a71a17fd1fde006955c8c8f73576eb9161705c00

In order to get the API key, you can copy it from UI in the Data fields section of the Company Overview settings page, clicking on and selecting Copy API key

You will see the same API key in the response when you query the data via the API.

1 Like

Thank you Tomas, I appreciate it.

I changed the code but am getting a new error.

Code: `$url = ‘https://’.$company_domain.‘.pipedrive.com/api/v1/persons?api_token=’ . $api_token;

if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
// Get the JSON data from the request body
$jsonString = file_get_contents(‘php://input’);

// Decode the JSON data
$data = json_decode($jsonString, true);

$event = $data['event'];

$customerID = $data[‘data’][‘customerID’];
$customerType = $data[‘data’][‘customerType’];
$firstName = $data[‘data’][‘firstName’];
$lastName = $data[‘data’][‘lastName’];
$emailAddress = $data[‘data’][‘emailAddress’];

$phoneNumberHome = $data[‘data’][‘numbers’][‘Home’][‘Number’];

$phoneNumberMobile = $data[‘data’][‘numbers’][0][‘Number’];

$pipedriveData = array(
‘name’ => array(‘value’ => $firstName . ’ ’ . $lastName),
‘email’ => array(‘value’ => $data[‘data’][‘emailAddress’], ‘label’ => ‘work’),
‘phone’ => array(‘value’ => $phoneNumberHome, ‘label’ => ‘home’),
‘f81e4666b06db2159a36d74a9xxx5c847’ => $phoneNumberMobile,
‘a640473877fc2c3ba26bb5xxxx81ea47166’ => $data[‘data’][‘physicalStreet’],
‘07120eb4257873663563xxxxd46d01b10’ => $data[‘data’][‘physicalCity’],
‘fda4c0c6dd5dbe9e84d084xxxx9dda6’ => $data[‘data’][‘physicalState’],
‘c03fe2b5dcb7d1e27879c1xxxxxcf9714d0’ => $data[‘data’][‘physicalZip’],
‘55834b106fcb4a1ac42e6xxxxea535e2b’ => $data[‘data’][‘CustomerID’]
);

$ch = curl_init($url);
`

The result I’m getting based on this code is: “Error posting data to Pipedrive: Internal Server Error occurred. Pipedrive staff was notified about this.”

Any insight on what I’m doing wrong here?

I have edited my API key’s in the above code.

Hi @xgrewellx ,
I can see the cause of the error, the name has to be a string, you have provided an array. So the code should be as follows:

$pipedriveData = array(
    'name' => $firstName . ' ' . $lastName,
    'email' => array('value' => $data['data']['emailAddress'], 'label' => 'work'),
    'phone' => array('value' => $phoneNumberHome, 'label' => 'home'),
    'f81e4666b06db2159a36d74a9xxx5c847' => $phoneNumberMobile,
    'a640473877fc2c3ba26bb5xxxx81ea47166' => $data['data']['physicalStreet'],
    '07120eb4257873663563xxxxd46d01b10' => $data['data']['physicalCity'],
    'fda4c0c6dd5dbe9e84d084xxxx9dda6' => $data['data']['physicalState'],
    'c03fe2b5dcb7d1e27879c1xxxxxcf9714d0' => $data['data']['physicalZip'],
    '55834b106fcb4a1ac42e6xxxxea535e2b' => $data['data']['CustomerID']
);

The API should definitely return a better error, I will ping the code owners to improve it.

2 Likes