Last updatedJul 22, 2019

Cloud Admin APIs Cookbook

Here is a task you can do by combining two of the cloud admin REST APIs.

Update a user's department

To change information about specific users you can use the organizations REST API to get the users and then use the user management REST API to perform more detailed operations on each user. This example shows how to select a user by name and change the user's department value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// This code sample uses the 'request' library:
// https://www.npmjs.com/package/request
var request = require('request');
var users;
var newDepartment = "Marketing";

var orgsOptions = {
   method: 'GET',
   url: 'https://api.atlassian.com/admin/v1/orgs/<Org ID>/users',
   auth: { bearer: '<API key>' },
   headers: {
      'Accept': 'application/json'
   }
};

function getUsers(options, callback){ 
   users = request(options, function (error, response, body) {
      if (error) throw new Error(error);
      callback(null, JSON.parse(body)); 
   });
}

getUsers(orgsOptions, function(err, body) {
  if (err) {
    console.log(err);
  } else {
    for (var user in body.data){
      if(body.data[user].name == "Lila Smalls"){
        var bodyData = `{
          "extended_profile": {
          "department": "` + newDepartment + `"
            }
          }`;

        var usersOptions = {
          method: 'PATCH',
          url: 'https://api.atlassian.com/users/' + body.data[user].account_id + '/manage/profile',
          auth: { bearer: '4oJByAvTCS9o4o2ZVlI5' },
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
            },
          body: bodyData
        };

        request(usersOptions, function (error, response, body) {
          if (error) throw new Error(error);
        });
      }
    }
  }
});