We tried to use the following logic: get issues for board (https://developer.atlassian.com/cloud/jira/software/rest/#api-rest-agile-1-0-board-boardId-issue-get) and subtract the issues for backlog (https://developer.atlassian.com/cloud/jira/software/rest/#api-rest-agile-1-0-board-boardId-backlog-get).
This seemed to work, but found a case when it does not.
https://<my_jira_base_url>/rest/agile/1.0/board/5/issue
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 4,
"issues": [...]
https://<my_jira_base_url>/rest/agile/1.0/board/5/backlog
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 3,
"issues": [...]
The backlog enpoint does not return the NG2-2 issue, which is resolved, but visible in the Backlog screen.
All of this is quite confusing, and really seemes to be a bug in the backlog endpoint.