How to get projects from Jira using JavaScript

Jira, a robust project management tool, offers a powerful API that lets developers extract a variety of important information. 

The projects that are stored and managed in Jira are no exception.

By pulling project data from Jira—either for your product or the other applications you use internally—you can more effectively perform tasks like monitoring and reporting. You can also prevent employees (or users, if you're integrating Jira with your product) from having to hop between multiple applications to find and/or enter in data.

To help you get projects from Jira, we’ll guide you through the process of setting up authentication for the Jira API and then break down how you can execute a GET request to retrieve projects. Lastly, we'll walk through a single API endpoint that can connect to dozens of third-party ticketing systems. 

Related: How you can fetch attachments from Jira via JavaScript

Authenticating with Jira

To access data from the Jira API, you need to authenticate your requests. You can do this by including an authorization header in your API request. 

The format for this header is `Authorization: Basic {Email Address}:{API-KEY}`. 

You'll need to base64 encode your email address and API key, and combine them in the format `email:API-KEY`. Then, prepend the result with the word "Basic" and a space to form the full authorization header value.

Here's the full code snippet to authenticate the request:


```javascript
const fetch = require('node-fetch');
const email = "YOUR EMAIL";
const apiToken = "YOUR API TOKEN";
const domain = "YOUR DOMAIN";
let offset = 0;
const base64Credentials = Buffer.from(`${email}:${apiToken}`).toString('base64');
const headers = {
  'Authorization': `Basic ${base64Credentials}`,
  'Accept': 'application/json'
};
```

Remember to replace <code class='blog_inline_code'>"YOUR EMAIL"</code>, <code class='blog_inline_code'>"YOUR API TOKEN"</code> and <code class='blog_inline_code'>"YOUR DOMAIN"</code> with your actual credentials and domain.

Related: A guide to fetching comments from JavaScript

Pulling projects from Jira

We’ll hit the API endpoint:<code class='blog_inline-code'>https://{DOMAIN}.atlassian.net/rest/api/3/project/search</code> with the query param <code class='blog_inline-code'>expand</code> set to <code class='blog_inline-code'>description</code>. 

We’ll also use offset-based pagination to limit the number of items requested at a time. The <code class='blog_inline-code'>maxResults</code> query param will be set to <code class='blog_inline-code'>50</code> and the offset will be included in the request as the path parameter <code class='blog_inline-code'>startAt</code>. 


```javascript
async function getProjects(offset) {
  const response = await fetch(`https://${domain}.atlassian.net/rest/api/3/project/search?expand=description&maxResults=50&startAt=${offset}`, { headers });
  const data = await response.json();
  if (data.values.length === 0) {
    console.log('No more projects.');
  } else {
    console.log(data.values);
    offset += 50;
    getProjects(offset);
  }
}
getProjects(offset);
```

This function will recursively fetch all projects in batches of 50 until there are no more projects to fetch. The projects will be logged to the console. 

Here’s an example of an individual item returned by this API endpoint:

{
    "expand": "field",
    "self": "http://www.example.com/jira/rest/api/2/project/PRJ",
    "id": "10000",
    "key": "PRJ",
    "description": "This is a sample project",
    "name": "Sample Project",
    "avatarUrls": {
        "48x48": "http://www.example.com/jira/secure/projectavatar?pid=10000&avatarId=10011",
        "24x24": "http://www.example.com/jira/secure/projectavatar?size=small&pid=10000&avatarId=10011",
        "16x16": "http://www.example.com/jira/secure/projectavatar?size=xsmall&pid=10000&avatarId=10011",
        "32x32": "http://www.example.com/jira/secure/projectavatar?size=medium&pid=10000&avatarId=10011"
    },
    "projectTypeKey": "business",
    "simplified": false,
    "style": "classic",
    "isPrivate": false,
    "properties": {}
}

Related: What you need to do to retrieve projects from Jira via Python

How to test your Jira integration

Even if everything seems perfect during your internal testing, there are numerous reasons why it might encounter issues once deployed in production. These could include unforeseen user interactions, compatibility issues, or environmental differences.

Here are some proactive measures you can take to mitigate these risks:

  • Understand the API specifications: It's crucial to fully understand the API you're working with. Dive into the documentation to get a grip on the request and response structures, endpoints, and expected behaviors. This will help you anticipate potential issues.

  • Create a test plan: Developing a comprehensive test plan is vital in ensuring that you're prepared for various real-world situations. This should include scenarios like how the system handles large data payloads or reacts to invalid inputs.
  • Test under different conditions: It's important to test your integration in varied environments. You can use tools like Apache JMeter or Blazemeter to simulate different network conditions, loads, and user behaviors to see how your integration holds up.
  • Security testing: Conducting thorough security testing is imperative to protect your data and systems. Use a tool like Nessus to scan for vulnerabilities and ensure your integration is secure against potential threats.
  • Version control for test cases: Implementing version control for your test cases is essential. This practice allows you to track changes, manage different test scenarios effectively, and ensures consistency in your testing process over time.

Final thoughts

In all likelihood, your clients use a spectrum of ticketing systems. 

You can broaden the integrations you offer quickly and easily by building to Merge's Ticketing Unified API. Once connected to the API, you’ll be able to offer 30+ integrations, which include tools like Jira, ClickUp, Zendesk, ServiceNow, etc.

To get a deeper understanding of Merge, you can schedule a demo with one of our integration experts.

Email Updates

Subscribe to the Merge Blog

Get stories from Merge straight to your inbox