> ## Documentation Index
> Fetch the complete documentation index at: https://docs.forest.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication, Filtering & Sorting

## Get authenticated to the Zendesk API

You first need to generate an authentication token to access the Zendesk API. We are going to use the basic authentication mechanism. [More details provided here](https://developer.zendesk.com/rest_api/docs/support/introduction#security-and-authentication). \
\
The 2 parameters required are: a user email (agent) that is allowed to access Zendesk, and the API Key that you can retrieve from the Zendesk console:

<img src="https://mintcdn.com/forest/UDnqA5TqR2qMKTU8/images/legacy/javascript-agents/image%20(494).png?fit=max&auto=format&n=UDnqA5TqR2qMKTU8&q=85&s=d161b45037abd5c204fdc260b0f448fd" alt="" width="1631" height="895" data-path="images/legacy/javascript-agents/image (494).png" />

These 2 parameters can be environment variables like this;

```javascript theme={null}
function getToken() {
  const authEmail = process.env.ZENDESK_AUTH_EMAIL;
  const apiKey = process.env.ZENDESK_API_TOKEN;
  return Buffer.from(`${authEmail}/token:${apiKey}`).toString('base64');
}
```

## Filtering and Sorting using the API

Zendesk API allows you to filter and sort the tickets/users, plus paginate the result.

What we need first, is to implement a way to transform the Forest filtering, sorting and pagination convention to the Zendesk API format.

<Info>
  Learn more about how to [authenticate, filter and sort with the Zendesk API](https://docs.forestadmin.com/woodshop/how-tos/zendesk-integration/authentication-filtering-and-sorting).
</Info>

### Filtering

```javascript theme={null}
function getFilterConditions(params) {
  let filters = [];
  if (params.filters) {
    let filtersJson = JSON.parse(params.filters);
    if (filtersJson.aggregator) {
      filters = filtersJson.conditions;
    } else {
      filters = [filtersJson];
    }
  }

  let filterConditions = [];
  if (params.search) {
    filterConditions.push(params.search);
  }
  for (let filter of filters) {
    if (filter.field === 'id') {
      filterConditions.push(`${filter.value}`);
    } else {
      // This example shows the equals, greater than and lower than conditions
      // cf. Search operators => https://support.zendesk.com/hc/en-us/articles/203663226-Zendesk-Support-search-reference#topic_lhr_wsc_3v
      let operator = ':';
      switch (filter.operator) {
        case 'before':
          operator = '<';
          break;
        case 'after':
          operator = '>';
          break;
      }
      filterConditions.push(`${filter.field}${operator}${filter.value}`);
    }
  }
  return filterConditions;
}
```

### Sorting

```javascript theme={null}
function getSort(params, options) {
  let sort_by = options.default_sort_by || '';
  let sort_order = options.default_sort_order || '';

  let sort = params.sort;
  if (sort) {
    let asc = true;
    if (sort.startsWith('-')) {
      asc = false;
      sort = sort.substring(1);
    }

    const collectionName = options.collection_name;
    const authorized_fields = Liana.Schemas.schemas[collectionName].fields
      .filter((field) => field.isSortable)
      .map((field) => field.field);

    if (authorized_fields.includes(sort)) {
      sort_by = sort;
      sort_order = asc ? 'asc' : 'desc';
    }
  }
  return { sort_by, sort_order };
}
```
