3

Here is the form that does not work:

<form action="https://api.pipedrive.com/v1/persons" method="post">   

    <!-- These hidden fields have valid values, just not shown here -->
    <input type="hidden" name="owner_id" id="owner_id" value="my-id" /> 
    <input type="hidden" name="org_id" id="org_id" value="my-company-id" /> 
    <input type="hidden" name="api_token" id="api_token" value="my-api-key" /> 
    <input type="hidden" name="stage_id" id="stage_id" value="35" /> 

    <b>Name</b><br/>
    <input type="text" name="name" id="name" id="name"/>
    <br/><br/>

    <b>E-mail</b><br/>
    <input type="text" name="email" id="email" id="email"/> 
    <br/><br/>

    <b>Arrival</b><br/>
    <input type="text" name="2fdf1284127d702e42595ce20bd8ffdf60763105" id="2fdf1284127d702e42595ce20bd8ffdf60763105"/>
    <br/><br/>

    <b>Departure</b><br/>
    <input type="text" name="2492a5afed2a9cb7948d6a22135fd4dd80de200c" id="2492a5afed2a9cb7948d6a22135fd4dd80de200c"/>
    <br/><br/>

    <b>Message</b><br/>
    <textarea style="width:300px; hight:70px;" id="7433280b87ffc7c1e3fd615eb35526273bcea6cf" name="7433280b87ffc7c1e3fd615eb35526273bcea6cf"></textarea>     
     <br/><br/> 
    <input type="submit" value="Remitir"/> 
</form>

Upon submission, the following response is returned:

{"success":false,"error":"Organization not found.","data":null,"additional_data":null}

What is missing for a successful addition of adding a new person via REST?

Here is the API: https://developers.pipedrive.com/v1

Many thanks for a solution.

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
iamtoc
  • 1,569
  • 4
  • 17
  • 24

2 Answers2

7

You can do something like that

<script language="javascript" type="text/javascript">
    function pDriveIntegration()
    {
       var url = "https://api.pipedrive.com/v1";
       var rsrc_deals = "/deals";
       var api_token = "YOUR_API_TOKEN";
       var deal
Json = '{' + '"title":"' + name + '","org_id":"?"' +
                    ',"value":"0"' + ',"currency":"GBP"' + ',"stage_id":"?"' + 
',"visible_to":"?"' + ',"status":"open"' + '"}';


        var xhrDeal = new XMLHttpRequest();
        xhrDeal.onreadystatechange = function()
        {
            if (xhrDeal.readyState == 4 && xhrDeal.status == 201) {
                var obj = JSON.parse(xhrDeal.responseText);
                var deal_id = obj.data.id;
            }
        }
        xhrDeal.open("POST", url + rsrc_deals + "?" + "api_token" + "=" + api_token, false);
        xhrDeal.setRequestHeader("Content-type", "application/json");
        xhrDeal.send(dealJson);

</script>
</head>
<body>

<form ...........>


<input type="submit" onclick="pDriveIntegration();"


</form>

Therefore you need AJAX. Even if it were possible to do that only with a HTML form, you have to encode a JSON to send with the post. To be honest, I tried but I always got some errors in any browser.

I noticed that is no secure at all, because somebody is able to see your source code and get your API_TOKEN through the browser. I mean someone could get all your info (I moved to other solutions, not using only HTML & AJAX).

But anyway it is a good beginning to understand everything that you need.

I'd also suggest to think about the "persons". For example, before than create a new deal I find the person. If he/she exist I won't create it, I get its id and I am gonna put on the Deal.

But, in the other case if the person doesn't exist I create a person before than all. After that, I'll use its id on the Deal.

I hope it will be useful.

Byee

ackuser
  • 5,681
  • 5
  • 40
  • 48
  • This is totally not secure due to that the user is able to see the api key. Best would be to use the answer below and post to there with ajax. Then the user will not be able to see the api key – Timberman May 01 '20 at 12:29
4

Using php, would be like this:


// ================================== //
// API token
// ================================== //
$api_token = 'xxxxxxxxxxxxxxxxxxxxxx';

// ================================== //
// FORM INPUT CAPTURE
// ================================== //
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$mail = $_POST['inputmail'];

// ================================== //
// PERSON'S API Key fields' Array values
// ================================== //
$persons = array(
    'name' => $name,
    'email' => $mail,
    'phone' => $phone
);

// ================================== //
// API PERSONS domain
// ================================== //
$url1 = 'https://personalpipedriveadress.pipedrive.com/v1/persons?api_token=' . $api_token;

// ================================== //
// CURL access
// ================================== //
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $persons);

echo 'Sending USER request...' . '<br>';

$output1 = curl_exec($ch1);
curl_close($ch1);

// ================================== //
// API array data convert to JSON format
// ================================== //
$result1 = json_decode($output1, true);

// ================================== //
// Check if an ID came back and print it
// ================================== //
if (!empty($result1['data']['id'])) {
    echo 'Person was added successfully!' . '<br>';
}
else {
    echo 'ERROR adding person';
}


Bitch Bros
  • 37
  • 5
  • api_token must be get from within your account settings; // API domain must be used with the username registerfed at Pipedrive. – Bitch Bros Dec 26 '19 at 23:18