Run API tests against a preview environment for every pull request

Vercel, Netlify, Render, Fly, Kubernetes namespaces per branch: many teams deploy every pull request to its own URL, such as https://pr-482.preview.example.com. That's the best place to run API tests, before anything is merged. The only thing that changes per run is the base URL.

One environment, one variable that changes

Create an environment Preview with the values every preview shares: test account ids, feature flags, secret variables for tokens. Leave baseUrl out, or set it to a placeholder. Requests use {{baseUrl}} as usual:

HTTP
GET {{baseUrl}}/health

Values passed on the command line win over the environment, so a run can fill baseUrl without anyone editing the environment.

With the CLI (GitHub Actions)

.github/workflows/preview-api-tests.yml
name: Preview API tests
on:
  deployment_status:

jobs:
  api-tests:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npm install -g @requestarium/cli
      - name: Run the suite against the preview
        env:
          REQUESTARIUM_TOKEN: ${{ secrets.REQUESTARIUM_TOKEN }}
        run: |
          requestarium run --suite "Orders/Smoke" --env Preview \
            --var baseUrl="${{ github.event.deployment_status.target_url }}" \
            --save-run --junit results.xml

deployment_status fires when your hosting provider reports the deployment, with the preview URL in target_url. Other setups expose the URL as a job output or an environment variable; the --var part is the same.

--save-run records the run with the branch, the commit and a link to the job, so the suite's history shows which pull request a failure came from. Inside GitHub Actions, the results also appear in the job summary.

With the Run API (no install)

If the preview is reachable from the internet, Requestarium's server can run the tests for you:

Terminal
curl -sS https://api.requestarium.com/api/v1/runs \
  -H "Authorization: Bearer $REQUESTARIUM_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: preview-$PR_NUMBER-$COMMIT_SHA" \
  -d "{
    \"workspace_id\": \"$WORKSPACE_ID\",
    \"target\": { \"kind\": \"suite\", \"id\": \"$SUITE_ID\" },
    \"environment_id\": \"$PREVIEW_ENV_ID\",
    \"variables\": { \"baseUrl\": \"$PREVIEW_URL\" },
    \"wait\": 60
  }"

With wait, the call returns the finished run (or what's done after 60 seconds, with a link to poll). GET /api/v1/runs/{id}/junit gives you JUnit XML for the CI test view. The idempotency key makes a retried CI step return the same run instead of starting a second one. See the Run API article.

Server-side runs reach public addresses only. For previews behind a VPN or in a private cluster, use the CLI in a runner that can reach them.

Wait for the preview to be ready

"Deployed" and "ready" aren't always the same moment. Start the run with a request that waits for the health endpoint, or retry the first request:

Terminal
requestarium run "Health" --env Preview --var baseUrl="$PREVIEW_URL" --retries 5

--retries resends on connection errors and 502/503/504, waiting 1, 2, 4, 8 and 16 seconds.

Test data per preview

Each preview usually has its own empty database. Make the suite create what it needs in Setup steps (a user, a token, a product) and remove it in Clean up steps, instead of relying on seeded ids. See setup and clean-up steps.