Chuckkir
Chuckkir

Reputation: 93

How do I get the PackageID for Azure Devops API?

I need the list of available versions for a Universal Package stored in Azure Devops. My thought is to call the REST API Get Package Versions to get a list of the versions for packages on a feed.

GET https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/Feeds/{feedId}/Packages/{packageId}/versions?api-version=5.1-preview.1

The problem is that it requires a packageId, which is the GUID and I only know the name. The only way I've figured out so far to convert a package name to a GUID is using "Get Packages" but that returns every package on the feed (which for me includes thousands of NPM packages) and that makes the download very large for the handful of items I need. Is there some way to extract the packageId for a given package name? Or is there a better way to extract all the versions for a package?

Upvotes: 5

Views: 7736

Answers (2)

Chuckkir
Chuckkir

Reputation: 93

Someone pointed out to me that the Get Packages API has options for IncludeAllVersions and packageNameQuery to achieve what I want rather than using GetAllVersions.

https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/Feeds/{feedId}/Packages?includeAllVersions=true&packageNameQuery={packageName}&protocol​Type=nuget

Upvotes: 3

Mengdi Liang
Mengdi Liang

Reputation: 19026

I assume you have checked some docs and found there's no direct API can let you get specified packaged ID, right? Also, as the doc said, the package name could not be used in this API:

enter image description here


In fact, you have very close to the answer. Just add some filter while you running Get Packages. But this need you execute some script in Powershell command line or Powershell ISE which is the most convenient approach can for you use. You can also run below script in Azure Devops pipeline, but compare with running in command line, it is a bit cumbersome.

Run below script in your Powershell command line or Powershell ISE:

$token = "{your PAT token}"
$url = 'https://feeds.dev.azure.com/{org name}/_apis/packaging/Feeds/{feed ID}/packages'

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get

$results = $response.value | Where {$_.name -eq "{Package Name}"} #|

Write-Host "results = $($results | ConvertTo-Json -Depth 100)"

Note: In the above script, you just need to replace the value of PAT token, Organization name and your package name.


Then you will get the info of the specified package, and you can copy the package ID from the command line and apply it in another place :

enter image description here

Note: The above script can also be applied in the Powershell task of Azure Devops without change anything.

Upvotes: 1

Related Questions