Testing the Vercel REST API for deployments and environment variables

On Vercel, every push gets a deployment with its own URL. That's ideal for API tests: run the collection against the preview before merging. The missing piece is knowing which URL and when it's ready, and that's what the Vercel REST API is for.

Step 1: Token and environment

In Vercel, Account Settings ▸ Tokens, create a token scoped to the team that owns the project, with an expiry.

VariableValue
vercelBasehttps://api.vercel.com
vercelTokenthe token, marked as secret
teamIdteam_… (Team Settings ▸ General)
projectIdprj_… (Project Settings ▸ General)

Authorization: Bearer token with {{vercelToken}}. For team projects, every request needs ?teamId={{teamId}}; without it, Vercel looks in your personal account and answers 404 or an empty list. That's the most common surprise with this API.

Step 2: The latest deployment for a branch

HTTP
GET {{vercelBase}}/v6/deployments?projectId={{projectId}}&teamId={{teamId}}&limit=5&target=preview
JavaScript
const deps = pm.response.json().deployments;
const branch = pm.environment.get('gitBranch');
const mine = deps.find((d) => d.meta && d.meta.githubCommitRef === branch);
pm.test('deployment for this branch exists', () => pm.expect(mine).to.exist);
pm.environment.set('deploymentId', mine.uid);

Pass gitBranch from CI with --var gitBranch=$BRANCH. The meta fields come from the Git integration; their names depend on the provider (githubCommitRef, gitlabCommitRef, …).

Step 3: Wait until it's ready

HTTP
GET {{vercelBase}}/v13/deployments/{{deploymentId}}?teamId={{teamId}}
JavaScript
const d = pm.response.json();
pm.test('deployment is ready', () => pm.expect(d.readyState).to.equal('READY'));
pm.environment.set('previewUrl', `https://${d.url}`);

In CI, call this in a small loop until it's READY or ERROR, then run the collection against {{previewUrl}}:

Terminal
for i in $(seq 1 30); do
  state=$(requestarium run "Get deployment" --env Vercel --env-var vercelToken=VERCEL_TOKEN --var deploymentId=$ID --json \
    | jq -r '.response.body | fromjson | .readyState')
  [ "$state" = "READY" ] && break
  [ "$state" = "ERROR" ] && exit 1
  sleep 10
done
requestarium run-collection "Orders API" --env Preview --var baseUrl=https://$URL

See testing preview environments for pull requests for the rest of that pipeline.

Step 4: Protected previews

If Deployment Protection is on, requests to the preview URL answer 401 with a login page. Create a Protection Bypass for Automation secret in the project settings and send it as the x-vercel-protection-bypass header in your API collection (as a secret variable), so tests reach the app without turning protection off.

Step 5: Environment variables

HTTP
GET {{vercelBase}}/v9/projects/{{projectId}}/env?teamId={{teamId}}

Values of sensitive variables aren't returned. A useful check is that the variables your app needs exist for the preview target:

JavaScript
const keys = pm.response.json().envs.filter((e) => e.target.includes('preview')).map((e) => e.key);
pm.test('preview has the database URL', () => pm.expect(keys).to.include('DATABASE_URL'));