Search code examples
phpapicurlpipedrive-api

Pipedrive API PUT and POST requests for Filters endpoint not working


I'm trying to pull Deals from the Pipedrive API for a specific date range. I'm accomplishing this by editing (or creating/deleting) a filter via the API with the date range I want, and then passing in that filter in a subsequent request to pull all deals.

The "deals" endpoint works perfectly. My problem is that the API doesn't seem to like the "conditions" parameter that I'm passing in for the "filters" endpoint--but only if I'm using cURL (in my own code) or Postman. If I test either the "edit filter" or "create filter" endpoints in the API docs, both of them work exactly as intended when I copy and paste the JSON object from my code into the "conditions" parameter. However, if I use cURL or Postman, the PUT endpoint simply returns the filter I'm editing without editing it, and the POST endpoint creates a new filter with empty conditions.

Here's the PHP code I'm using for the POST endpoint:

$data = [
    'name' => 'Custom date range',
    'type' => 'deals',
    'conditions' => '{
        "glue": "and",
        "conditions": [
            {
                "glue": "and",
                "conditions": [
                    {
                    "object": "deal",
                    "field_id": "12449",
                    "operator": "=",
                    "value": "won",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": ">=",
                    "value": "2017-03-01",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": "<=",
                    "value": "2017-03-10",
                    "extra_value": "null"
                    }
                ]
            },
            {
                "glue": "or",
                "conditions": []
            }
        ]
     }'
];

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=$apiKey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json;"));
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);

And this is the description of the "conditions" parameter for the "create filter" endpoint:

"Filter conditions as a JSON object. It requires a minimum structure as follows:

{"glue":"and","conditions":[{"glue":"and","conditions": [CONDITION_OBJECTS]},{"glue":"or","conditions":[CONDITION_OBJECTS]}]}

Replace CONDITION_OBJECTS with JSON objects of the following structure:

{"object":"","field_id":"", "operator":"","value":"", "extra_value":""} or leave the array empty. 

Depending on the object type you should use another API endpoint to get field_id. There are five types of objects you can choose from:

"person", "deal", "organization", "product", "activity" 

and you can use these types of operators depending on what type of a field you have:

"IS NOT NULL", "IS NULL", "<=", ">=", "<", ">", "!=", "=", "LIKE '%$%'", "NOT LIKE '%$%'", "LIKE '$%'", "NOT LIKE '$%'", "LIKE '%$'", "NOT LIKE '%$'".

To get a better understanding of how filters work try creating them directly from the Pipedrive application."

The POST endpoint's "conditions" parameter is the same. Again, when I paste that big JSON object into the API docs test, both endpoints works perfectly--but not in my own code. Any help would be appreciated.

Edit: this is the response I get from cURL for the "create filter" endpoint:

{#233 ▼
    +"id": 60
    +"name": "Custom date range"
    +"active_flag": true
    +"type": "deals"
    +"temporary_flag": null
    +"user_id": 504569
    +"add_time": "2017-04-19 11:18:10"
    +"update_time": "2017-04-19 11:18:10"
    +"visible_to": "7"
    +"custom_view_id": null
    +"conditions": {#219 ▼
        +"glue": "and"
        +"conditions": array:2 [▼
            0 => {#230 ▼
                +"glue": "and"
                +"conditions": []
            }
            1 => {#223 ▼
                +"glue": "or"
                +"conditions": []
            }
        ]
    }
}

No errors, but as you can see, the conditions are empty. I also just tried the PHP wrapper that was created for the Pipedrive API and am getting the same result.


Solution

  • Pipedrive engineer here. Here is an example I tested..

    <?php
    $data = '
    {
        "name":"Custom filter less than 1000",
        "type":"deals",
        "visible_to":1,
        "conditions":{
            "glue": "and",
            "conditions":[
                {
                    "glue": "and",
                    "conditions": [
                            {
                                "object": "deal",
                                "field_id": "12452",
                                "operator": "<",
                                "value": 1000,
                                "extra_value": null
                            }
                        ]
                    },
                {
                    "glue": "or",
                    "conditions": []
                }
            ]
        }
    }
    ';
    
    $ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=xxx");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json;', 'Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST,           1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($ch);
    
    echo $response;
    

    Notice that

    1. Entire json is raw-posted
    2. I added Content-Type header
    3. I added visible_to param

    Filtering API is quite complex and we are working on improving documentation. Hope this helps