Testing the GitLab REST API with a project access token
GitLab's REST API is what release scripts, bots and internal dashboards call to create merge requests, read pipelines and tag releases. It's well documented and predictable, with two details that trip people up: projects are addressed by a URL-encoded path, and pagination lives in response headers, not in the body.
Step 1: A token and an environment
For tests, prefer a project access token (Project ▸ Settings ▸ Access tokens) with read_api, or api if the tests write. It's limited to one project and doesn't belong to a person. On gitlab.com, project tokens need Premium; otherwise use a personal access token of a test user.
| Variable | Value |
|---|---|
baseUrl | https://gitlab.com/api/v4, or https://gitlab.example.com/api/v4 |
gitlabToken | the token, marked as secret |
projectPath | my-group%2Fmy-project |
On the collection's Authorization tab, choose Bearer token with {{gitlabToken}}. GitLab accepts it as well as its own PRIVATE-TOKEN header.
Step 2: Who am I?
GET {{baseUrl}}/userWith a project token you get the token's bot user. A 401 here means the token is wrong or expired, which is worth knowing before anything else fails.
Step 3: Projects by path
A project's path contains a slash, so it must be URL-encoded:
GET {{baseUrl}}/projects/{{projectPath}}pm.test('project found', () => {
pm.expect(pm.response.code).to.equal(200);
const p = pm.response.json();
pm.expect(p).to.have.property('id');
pm.environment.set('projectId', String(p.id));
});After this, use {{projectId}}. Numeric ids don't break when someone renames the group.
Step 4: Pagination is in the headers
List endpoints return 20 items by default, up to 100 with per_page:
GET {{baseUrl}}/projects/{{projectId}}/merge_requests?state=opened&per_page=100const next = pm.response.headers.get('x-next-page');
pm.test('pagination headers present', () => {
pm.expect(pm.response.headers.get('x-per-page')).to.equal('100');
});
console.log('next page:', next || 'none');x-next-page is empty on the last page. x-total and x-total-pages can be missing on very large collections, so don't make a test depend on them. For a general approach see how to test API pagination.
Step 5: Create something and clean up
POST {{baseUrl}}/projects/{{projectId}}/issues
Content-Type: application/json
{"title": "API test issue {{$timestamp}}", "labels": "api-test"}pm.test('issue created', () => pm.expect(pm.response.code).to.equal(201));
pm.environment.set('issueIid', String(pm.response.json().iid));Issues are addressed by iid, the number shown in the UI, within the project. Delete it in a cleanup step with DELETE {{baseUrl}}/projects/{{projectId}}/issues/{{issueIid}} (that needs Owner or admin rights; otherwise close it with state_event=close). See setup and cleanup steps.
Step 6: The errors worth testing
| Status | Usually means |
|---|---|
401 | missing, wrong or expired token |
403 | the token is valid but its scope or role isn't enough |
404 | the project doesn't exist or the token can't see it; GitLab doesn't tell you which |
409 | something with that name already exists |
429 | rate limited; wait for Retry-After |
That 404 is deliberate: GitLab hides private projects from tokens that can't see them. When a test gets 404 on a project you know exists, check the token's membership before the URL.
Run it in CI
requestarium run-collection "GitLab API" --env GitLab --env-var gitlabToken=GITLAB_TEST_TOKEN