Accessing profiles in Zendesk apps

You can use the Profiles API in Zendesk apps to retrieve profiles associated with users. The Profiles API accepts a Zendesk user id in GET requests to identify the user:

GET /api/v2/users/{user_id}/profiles

In the Zendesk Apps framework, you can get a Zendesk user id from the User object. The User object is available in all app locations in Support. The User object can also be a property of other objects in the framework. For example, the ticket.requester property of the Ticket object is a User object.

This article provides a sample function for a Zendesk app that gets the profiles associated with the ticket requester.

Disclaimer: Zendesk provides this article for instructional purposes only. Zendesk does not support or guarantee the code. Zendesk also can't provide support for third-party technologies such as JavaScript. Please post any issue in the comments section below or search for a solution online.

Example

Details

  • App location in Support: Ticket sidebar

  • Zendesk user id: The ticket requester

JavaScript function for app

(function(){
  // create ZAF client  const client = ZAFClient.init();
  // get the Zendesk user id of the ticket's requester  client.get('ticket.requester.id')  .then(function (data) {    user_id = data['ticket.requester.id'];    return user_id;  })
  // make the API request  .then(function (user_id) {    const settings = {      type: 'GET',      url: '/api/v2/users/' + user_id + '/profiles'    };    client.request(settings)    .then(function (data) {      for (let profile of data['profiles']) {        console.log(profile);      }    })  })
  .catch(function () {    console.error('Error');  });
})();

Response

The function displays the profiles in the browser console:

How it works

Because framework requests are asynchronous and some requests must follow others, the function consists of a promise chain.

Preliminaries

The function starts by creating a ZAF client to work with the framework APIs:

const client = ZAFClient.init();

Get the Zendesk user id

Next, the function gets the Zendesk user id of the person requesting support in the ticket, then returns the id to the next function in the chain:

client.get('ticket.requester.id')  .then(function (data) {    user_id = data['ticket.requester.id'];    return user_id;  })

The function uses the client.get(path) framework method to call the framework API. See Working with framework APIs.

Get the profiles

Finally, the function makes a request to the Profiles API and displays the profiles in the browser console. The function uses the client.request() framework method to call the API. See Working with requests.

.then(function (user_id) {    const settings = {      type: 'GET',      url: '/api/v2/users/' + user_id + '/profiles'    };    client.request(settings)    .then(function (data) {      for (let profile of data['profiles']) {        console.log(profile);      }    })  })