How to run API tests in GitLab CI

GitLab has good built-in support for test reports: give it a JUnit file and it shows passed and failed tests right on the merge request. That makes it a natural place to run API tests. This guide sets up the Requestarium CLI in GitLab CI so the collections your team already maintains run on every merge request.

Before you start

  • A collection with some pm.test checks, and an environment such as Staging.
  • An API token for CI, from Settings → API tokens in the app (it starts with trt_). Use a dedicated one so you can revoke it on its own.
The API tokens settings page with a new token named GitHub Actions, shown once with a Copy button
Create a dedicated token for CI in Settings → API tokens. It is shown only once.

In GitLab, open Settings → CI/CD → Variables and add:

KeyValueOptions
REQUESTARIUM_TOKENyour trt_… tokenMasked
STAGING_API_KEYthe API key your requests needMasked

The pipeline

Add a job to .gitlab-ci.yml:

YAML
api-tests:
  image: node:22
  stage: test
  script:
    - npm install -g @requestarium/cli
    - requestarium run-collection "Orders API" --env Staging
        --env-var API_KEY=STAGING_API_KEY
        --junit results.xml --html report.html --save-run
  artifacts:
    when: always
    paths:
      - results.xml
      - report.html
    reports:
      junit: results.xml

What each part does:

  • REQUESTARIUM_TOKEN is picked up from the environment, so no login step is needed.
  • --env-var API_KEY=STAGING_API_KEY reads the GitLab variable STAGING_API_KEY into the collection variable {{API_KEY}}. It is masked in every report, and the run stops if the variable is missing.
  • reports: junit makes GitLab show the results in the merge request's test summary. Each pm.test is one test case.
  • when: always keeps the reports even when the job fails, which is exactly when you need them.
  • --save-run stores the run in the collection's history in Requestarium, with the branch, the commit and a link to the GitLab job.

The CLI exits with code 1 when any request fails, errors or has a failed test, so the job, and the merge request, go red.

Run only what matters on merge requests

A full regression run can be slow. Run a smoke folder on merge requests and everything on the default branch:

YAML
api-smoke:
  image: node:22
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  script:
    - npm install -g @requestarium/cli
    - requestarium run-collection "Orders API" --folder Smoke --env Staging --bail --junit results.xml
  artifacts:
    when: always
    reports:
      junit: results.xml

api-full:
  image: node:22
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - npm install -g @requestarium/cli
    - requestarium run-collection "Orders API" --env Staging --retries 2 --junit results.xml --save-run
  artifacts:
    when: always
    reports:
      junit: results.xml

Nightly runs with a schedule

Under Build → Pipeline schedules, create a schedule (for example every night at 03:00). Then run a job only for scheduled pipelines:

YAML
api-nightly:
  image: node:22
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
  script:
    - npm install -g @requestarium/cli
    - requestarium run-collection "Orders API" --env Staging --save-run --max-p95 800

--max-p95 800 also fails the run when the 95th percentile response time goes above 800 ms. See Set a performance budget for your API tests.

Look at the history

Because each run is saved, the collection's run history in the app shows GitLab runs next to runs from developers' laptops. From a terminal:

Run history of a collection: CI runs on main and a feature branch next to a run from the app
With --save-run, GitLab runs show up in the collection's Runs tab, with their branch
Terminal
requestarium runs "Orders API"