Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add labels to Jira via Rest API

Devi Foskett February 1, 2019 edited

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
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 28, 2022

Thanks, this was very helpful!

Rusty Czerwinski
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 2, 2023

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

Like Richard Cross likes this
1 vote
Andrew Januszak
Contributor
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" } ] } }

Like Daniel Eads likes this
0 votes
Tomer Ben Arye March 13, 2024 edited

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"
}
]
}
}

 

Fadi Hawli
Contributor
August 7, 2024

As of now I thought the only way you can create labels is by passing the issue key and then adding a label against the issue key. If you do not use an issue the api will not work. Has anyone solved this by being able to create labels without reference to an issue key?

Tomer Ben Arye August 8, 2024

But I gave you the option to enter KEY or ID...

 

Roman Wolf
Contributor
March 11, 2025 edited

Hi @Fadi Hawli ,

as per my understanding the label concept in JIRA is different from what you expect.

 

You expect labels to be an independent entity in JIRA, like components or versions.

In fact labels just exist as an attribute of an issue. When you remove a label from all the issues the label is gone and cannot be found anywhere.

That explains why it is mandatory to provide an issue key.

 

Hope this helps, Roman

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 edited

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
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events