Access Gitlab API from CI pipeline

I`m trying to request GET /projects/:id/pipelines/:pipeline_id/jobs endpoint from pipeline to get information about coverage percent. The goal is to fail job if coverage is below certain value. I use CI_JOB_TOKEN to authenticate api, but it returns error {“message”:“404 Project Not Found”}.
Script that I use in job:

- CURRENT_COVERAGE=`curl -s "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/pipelines/${CI_PIPELINE_ID}/jobs?private_token=${CI_JOB_TOKEN}" | jq --arg JOB_NAME "$JOB_NAME" '.[] | select(.name==$JOB_NAME) | .coverage'`
- echo "current coverage ${CURRENT_COVERAGE}"
- if [ "$CURRENT_COVERAGE" -lt "$MIN_COVERAGE" ]; then echo "Current coverage ${CURRENT_COVERAGE} is less than min coverage ${MIN_COVERAGE}" && exit 1; else exit 0; fi;

I tried supplying CI_JOB_TOKEN as job_token as well and it doesnt work, but it works if I supply my personal private_token, but I dont think it`s great idea to use private_token in pipeline.

How should I authenticate gitlab API in job script?
Or maybe there is other solution to fail pipeline if coverage is below certain percent (I use NodeJS/Jest for unit testing)?

I’m not sure why the CI token isn’t working, but you can use your PAT, add it to the CI/CD env vars in your project settings, and set that to Masked so that the token won’t appear in the pipeline logs.