Build your first LeadApp in 5 min

Welcome to our “Build your first LeadApp in 5 min”.

In this article, we will show you an example on how to build a LeadApp that prints out the total number of users through our REST API.

The sample code is in PHP and Python.

Note: API urls are zone specific and in examples, there is just one of these

  • NOR zone – “https://restapi-nor.leaddesk.com/stable”
  • QED zone – “https://restapi-qed.leaddesk.com/stable/”
  • CEU zone – “https://restapi-ceu.leaddesk.com/stable/”

Requirements and Preparations

If you’re using PHP, here is the environment we used in the sample code:
PHP >= 7.0
guzzle >= 6.0

If you’re using Python, here is the environment we used in the sample code:
Python >= 3.4
requests >= 2.1

Import libraries and define the base url.

 


use GuzzleHttp\Client;

$client = new Client();
$baseUrl = "https://restapi-nor.leaddesk.com/stable";

 

 


import requests

BASE_URL = "https://restapi-nor.leaddesk.com/stable"

 

If you’re using PHP, here is the environment we used in the sample code:
1. PHP >= 7.0
2. guzzle >= 6.0

If you’re using Python, here is the environment we used in the sample code:
1. Python >= 3.4
2. requests >= 2.1

How to get access token

LeadDesk REST API uses OAuth 2 authentication. In this example, we use ‘leaddesk_client_id’ as ‘grant_type’ to get the Bearer Token.

 


$resToken = $client->request(
    "POST",
    $baseUrl. "/oauth/access-token",
    ["json" =>
       [
          "grant_type" => "leaddesk_client_id",
          "client_id" => "", //Found in Admin > General settings > API;
          "client_secret" => "", //Found in Admin > General settings > API;
          "leaddesk_client_id" => 0, //your Leaddesk customer ID;
       ]
    ]
);

$resTokenJson = json_decode($resToken->getBody(), true);
$accessToken = $resTokenJson["access_token"];

 

 


res_token = requests.post(
    "{}{}".format(BASE_URL, "/oauth/access-token"),
    json={
        "grant_type":"leaddesk_client_id",
        "client_id": "",  # <your-client-id>
        "client_secret": "",  # <your-client-secret>
        "leaddesk_client_id": 0 # <your-leadDesk-client-id>
    }

)
res_token.raise_for_status()
access_token = res_token.json()["access_token"]

 

How to request to LeadDesk API

In this step, we will use the method “GET” to the “/users” endpoint to fetch users data. As our API supports pagination, we are fetching the first page of the user list, 5 users per page.

LeadDesk REST API has various endpoints and supports methods GET, POST, PATCH and DELETE. See more from our documentation.

 


$response = $client->request(
    "GET",
    $baseUrl. "/users",
    [
        "http_errors" => false,
        "headers" => [
            "Authorization" => "Bearer ". $accessToken,
        ],
        "query" => [
            "per_page" => 5,
            "page" => 1,
        ]
    ]
);

$result = [
    "code" => $response->getStatusCode(),
    "data" => json_decode($response->getBody(), true)
];

 

 


response = requests.request(
    "GET",
    "{}{}".format(BASE_URL, "/users"),
    headers={
        "Authorization": "Bearer {}".format(access_token)
    },
    params={
        "per_page": 5,
        "page": 1
    }
)

result = {
    "code": response.status_code,
    "data": response.json()
}

 

Output

After fetching the data, we simply print out the total user number of the client. If everything works fine, you should see the output:


"Hello world, we have {x} users in LeadDesk."

 

 


if ($result["code"] >= 200 && $result["code"] <= 299) {
    echo "Hello world, we have {$result["data"]["_meta"]["count"]} users in LeadDesk.\n";
} else {
    echo "Fail to get users data.\n";
    var_dump($result);
}

 

 


if result.get("code") >= 200 and result.get("code") <= 299:
    print("Hello world, we have {} users in LeadDesk.\n".format(result["data"]["_meta"]["count"]))
else:
    print("Fail to get users data.\n")
    print(result)