Export tickets to CSV

Here's a code example for API v3 on how to export tickets to CSV.

1. First, make sure you have an API key created in Configuration > System > API. If the key is ready, ensure that the permissions are enabled for the tasks you are going to perform. In this example, we are working with tickets, so the permissions for tickets should be enabled for reading at least.

2. To make the example more interesting, let's export only a group of tickets, e.g., tickets with a specific tag. To do this, add a filter to the export script. For more information on this, see the advanced API v3 filters. Since the tag filter does not work with tag names, you need to find the tag ID, e.g., using the API v3 documentation playground. The filter for tickets that have a specific tag (but may also have other tags) would look like this:


3. To export all tickets (/tickets endpoint), iterate through all the found tickets and load all messages for each ticket (/tickets/{ticketId}/messages endpoint). However, there are different types of messages, such as internal notes, header info, title info, and user agent info. By default, we don't need these; therefore, filter them out by focusing only on type 'M', which represents the actual message.

4. For the CSV header, use these values:
   Ticket ID, Ticket code, Ticket owner, Tags, Subject, Messages

The final export script would look like this (PHP code):


const LIVEAGENT_API_URL = 'https://your-account.liveagent.com/api/v3/';

// YOUR API KEY YOU CAN FIND IN MENU Configuration> System> API
const API_KEY = 'v3y24r9ha6tc59d4qc0bg1yvvuotd4mz';

// this is a helper function to make API call requests
function sendRequest($apiCommand, $params = array(), $filters = '') {
    if (!empty($filters)) {
        $filters = '&_filters='.urlencode($filters);

    $headers = array('apikey: '.API_KEY);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, LIVEAGENT_API_URL . $apiCommand . '?' . http_build_query($params).$filters);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    $curl_response = curl_exec($ch);

    if ($curl_response === false) {
        $info = curl_error($ch);
        die('Error occurred during cURL execution. Additional info: ' . var_export($info));
    return json_decode($curl_response);

// open a CSV file for writing
$fp = fopen('tickets-export.csv', 'w');

// set the CSV header
fputcsv($fp, array('Ticket ID','Ticket Code','Ticket owner','Tags','Subject','Messages'));

$page = 1;
$records = 20;

// get the list of all conversations with a specific tag
$tag = '1a2b';

$filters = '[["tags","L","'.$tag.'"]]';

// get first 20 tickets
do {
    $tickets = sendRequest('tickets', array('_page'=>$page, '_perPage'=>$records, '_sortDir'=>'ASC'), $filters);
    $innerPage = 1;
    foreach ($tickets as $ticket) {
        if (!isset($ticket->id)) {
            die("There was an error: $ticket");

        $ticketTags = implode(',',$ticket->tags);
        $messagesStr = '';

        // get all messages of the ticket
        do {
            $allMessages = sendRequest('tickets/' . $ticket->id . '/messages', array('_page'=>$innerPage,'_perPage'=>$records,'_sortDir'=>'ASC'));
            foreach ($allMessages as $messages) {
                foreach ($messages->messages as $message) {
                    if ($message->type == 'M') {
                        $messagesStr .= $message->userid . ' (' . $message->datecreated . '): ' . $message->message . "\n";
                fputcsv($fp, array(
        } while (!empty($allMessages));
} while (!empty($tickets));


After the script is processed, you will find your tickets exported in tickets-export.csv file.