Export Zendesk Tickets and Comments Using the API

Update: github changed the gist url structure, so we’ve updated the url for the gist to go to the right place. Sorry for the confusion.
We love Zendesk as a ticketing service for our support clients, but when we found out you cannot export Zendesk tickets without purchasing a larger plan, we were disappointed. As web developers, we pride ourselves on finding a way if one exists, utilizing the latest tools and methods to build solutions to our client’s problems. This case was not an exception.

Zendesk is the perfect helpdesk

Getting Started

The Good News

There’s hope yet! Zendesk provides a terrific API which is open to anyone with an account. With a little web development magic we can bend this to our will. They have endpoints for getting tickets, users, comments and a variety of other methods we aren’t going to be getting into in great detail this time around. That means we can export Zendesk tickets however we want using these web services!
To get started get the full code on Gist.

The Bad News

Unfortunately, this requires a little bit of development work to get everything to work. Don’t worry! We have you covered. We were able to find a Zendesk-specific API class written by Julien Renouard in PHP to get started. This just wraps the curl requests in the right way and provides some additional helper functionality when it comes to making requests and getting back results.

Working With the Code

The following code is meant to do one fairly niche thing, get all tickets from a particular organization, listing out title, comments and comment authors. This is about a bare-bones as you can get with these types of things. There is very little markup and just one usability enhancement to make it easier to digest the tickets. Ok, let’s get into it.

Setting Up Your Credentials

First, you need to enable access to the REST API through your account. Find this under Settings / Channels / API. They call it Token Access. Enable this and you’ll receive your very own API Key. Guard this with your life or at least change it if you lose it, release it to the public, etc. Jump down in the code until you find the following:

$face = new Zendesk( API_KEY, USERNAME, SUBDOMAIN );
$org_id = ORG_ID;
$test = $face->call('/organizations/'.$org_id.''/tickets', array(), 'GET');

You want to replace your API_KEY with the one you just got from Zendesk. USERNAME is usually the email address you use to log into the site. SUBDOMAIN is the prefix to the url you use to access the site. In our case it’s ‘wdgdc’. Finally, you want to set ORG_ID to the id of the organization you want to export. The easiest way I found to get this id is to go to Zendesk under Settings / People and then click Organizations under the search box. Click on the organization you want to use and the ID will appear in the url after /organizations/.

Run it!

That’s all you need to do! Add this file to a server or to your localhost and run it. You should see a webpage with a list of all the titles of all tickets associated with the organization. To note, there aren’t any of the optimizations we usually put into functionality like this, so it might timeout or hit a memory limit. Be careful and never run this on a production server.

If you want to learn some more about how the rest of this code works, continue on.

Explanation of the Code

Users

One of the first things I noticed was the fact that comments only returned a user ID, so the following calls a list of all users in Zendesk and saves them out in their own array. We’ll use this later to match up the user id to the output we’re looking for.

$users = $face->call('/users', array(), 'GET');
$users = $users->users;
$authors = array();
foreach($users as $user) {
  $authors[$user->id] = $user->name.' ('.$user->email.')';
}

Tickets

Next we need to iterate through the tickets we fetched from the organization earlier in our code (marked here by …). We want to grab the title, timestamp and description of each ticket and output that in a way that can be easily digested. One thing that was irksome, the description of the ticket was not filtered in any way. I opted for the easy route to make this show up correctly, htmlentities takes all the characters html normally uses for markup and encodes them for display on the web. In this way you can have html markup show up in the text rather than have the html markup get read by the browser. From there I wrapped it in pre tags to force the correct white space to show up.

$test = $face->call('/organizations/'.$org_id.''/tickets', array(), 'GET');
...
$tickets = $test->tickets;
print '

Tickets

'; $ids = array(); foreach($tickets as $ticket) { print '

Subject: '.$ticket->subject.'

'; print 'Show Ticket'; print '

Comments

While we’re in the foreach loop for the tickets it’s easiest at this point to make another call to get the comments associated with this ticket. This isn’t the most efficient way to handle this, but as I mentioned before we’re going for the quick solution here. Once we get the comments we iterate through each one, printing out the author (remember when we collected those earlier?) and comment body. In this case the comments actually come with html version of the body copy. It’s strange this would be different from the tickets, but there you go. All we have to do is wrap the pre tags around the body and it’s good to go.

$comments = $face->call('/tickets/'.$ticket->id.'/comments', array(), 'GET');
$comments = $comments->comments;
foreach($comments as $comment) {
  print '

'.$authors[$comment->author_id].'

'; print $comment->html_body; print '
'; }

Get the full code on Gist.

In Closing

This is one of the many cases where an API allowed you to improve upon or create functionality from a service. I highly recommend everyone taking a look at Zendesk’s API. It’s very well put together.

Getting familiar with this type of communication with servers and services is going to be incredibly important in the near future as we’re seeing both Drupal and WordPress start to implement some really incredible API features into their cores that will allow functionality that hasn’t been possible up to this point.

Are you interested in building the web’s next big site, services or app? Get in touch as we are very excited about these new features. We are WordPress Developers and Drupal Developers teamed up with some of the best web designers and user experience experts in the business.

Related Insights

Start a Project

Want to Skip Right
to the Good Stuff?

Contact Us