Node.js Jira - Issues: Create Issue

Back to Index

Creates an issue or a sub-task from a JSON representation.

Documentation: https://developers.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-post

CURL Command

curl -X POST --user jira@example.com:JIRA_API_TOKEN \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '
    {
      "fields": {
        "project": {
          "id": "10000"
        },
        "summary": "something is wrong",
        "issuetype": {
          "id": "10000"
        },
        "assignee": {
          "name": "matt"
        },
        "priority": {
          "id": "3"
        },
        "labels": [
          "bugfix",
          "blitz_test"
        ],
        "description": "description",
        "fixVersions": [
          {
            "id": "10001"
          }
        ],
        "customfield_10005": "blah blah"
      }
    }' \
  --url 'https://your-domain.atlassian.net/rest/api/2/issue'

Node.js Example

var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node6_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node6_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node6_linux32');
    } else {
        var chilkat = require('chilkat_node6_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node6_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    //  URL: https://your-domain.atlassian.net/rest/api/2/issue
    var bTls = true;
    var port = 443;
    var bAutoReconnect = true;
    success = rest.Connect("your-domain.atlassian.net",port,bTls,bAutoReconnect);
    if (success !== true) {
        console.log("ConnectFailReason: " + rest.ConnectFailReason);
        console.log(rest.LastErrorText);
        return;
    }

    rest.SetAuthBasic("jira@example.com","JIRA_API_TOKEN");

    var json = new chilkat.JsonObject();
    json.UpdateString("fields.project.id","10000");
    json.UpdateString("fields.summary","something is wrong");
    json.UpdateString("fields.issuetype.id","10000");
    json.UpdateString("fields.assignee.name","matt");
    json.UpdateString("fields.priority.id","3");
    json.UpdateString("fields.labels[0]","bugfix");
    json.UpdateString("fields.labels[1]","blitz_test");
    json.UpdateString("fields.description","description");
    json.UpdateString("fields.fixVersions[0].id","10001");
    json.UpdateString("fields.customfield_10005","blah blah");

    rest.AddHeader("Content-Type","application/json");
    rest.AddHeader("Accept","application/json");

    var sbRequestBody = new chilkat.StringBuilder();
    json.EmitSb(sbRequestBody);
    var sbResponseBody = new chilkat.StringBuilder();
    success = rest.FullRequestSb("POST","/rest/api/2/issue",sbRequestBody,sbResponseBody);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    var respStatusCode = rest.ResponseStatusCode;
    if (respStatusCode >= 400) {
        console.log("Response Status Code = " + respStatusCode);
        console.log("Response Header:");
        console.log(rest.ResponseHeader);
        console.log("Response Body:");
        console.log(sbResponseBody.GetAsString());
        return;
    }

    var jsonResponse = new chilkat.JsonObject();
    jsonResponse.LoadSb(sbResponseBody);

    var id;
    var key;
    var self;

    id = jsonResponse.StringOf("id");
    key = jsonResponse.StringOf("key");
    self = jsonResponse.StringOf("self");

}

chilkatExample();

Sample JSON Response Body

{
  "id": "10023",
  "key": "SCRUM-24",
  "self": "https://chilkat.atlassian.net/rest/api/2/issue/10023"
}