There was an error invoking the function - Jira API error 400

Hi,

I can not solve my bug in index.js NodeJS back-end code for my Jira App made with Forge API:

import api, { route } from '@forge/api';

export async function getIssues({ project = null } = {}) {
  try {
    console.log('getIssues called, project:', project);

    // Build JQL query
    let jql = 'ORDER BY id DESC';
    if (project && project !== '-- ALL --') {
      jql = `project = "${project}" AND ${jql}`;
    }

    console.log('JQL query:', jql);

    // Build URL as string (no template literal route with variable)
    const url = route`/rest/api/3/search/jql`;

    const body = {
      jql,
      startAt: 0,
      maxResults: 50,
      fields: ['summary', 'issuetype', 'project']
    };

    const res = await api.asApp().requestJira(url, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(body)
    });

    console.log('Jira search status:', res.status);

    if (res.status >= 400) {
      const text = await res.text();
      console.error('API Error status:', res.status, 'body:', text);
      throw new Error(`Jira API error ${res.status}`);
    }

    const data = await res.json();

    const issues = (data.issues || []).map(i => ({
      id: i.id,
      key: i.key,
      summary: i.fields?.summary || '(no summary)',
      avatarUrl: i.fields?.issuetype?.iconUrl || '',
      issueType: i.fields?.issuetype?.name || 'Unknown',
      projectKey: i.fields?.project?.key || ''
    }));

    console.log('Parsed issues count:', issues.length);
    return { issues };
  } catch (err) {
    console.error('getIssues error:', err);
    throw err;
  }
}

What is the problem and Copilot can not help in Visual Studio Code AI Chat. Copilot haves outdated knowledge and know only outdated functions in Jira REST API.

Hello @matti.kiviharju

If you refer to the Search for Issues Using JQL endpoint documentation, you will see that that it does not support a startAt parameter.

Hi. I removed this but console.log in forge tunnel error is:

INFO    04:59:14.251  d88c601c-b4d2-4dfb-9477-65f193eba29a  Request body: {"jql":"ORDER BY createdDate DESC","maxResults":50,"fields":["summary","issuetype","project"]}
INFO    04:59:14.873  d88c601c-b4d2-4dfb-9477-65f193eba29a  Jira search status: 400
ERROR   04:59:14.877  d88c601c-b4d2-4dfb-9477-65f193eba29a  API Error status: 400 body: {"errorMessages":["Unbounded JQL queries are not allowed here. Please add a search restriction to your query."],"errors":{}}

I recommend that you do a basic Google / ChatGPT search on that error as, IMHO, that topic has also been discussed too many times in too many places to warrant yet another explanation.

Alternatively, search the public Jira Community forum for “Unbounded JQL queries” and you’ll find where the same question is frequently asked and answered.

Have fun!

I already use ChatGPT and Copilot in my Visual Studio Code but both always answerd with outdated examples like /rest/api/3/search and not /rest/api/3/search/jql and giving bad code based outdated requests. Thats seams to help if I write links to latest API docs.

I used Google Search instead Copilot/ChatGPT beauce AIs just tries to quest what is bug and suggest different answer per error logs, etc.

I got it now work:

With JQL:

let jql = 'updated >= -90d OR created >= -90d ORDER BY created DESC';

But now problem is timesheet not list all issues paginated. There is older issues than created in 90 days.