What's the process for programmatically adding comments to Jira Data Center issues?

I’m trying to figure out how to add comments to Jira Data Center issues using code. I’ve got the right permissions and can make authenticated requests, but I’m not sure about the exact endpoint and how to format the data I’m sending.

Here’s a snippet of what I’ve tried:

const jiraApi = axios.create({
  baseURL: 'https://jira.mycompany.com/rest/api/2',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer myToken123'
  }
});

const addComment = async (issueKey, commentText) => {
  try {
    const response = await jiraApi.post(`/issue/${issueKey}/comment`, {
      body: commentText
    });
    console.log('Comment added:', response.data);
  } catch (error) {
    console.error('Error adding comment:', error);
  }
};

addComment('PROJ-123', 'This is a test comment');

Can someone help me understand if this is the right way to do it? Are there any other headers or auth details I need to include? Thanks!

I’ve implemented something similar in my work, and your code looks spot-on. One thing I’d suggest is adding error handling for different HTTP status codes. It’s helped me pinpoint issues faster.

For example, a 401 might mean your token expired, while a 403 could indicate insufficient permissions. Also, if you’re dealing with a lot of issues, consider implementing a rate limiter to avoid hitting Jira’s API limits.

In my experience, it’s also useful to add a method for retrieving existing comments. This way, you can check if a similar comment already exists before adding a new one, reducing duplication.

Lastly, make sure your token has the right scope. I once spent hours debugging only to realize my token didn’t have the ‘write:comment’ scope. Hope this helps!

Your approach appears solid. The endpoint and structure for adding comments to Jira Data Center issues are correct. I suggest confirming that your authentication token has the necessary permissions to perform this action. In some cases, Jira may also require an additional header such as X-Atlassian-Token: no-check to bypass CSRF restrictions. Furthermore, refining your error handling for different HTTP status codes could help diagnose issues more precisely. If comment visibility restrictions are needed, consider including a visibility field. It is always advisable to consult the latest Atlassian API documentation for any updates.

your approach looks good mate. one thing tho, make sure ur token’s not expired. that’s bitten me before lol. also, if ur adding lots of comments, watch out for rate limits. jira can get grumpy if u hammer it too hard. good luck!