How to run API tests in Jenkins

Plenty of teams still run their builds on Jenkins, and there's nothing wrong with that. Jenkins reads JUnit reports out of the box, keeps credentials in its own store, and runs anything you can run in a shell. That's all you need to run API tests with the Requestarium CLI.

Before you start

  • Node.js 20.10 or later on the agent, or a Docker agent with a node:22 image.
  • An API token from Settings → API tokens in Requestarium. In Jenkins, add it under Manage Jenkins → Credentials as a Secret text credential with the ID requestarium-token. Add any API keys your requests need the same way, for example staging-api-key.
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.

The Jenkinsfile

Jenkinsfile
pipeline {
  agent { docker { image 'node:22' } }

  environment {
    REQUESTARIUM_TOKEN = credentials('requestarium-token')
    API_KEY            = credentials('staging-api-key')
  }

  stages {
    stage('Install CLI') {
      steps {
        sh 'npm install -g @requestarium/cli'
      }
    }
    stage('API tests') {
      steps {
        sh '''
          requestarium run-collection "Orders API" --env Staging --env-var API_KEY \
            --junit results.xml --html report.html --save-run --no-color
        '''
      }
    }
  }

  post {
    always {
      junit 'results.xml'
      archiveArtifacts artifacts: 'report.html', allowEmptyArchive: true
    }
  }
}

How it fits together:

  • credentials(...) exposes the secret as an environment variable and masks it in the console log. The CLI uses REQUESTARIUM_TOKEN automatically, so there's no login step.
  • --env-var API_KEY makes the Jenkins secret available to your requests as {{API_KEY}}, masked in reports too.
  • junit 'results.xml' publishes the results. Each request run is a test suite and each pm.test is a test case, so Jenkins' test trend graph shows API tests next to your unit tests.
  • --no-color keeps the console log readable if you don't use the ANSI color plugin.
  • --save-run records the run in Requestarium with the branch, commit and a link back to the Jenkins build.

Without Docker

If your agents run Node.js directly, drop the docker agent and use agent any. If you can't install packages globally, prefix the command with npx:

Jenkinsfile
sh 'npx @requestarium/cli run-collection "Orders API" --env Staging --junit results.xml'

Behind a corporate proxy

Jenkins agents often sit behind a proxy and trust an internal certificate authority. The CLI has flags for both:

Terminal
requestarium run-collection "Orders API" --env Staging \
  --proxy http://proxy.internal:3128 --ca-cert /etc/ssl/certs/company-ca.pem

Nightly regression

Add a trigger to run the pipeline every night, and fail it on slow responses as well as broken ones:

Jenkinsfile
triggers { cron('H 3 * * *') }
Terminal
requestarium run-collection "Orders API" --env Staging --max-p95 800 --save-run