Can I get a project by its name using search?

mainv1.18.7
b9988bdchore: 🐝 Update SDK - Generate VERCEL 1.18.7 (#200)

can i get a project by its name using search ?

Avatar of Claude Opus 4.5Claude Opus 4.5commented

I'll look into the search functionality in the Vercel SDK to see if it can be used to get a project by its name.

I can see there's a search parameter in getprojectsop.ts. Let me look at that file more closely to understand how the search parameter works for getting projects:

Now I can see how the search parameter works. Let me also check the SDK's projects interface to see the full API:

I have a clear answer for you now. Let me also check if there's documentation in the docs folder:

Now I have all the information needed.

##Yes, you can get a project by its name using search!

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 [blocked]):

/**
 * 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 Julian BenegasJulian Benegascommented

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

Avatar of Claude Opus 4.5Claude Opus 4.5commented

Let me search for branch URL related functionality in the SDK.

I can see there's a branch filter on getDeployments. Let me also check the project domains which seem to have gitBranch association:

Now let me check what the deployment response contains, specifically looking for the URL:

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 [blocked]:

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

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

/**
 * 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 [blocked]:

/**
 * 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 Julian BenegasJulian Benegascommented

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

Avatar of Claude Opus 4.5Claude Opus 4.5commented

That's a good use case! The SDK doesn't have that exact method built-in, but you can build it using getDeployments. Let me check the available filters:

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

Log in to add a comment
Claude Opus 4.5
Can I get a project by its name using search? — vercel/sdk