How to add labels to Jira via Rest API

Devi Foskett February 1, 2019

I would like to be able to add labels to a Jira using rest apis. I am using javascript currently to create an issue. I do not know the json required to create the label and need help figuring that out. I use only javascript and json for this thank you! When using postman there is a 415 error when I add "Labels":["Test"] to create a label.

 

Javascript:

var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Content-Type","application/json");
labels = ' {"fields": { "project": { "key": "'+ projectKey +'" }, "labels":["sd_reviewed", "ta_reviewed", "Reviewed", "Functional"]}} ';
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
};

xhr.send(labels);

 

4 answers

2 votes
Alin_Gabriel_Draghici May 1, 2020

Reposting cause my edit dissapeared (?)

Endpoint: https://YourJiraInstance.com/rest/api/2/issue/issue-id-right-here

Body: {"update":{"labels":[{"add":"this-is-the-label-you're-adding"}]}}

POST method, basic AUTH

Cristina Donaldson January 28, 2022

Thanks, this was very helpful!

Rusty Czerwinski January 2, 2023

Works with this payload, using PUT method as Warren mentions below..  Thanks!

Like Richard Cross likes this
0 votes
Tomer Ben Arye March 13, 2024

PUT method.  

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-put

/rest/api/3/issue/{issueIdOrKey}

{
"update": {
"labels": [
{
"add": "bump1"
},
{
"add": "no_sf_case_id"
}
]
}
}

 

0 votes
Andrew Januszak July 8, 2022

Per REST API Doc (https://docs.atlassian.com/software/jira/docs/api/REST/8.22.4/#issue-editIssue) the syntax is:

"labels":[{"add":"triaged"},{"remove":"blocker"}]} 

In your case, I believe it would be: 

"labels":[{"add":"sd_reviewed", "ta_reviewed", "Reviewed", "Functional"}]
Tomer Ben Arye March 13, 2024

nope...
This one works:


{ "update": { "labels": [ { "add": "lable1" }, { "add": "label2" } ] } }

0 votes
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 1, 2019

Hi @Devi Foskett

Within the JSON, this is the format of adding a label

 "labels": [
"Testing"
]

Hope this helps you

Devi Foskett February 1, 2019

In postman this will return a 415 error and not work.

Devi Foskett February 1, 2019

@Warren I have tried the code mentioned above with no avail. If you would like to see code to better understand what I am trying to accomplish I can add it above.

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 1, 2019

Are you trying to

  1. create an issue and set the label at the same time? If so, you're missing the issuetype. You also need to pass in values for EVERY mandatory field
  2. add a label to an existing issue?

 

What error do you get using your javascript?

Below is what my JSON looks like to create an issue and set it's label

{
"fields": {
"project": {
"key": "DS"
},
"summary": "Post Refresh work",
"issuetype": {
"name": "Task"
},
"labels": [
"Label1",
"Label2"
]
}
}
Devi Foskett February 1, 2019

@Warren I am trying to add labels to an existing project. With the code you provided I get a status error 400.

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 1, 2019

If you look at this you'll see you need to do a PUT if you're updating an existing issue. Is that what you've been using?

Devi Foskett February 1, 2019

@Warren No I have not I will try that!

Devi Foskett February 1, 2019

I am getting returned a status error 405 now.

Like Fianna O'Brien likes this

Suggest an answer

Log in or Sign up to answer