Can I get a project by its name using search?

mainv1.18.7
b9988bdchore: 🐝 Update SDK - Generate VERCEL 1.18.7 (#200)
This post might have stale content, as main is 45 commits ahead.

can i get a project by its name using search ?

Avatar of anthropic/claude-opus-4.5
Claude Opus 4.5
commented

Now I have all the information needed.

The getProjects method supports a search parameter that filters projects by their name. Here's how it works:

###From the source code (src/models/getprojectsop.ts):

/**
 * Search projects by the name field
 */
search?: string | undefined;

###Usage example:

const result = await vercel.projects.getProjects({
  search: "my-project-name"
});

###Important notes:

  1. It returns a list - The search parameter filters the project list, so you'll get an array of matching projects, not a single project. You'll need to iterate or pick the first result.

  2. Partial matching - The search likely performs a partial/fuzzy match on the name field, so searching for "my-proj" might return "my-project", "my-project-2", etc.

  3. If you need an exact match, you might want to:

    • Use search and then filter the results for an exact name match
    • Or use other project methods like updateProject or deleteProject which accept the project name directly via idOrName parameter (as shown in the source: "Update the fields of a project using either its name or id" and "Delete a specific project by passing either the project id or name")

Would you like me to show you more details about how to work with the search results or other ways to get a project by name?

Avatar of julianbenegas
Julian Benegas
commented

great! and what if i want to get branch URL of a branch in a project?

Avatar of anthropic/claude-opus-4.5
Claude Opus 4.5
commented

Now I have a complete picture. Let me summarize what I found:

##Getting a Branch URL

The Vercel SDK doesn't have a direct method to get a "branch URL". However, there are a few approaches you can use:

###Option 1: List Deployments filtered by branch

The getDeployments method supports filtering by branch:

const deployments = await vercel.deployments.getDeployments({
  projectId: "your-project-id",
  branch: "feature-branch",    // Filter by branch name
  state: "READY",              // Only ready deployments
  limit: 1                     // Get latest one
});

// Get the URL from the deployment
const branchUrl = deployments.deployments[0]?.url;

From src/models/getdeploymentsop.ts:

/**
 * Filter deployments based on the branch name
 */
branch?: string | undefined;

Each deployment in the response includes a url property (line 446):

/**
 * The URL of the deployment.
 */
url: string;

###Option 2: Get Project Domains filtered by branch

You can list domains associated with a specific git branch:

const domains = await vercel.projects.getProjectDomains({
  idOrName: "your-project",
  gitBranch: "feature-branch"  // Filter domains by branch
});

// Each domain has a name which is the URL
const branchDomainUrl = domains.domains[0]?.name;

From src/models/getprojectdomainsop.ts:

/**
 * Filters domains based on specific branch.
 */
gitBranch?: string | undefined;

###Which approach to use?

  • Use getDeployments if you want the latest deployment URL for a specific branch
  • Use getProjectDomains if you have custom domains configured per branch (domains return the configured domain name, not just deployment URLs)

Would you like more details on either approach?

Avatar of julianbenegas
Julian Benegas
commented

getDeployments sounds good. what if i want getDeploymentURLByBranchOrCommit({ project, branchOrCommit })?

Avatar of anthropic/claude-opus-4.5
Claude Opus 4.5
commented

The SDK has both branch and sha (commit) filters. Here's how you could implement getDeploymentURLByBranchOrCommit:

import { Vercel } from "@vercel/sdk";

interface GetDeploymentURLOptions {
  project: string;
  branchOrCommit: string;
  teamId?: string;
}

async function getDeploymentURLByBranchOrCommit(
  vercel: Vercel,
  { project, branchOrCommit, teamId }: GetDeploymentURLOptions
): Promise<string | null> {
  // First, try to find by branch name
  const byBranch = await vercel.deployments.getDeployments({
    projectId: project,
    branch: branchOrCommit,
    state: "READY",
    limit: 1,
    teamId,
  });

  if (byBranch.deployments.length > 0) {
    return byBranch.deployments[0].url;
  }

  // If not found by branch, try by commit SHA
  const bySha = await vercel.deployments.getDeployments({
    projectId: project,
    sha: branchOrCommit,
    state: "READY",
    limit: 1,
    teamId,
  });

  if (bySha.deployments.length > 0) {
    return bySha.deployments[0].url;
  }

  return null;
}

###Usage:

const vercel = new Vercel({ bearerToken: "your-token" });

// By branch name
const url1 = await getDeploymentURLByBranchOrCommit(vercel, {
  project: "my-project-id",
  branchOrCommit: "feature-branch",
});

// By commit SHA
const url2 = await getDeploymentURLByBranchOrCommit(vercel, {
  project: "my-project-id", 
  branchOrCommit: "abc123def456",
});

###Key filters from the SDK:

FilterDescription
branchFilter deployments based on the branch name
shaFilter deployments based on the commit SHA
stateFilter by state (READY, BUILDING, ERROR, etc.)
limitMaximum number of deployments to return
projectIdFilter by project ID or name

Note: The function tries branch first, then falls back to SHA. If you know ahead of time which one you're passing, you could optimize by only making one API call. You could also detect SHA format (40-char hex string) to decide which filter to use first.


END OF POST

Can I get a project by its name using search? — vercel/sdk