Getting all Deals

Follow these steps to get all Deals using our API.

Only want to see the finished code for getting titles of all Deals? No problem, you can find it below in PHP and in node.js! You can also try out GET /deals endpoint in our API Reference to get all information regarding all Deals.



Step 1: GET your API token and company domain


Follow our tutorials on how to find the API token and how to get the company domain.

Then create a file getAllDeals.php and give value to the $company_domain and $api_token variables:

<?php
// Content of getAllDeals.php
 
// Pipedrive API token
$api_token = 'Your API token goes here';
 
// Pipedrive company domain
$company_domain = 'Your company domain goes here';


Step 2: Define target URL


To make a request, you'll need the correct URL for getting the Deals listed, an example would look like this https://companydomain.pipedrive.com/api/v1/deals?api_token=659c9fddb16335e48cc67114694b52074e812e03.

You need to create a $url variable that holds the correct URL for listing the Deals:

// URL for Deal listing
$url = 'https://'.$company_domain.'.pipedrive.com/api/v1/deals?limit=500&api_token=' . $api_token;

PS! We've set the limit of items shown per page to 500 (the maximum). The default limit is 100.



Step 3: Make a GET request


This part of the code is more complex and you don’t need to understand it right away, all you need to know is that it contains everything to make a GET request with your data against our API.

Simply copy and paste this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
echo 'Sending request...' . PHP_EOL;
 
$output = curl_exec($ch);
curl_close($ch);


Step 4: Check the result


$output variable holds the full response you get back from the server. As all responses from us are in JSON format, the first thing you'll want to do is to convert it into a proper PHP array:

// Create an array from the data that is sent back from the API
// As the original content from server is in JSON format, you need to convert it to a PHP array
$result = json_decode($output, true);

Then you can check if data returned in the result is not empty:

if (empty($result['data'])) {
    exit('No deals created yet' . PHP_EOL);
}

And then you can loop over and echo the titles of the Deals:

// Iterate over all found Deals
foreach ($result['data'] as $key => $deal) {
    $deal_title = $deal['title'];
       // Print out a deal title with its ID
       echo '#' . ($key + 1) . ' ' .  $deal['title'] . ' ' . '(Deal ID:'. $deal['id'] . ')' . PHP_EOL;  
}


Full working example


Copy the full working example into getAllDeals.php:

<?php
// Content of getAllDeals.php
  
// Pipedrive API token
$api_token = 'Your API token goes here';
 
// Pipedrive company domain
$company_domain = 'Your company domain goes here';
  
//URL for Deal listing with your $company_domain and $api_token variables
$url = 'https://'.$company_domain.'.pipedrive.com/api/v1/deals?limit=500&api_token=' . $api_token;
  
//GET request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
echo 'Sending request...' . PHP_EOL;
  
$output = curl_exec($ch);
curl_close($ch);
  
// Create an array from the data that is sent back from the API
// As the original content from server is in JSON format, you need to convert it to a PHP array
$result = json_decode($output, true);
 
// Check if data returned in the result is not empty
if (empty($result['data'])) {
    exit('No Deals created yet' . PHP_EOL);
}
 
// Iterate over all found Deals
foreach ($result['data'] as $key => $deal) {
    $deal_title = $deal['title'];
       // Print out a deal title with its ID
       echo '#' . ($key + 1) . ' ' .  $deal['title'] . ' ' . '(Deal ID:'. $deal['id'] . ')' . PHP_EOL;  
}
const pipedrive = require('pipedrive');
const defaultClient = pipedrive.ApiClient.instance;

// Configure authorization by settings api key
// PIPEDRIVE_API_KEY is an environment variable that holds real api key
defaultClient.authentications.api_key.apiKey = process.env.PIPEDRIVE_API_KEY;

async function getDeals() {
    try {
        console.log('Sending request...');

        const api = new pipedrive.DealsApi();
        const response = await api.getDeals();

        console.log('Got deals successfully!', response);
    } catch (err) {
        const errorToLog = err.context?.body || err;

        console.log('Getting deals failed', errorToLog);
    }
}

getDeals();


To execute the code


Use php getAllDeals.php command to execute the code in the command line.

Here's an example output with 21 Deals:

$ php getAllDeals.php
Sending request...
#1 Batman deal (Deal ID: 1)
#2 Big apple sale (Deal ID: 2)
#3 Robin sale (Deal ID: 3)
#4 Silly goose (Deal ID: 4)
#5 Mademoiselle sale (Deal ID: 5)
#6 Cats and dogs (Deal ID: 6)
#7 Panna on vaja (Deal ID: 7)
#8 Lucky (Deal ID: 8)
#9 Call him now (Deal ID: 9)
#10 Sending (Deal ID: 10)
#11 Wolt (Deal ID: 11)
#12 PD (Deal ID: 12)
#13 The Explorer (Deal ID: 13)
#14 The Carryall (Deal ID: 14)
#15 The Pineapple (Deal ID: 15)
#16 The Base (Deal ID: 16)
#17 Kisser (Deal ID: 17)
#18 Hugger (Deal ID: 18)
#19 The Scholar (Deal ID: 19)
#20 The Artist (Deal ID: 20)
#21 The Ace (Deal ID: 21)

If you don't have any Deals, this is the output you'll get:

$ php getAllDeals.php
Sending request...
No Deals created yet


Did this page help you?