Freshdesk API Samples

Hi All


We've added our Sample APIs here - https://github.com/freshdesk/fresh-samples/tree/master/Ruby_api_samples


Please use this Forums to raise all your queries regarding Freshdesk API and you may also share your Ruby or PHP API samples here


thanks

Vijay


6 people have this question

All,

We added samples in couple of more languages today--PHP and C#:

https://github.com/freshdesk/fresh-samples/

 

Subhash.

My Freshdesk "apps" area does not include freshbooks...I am trying to integrate freshdesk with freshbooks.  Why is this option not available?  Thank you.

Hi Casey,


You have subscribed to the Free Plan which does not support the time tracking Feature in Freshdesk, due to which the Freshbooks integration is not available.

Any chance of getting a PHP sample posted for modifying (not adding!) a user or company with the new custom fields? I've been able to get it to work using command-line curl but when I try it with PHP curl I am getting an error like:


< HTTP/1.1 405 Method Not Allowed

< Allow: GET, PUT, DELETE


(it's probably just my poor php skillz since it doesn't look like it has anything to do with the custom fields..)

Relatedly...why are the curl example all shown with PUT option instead of using POST? Typically, that option (as I understand it) is used for file uploads, not normal data. 


That would seem to imply that we need to be using CURLOPT_PUT , but then it has dependencies to filesize (and it's a string, not a file..)

At the risk of monopolizing the conversation, this set of curl options seems to work for UPDATE


 curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);

 curl_setopt($connection, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($jsondata)));

 curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);

 curl_setopt($connection, CURLOPT_HEADER, false);

 curl_setopt($connection, CURLOPT_USERPWD, $email.":".$password);

 curl_setopt($connection, CURLOPT_POSTFIELDS, $jsondata);

 curl_setopt($connection, CURLOPT_CUSTOMREQUEST, 'PUT');  << the key...

 curl_setopt($connection, CURLOPT_VERBOSE, 1);


$jsondata should be your array data - for example:


 $data = array(

 "user" => array(

 "email"=>"me@abc2.com",

 "name"=>"Me",

 "company"=>"ABC",

 "mobile"=>"555-1212",

 "custom_field" => array(

 "cf_test_field" => "did this work?",

 )

 )

 );


 //encoding to json format

 $jsondata= json_encode($data);


Note the example above includes a custom field. You basically treat these as nested arrays and the json_encode should "just work"


This example also shows that you can change an email address for a contact (which I had wondered..).



Any one can give the list of forums, that people create on freshdesk. for example I create stforum.freshdesk.com similarly other forums list that people created.


