Tutorial: Creating, Reading, Updating, and Deleting Issues and Projects using Jira Cloud REST API with Node.js

Posted by


In this tutorial, we will be learning how to interact with the JIRA Cloud REST API using Node.js. We will cover how to create, read, update, and delete issues and projects within JIRA Cloud. By the end of this tutorial, you will have a basic understanding of how to use the JIRA Cloud REST API in your Node.js applications.

Prerequisites:

  1. Node.js installed on your machine
  2. Basic understanding of JavaScript
  3. JIRA Cloud account

Step 1: Set up a new Node.js project
First, let’s create a new directory for our project and initialize it with npm.

mkdir jira-api-tutorial
cd jira-api-tutorial
npm init -y

Step 2: Install required dependencies
Next, let’s install the necessary dependencies for our project.

npm install axios dotenv

We will be using the axios library to make HTTP requests to the JIRA Cloud API and the dotenv library to manage environment variables.

Step 3: Create a .env file
Create a .env file in the root of your project and add your JIRA Cloud credentials.

JIRA_EMAIL=your_email@example.com
JIRA_API_TOKEN=your_api_token
JIRA_BASE_URL=your_jira_base_url

Step 4: Create a JIRA API client
Create a new file called jiraClient.js and add the following code to it.

const axios = require('axios');
require('dotenv').config();

const email = process.env.JIRA_EMAIL;
const apiToken = process.env.JIRA_API_TOKEN;
const baseUrl = process.env.JIRA_BASE_URL;

const auth = {
  username: email,
  password: apiToken,
};

const client = axios.create({
  baseURL: baseUrl,
  auth: auth,
});

module.exports = client;

This code sets up an Axios client with your JIRA Cloud credentials and exports it for use in other files.

Step 5: Create a JIRA service
Create a new file called jiraService.js and add the following code to it.

const client = require('./jiraClient');

async function getIssue(issueKey) {
  try {
    const response = await client.get(`/rest/api/3/issue/${issueKey}`);
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.errorMessages[0]);
  }
}

async function createIssue(projectKey, issueType, summary, description) {
  const data = {
    fields: {
      project: {
        key: projectKey,
      },
      issuetype: {
        name: issueType,
      },
      summary: summary,
      description: description,
    },
  };

  try {
    const response = await client.post('/rest/api/3/issue', data);
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.errorMessages[0]);
  }
}

async function updateIssue(issueKey, updates) {
  try {
    const response = await client.put(`/rest/api/3/issue/${issueKey}`, updates);
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.errorMessages[0]);
  }
}

async function deleteIssue(issueKey) {
  try {
    const response = await client.delete(`/rest/api/3/issue/${issueKey}`);
    return response.data;
  } catch (error) {
    throw new Error(error.response.data.errorMessages[0]);
  }
}

module.exports = {
  getIssue,
  createIssue,
  updateIssue,
  deleteIssue,
};

This code defines functions to interact with JIRA Cloud’s issue endpoints, including getting an issue, creating an issue, updating an issue, and deleting an issue.

Step 6: Use the JIRA service in your main file
Create a new file called index.js and add the following code to it to test out the JIRA service.

const jiraService = require('./jiraService');

async function main() {
  // Get an issue
  console.log(await jiraService.getIssue('ISSUE-KEY'));

  // Create an issue
  console.log(await jiraService.createIssue('PROJECT-KEY', 'Bug', 'New bug', 'A new bug has been found.'));

  // Update an issue
  console.log(await jiraService.updateIssue('ISSUE-KEY', { fields: { summary: 'Updated summary' } }));

  // Delete an issue
  console.log(await jiraService.deleteIssue('ISSUE-KEY'));
}

main();

Replace ‘ISSUE-KEY’ and ‘PROJECT-KEY’ with actual issue and project keys in your JIRA Cloud account.

Step 7: Run your Node.js application
To run your Node.js application, execute the following command in your terminal.

node index.js

You should see the output of the JIRA API calls printed to your console.

Congratulations! You have successfully interacted with the JIRA Cloud REST API using Node.js. You can now incorporate this knowledge into your own projects to automate tasks and interact with JIRA Cloud programmatically.

0 0 votes
Article Rating

Leave a Reply

22 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@josearias9882
2 hours ago

It worked! However, I do not why I had to hard code the Key as "TEST22" instead of using the Project key variable, const issueKey = await createIssue("TEST22", issueType, summary, description); If I use the variable the KEY is undefined. But that's ok, I used this repo to study REST API + Jira for an oncoming interview. Thanks.

@ArunaMahajan-t2j
2 hours ago

hi, Very nice video. thanks for the information. now i want something like to show a popup in jira when an issue is moved from one sprint to another.

@arunamahajan2977
2 hours ago

Grt help. thank you so much. Now I want to show a popup in jira when an issue is moved from one sprint to another

@SHRUTIAGRAWAL-pk1ch
2 hours ago

Your help is very much needed. Thank you!

@irishrex995
2 hours ago

Good and Easy explanation, for experienced person its perfect.

@randomanimegalaxy6859
2 hours ago

Hey great video
1) Can you please share the official jira documentation where i can start my projects is in node.js

2) can we get all the projects of an user ?

3) and count of active sprints like how may sprints delayed or done or inprogress ?

4) user list who are the participants of an particualr project with there done, pending, delayed issue count ?

@juansebastianossa3717
2 hours ago

You are awesome <3

@adarshogirala4657
2 hours ago

Is it possible to integrate test case execution using selenium java. If yes guide me to dig more into it.
Thank you.

@SaloniMittal-hr1wd
2 hours ago

How to I know that my Atlassian account is a developer account? On listing the users by getUsers() function call, I don't get my account details. How to get my account details there?

@kkgolf5371
2 hours ago

Go much too quickly for beginners. I don't use GIT or Code much, you are thinking everyone comes here ready to code and already knows Git, VS etc.

@gokulakrishna-q9u
2 hours ago

Hey @Horea Porutiu

I need to make the jira dashboard components in confluence page so that we have used forge app.Now am currently working on reteriving the dashboard details on api's getting error while reteriving the api when we deployed on development server and we try to check it.

is there any other options is there our focus to create an app which contain the jira dashboard details in it

@xst-k6
2 hours ago

How do you create an EPIC using Jira REST API using Python?

@blecomp
2 hours ago

Hello friend, great video. Where is the complete course? thanks

@gabistoianova3170
2 hours ago

Hello, i want to ask (maybe) a stupid question, but i cannot figure it out on my own, because i'm beginner with jira cloud Rest APIs. I have an existing project with 2 folder – one for the react.js frontend and the other for backend, which contains the server.js, node_modules, package.json and package-lock.json made with express and MySQL. So my question is where i have to clone the repo to? I have to clone it in the backend folder, where my server.js is, or in a third separate folder to not get errors, because the git repo contains files that are the same like the package.json file ? Thanks in advance!

@ShubhamKumarChoudhry
2 hours ago

I get this when I follow your tutorial – error:

"unable to get local issuer certificate"
After running the command "node app.js"

@surendrababu9962
2 hours ago

same code i need in php how can I do that?

@caohoainam6413
2 hours ago

ありがとございます<3

@kalyankumardas3999
2 hours ago

Great one and thanks for the uploading the video. What are the extensions you have used in visual studio app to create api?

I will refer this video because this will helpful for me.

I am new on node js side.

@icarojose6316
2 hours ago

is it possible to fetch the current running sprint of a project ?

@JonHaakonAriansen
2 hours ago

I get this when I follow your tutorial – error:

{}

undefined

22
0
Would love your thoughts, please comment.x
()
x