Making API Calls In a Node.js Add-On Application

I'm making my first Node.js Add-On for Jira using the Atlassian-Connect Express framework and I'm having trouble making API calls.

In prior Add-Ons I used a basic JavaScript/PHP/Apache setup in order to create a connection between my own application and my JIRA instance using basic authorization (no JWT tokens). Making an API call was as simple as using AP.require.

I'm not entirely sure how to make API calls using ACE because when I use AP.require I receive a 403 error for simple GET requests. Should I be using Node to make API calls rather than AP.require? And where in ACE would I do this? I'm not sure if I have to create a route for making API calls or if I have to create a function in the Express build in app.js.

Originally I had my API calls setup in its own .js file as the following:

const baseUrl = "myinstance.atlassian.net";

function getAllProjects() {
return get(baseUrl + '/rest/api/2/project');
}

function get(url) {
return fetch(baseUrl + url).then(onSuccess, onError);
}

function onSuccess(response) {
return response.json();
}

function onError(error) {
console.log(error); // eslint-disable-line no-console
}

 When I try this I am unable to make a call to the API because it attaches my ngrok host link to the URL request like so:

ngrock_address/myinstance.atlassian.net/rest/api/2/project.

So I changed the code to the following:

const baseUrl = "myinstance.atlassian.net";
const token = document.getElementById("jwt-token").content;

function getAllProjects() {
return get(baseUrl + '/rest/api/2/project');
}

function get(url) {
AP.require('request', function(request) {
request({
url: url,
headers: {
'Authorization': `JWT ${token}`,
},
success: function(response) {
response = JSON.parse(response);
console.log(response);
},
error: function() {
console.log(arguments);
}
});
});
}

function onSuccess(response) {
return response.json();
}

function onError(error) {
console.log(error); // eslint-disable-line no-console
}

 But this only yields a 403 forbidden response.

 

1 answer

Don't you need to provide any sort of authentication?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 21, 2018 in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

1,243 views 10 18
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you