And how i can get users from different forums in freshdesk, using (https://domain.freshdesk.com/contacts.json?state=all) this url. I give forum name instead of domain, but {"access_denied":true} this message appears. Anyone can help me on this.


Thanks,

Awais Ahmed

I use woocommerce at my site, is it possible to use API so that a costumer who is logged in on my site can view his/hers tickets under the "my account" page for woocommerce?

Any chance of getting some examples of logging on and create customers / companies with a jquery Ajax structure?

No sample for javascript yet?


I have one in Jquery:


  

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="/web/scripts/jquery.min.new.js"></script>
    <script type="text/javascript" src="/web/scripts/jquery.md5.js"></script>
    <script type="text/javascript">
        var api_key = 'api_key'
        var company_name = 'company ltd.';
        var domains = 'company.com';
        var description = 'company description';
        var user_email = 'user@company.com';
        var user_name = 'firstname lastname';
        var returned_domain = '';
        var update_domain = '';
        var company_data_before = '';
        var company_data = '';
        $(document).ready(function () {
            var company_data_before = {
                "customer": {
                    "name": company_name, "domains": update_domain, "description": description
                }
            }
            var company_data = JSON.stringify(company_data_before);
            $.ajax({
                url: "http://helpdesk.freshdesk.com/customers.json?letter=[" + encodeURIComponent(company_data) + "]",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic " + btoa(api_key));
                },
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                processData: false,
                crossDomain: true,
                success: function (data) {
                    if (data[0]) {
                        //company exists - update company with eventuel additional domains
                        company_data_before = {
                            "customer": {
                                "name": company_name, "domains": domains, "description": description
                            }
                        }
                        company_data = JSON.stringify(company_data_before);
                        returned_domain = data[0].customer.domains
                        returned_domain_array = returned_domain.split(',')
                        found = false;
                        for (i = 0; i < returned_domain_array.length; i++) {
                            if (domains.toLowerCase() != returned_domain_array[i].toLowerCase()) {
                                update_domain = update_domain + returned_domain_array[i].toLowerCase() + ','
                            } else {
                                found = true;
                            }
                        }
                        if (found == false) {
                            update_domain = update_domain + domains
                        }
                        if (update_domain.substr(update_domain.length - 1) == ',') {
                            update_domain = update_domain.substr(0, update_domain.length - 1)
                        }
                        var company_data_before = {
                            "customer": {
                                "name": company_name, "domains": update_domain, "description": description
                            }
                        }
                        var company_data = JSON.stringify(company_data_before);
                        $.ajax({
                            url: "http://helpdesk.freshdesk.com/customers/" + data[0].customer.id + ".json",
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("Authorization", "Basic " + btoa(api_key));
                            },
                            type: 'PUT',
                            dataType: 'json',
                            contentType: 'application/json',
                            processData: false,
                            data: company_data,
                            crossDomain: true,
                            success: function (data) {
                                contact();
                            },
                            error: function (xhr, status) {
                                if ((xhr.status == '200' && xhr.responseText.replace(' ', '') == '') || xhr.status == '422') {
                                    contact()
                                } else {
                                    console.log(xhr.status)
                                }
                            }
                        });
                    } else {
                        //create company
                        company_data_before = {
                            "customer": {
                                "name": company_name, "domains": domains, "description": description
                            }
                        }
                        company_data = JSON.stringify(company_data_before);
                        $.ajax({
                            url: "http://helpdesk.freshdesk.com/customers.json",
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("Authorization", "Basic " + btoa(api_key));
                            },
                            type: 'POST',
                            dataType: 'json',
                            contentType: 'application/json',
                            processData: false,
                            data: company_data,
                            crossDomain: true,
                            success: function (data) {
                                contact();
                            },
                            error: function (xhr) {
                                if ((xhr.status == '200' && xhr.responseText.replace(' ', '') == '') || xhr.status == '422') {
                                    contact()
                                } else {
                                    console.log(xhr)
                                }
                            }
                        });
                    }

                },
                error: function (xhr) {
                    if ((xhr.status == '200' && xhr.responseText.replace(' ', '') == '') || xhr.status == '422') {
                        contact()
                    } else {
                        console.log(xhr)
                    }
                }
            });
        })

        function contact() {
            //create user
            var user_data_before = {
                "user": {
                    "name": user_name,
                    "email": user_email
                }
            }
            var user_data = JSON.stringify(user_data_before)
            $.ajax({
                url: "http://helpdesk.freshdesk.com/contacts.json",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic " + btoa(api_key));
                },
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                processData: false,
                data: user_data,
                crossDomain: true,
                success: function (data) {
                    login();
                },
                error: function (xhr) {
                    if ((xhr.status == '200' && xhr.responseText.replace(' ', '') == '') || xhr.status == '422') {
                        login();
                    } else {
                        console.log(xhr)
                    }
                }
            });
        }
        function login() {
            timestamp = Math.floor((new Date()).getTime() / 1000)
            hash = user_name + user_email + timestamp
            salt = "sso shared key"
            path = "http://helpdesk.freshdesk.com/login/sso?name=" + user_name + "&email=" + user_email + "&timestamp=" + timestamp + "&hash=" + $.md5(hash, salt)
            window.open(path, '_self');
        }
    </script>
</head>
<body>
    &nbsp;
</body>
</html>

  

Anybody who has a working example how to fetch the timetracking from tickets with filtering option to filter on customer/company and a specific date range (previous month) for the timetracking (if any available)? 


?filter=new_and_my_open";

This is interesting to collect all timetracking from customer X and push to invoicing application to automate e.g. customer support monthly to your customer.

I need the time tracking from a certain date range only, because old tickets can be re-opened and could cause double invoicing. 
So the timetracking has to be date range filtered. 

Thanks!

I tried your C# sample, didn't work for me, the error occurred "{"The remote name could not be resolved: 'https'"}" 

at the line:  Stream dataStream = request.GetRequestStream();

Could you please help?


Thanks,

Thank you information.


Gclub

Login or Signup to post a comment