typescriptintermediate

Vercel API — Trigger and List Deployments

Use the Vercel REST API to trigger deployments, list recent builds, and check deployment status.

typescript
const VERCEL_TOKEN = process.env.VERCEL_TOKEN!;
const PROJECT_ID = process.env.VERCEL_PROJECT_ID!;

interface Deployment {
  uid: string;
  url: string;
  state: 'READY' | 'ERROR' | 'BUILDING' | 'QUEUED';
  created: number;
}

async function vercelFetch<T>(path: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`https://api.vercel.com${path}`, {
    ...init,
    headers: {
      Authorization: `Bearer ${VERCEL_TOKEN}`,
      'Content-Type': 'application/json',
      ...init?.headers,
    },
  });
  if (!res.ok) throw new Error(`Vercel API ${res.status}: ${await res.text()}`);
  return res.json();
}

// List recent deployments
async function listDeployments(limit = 5): Promise<Deployment[]> {
  const { deployments } = await vercelFetch<{ deployments: Deployment[] }>(
    `/v6/deployments?projectId=${PROJECT_ID}&limit=${limit}`
  );
  return deployments;
}

// Trigger a redeployment
async function redeploy(deploymentId: string): Promise<Deployment> {
  return vercelFetch<Deployment>('/v13/deployments', {
    method: 'POST',
    body: JSON.stringify({
      name: PROJECT_ID,
      deploymentId,
      target: 'production',
    }),
  });
}

// Check deployment status
async function getStatus(deploymentId: string): Promise<string> {
  const { state } = await vercelFetch<Deployment>(
    `/v13/deployments/${deploymentId}`
  );
  return state;
}

Use Cases

  • Triggering deployments from CI or Slack bots
  • Monitoring deployment status programmatically
  • Building internal deployment dashboards

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.