Query Latest Packages Endpoint
  • 04 Jan 2024
  • 2 Minutes to read
  • Dark
    Light
  • PDF

Query Latest Packages Endpoint

  • Dark
    Light
  • PDF

Article Summary

The Query Latest Package is an endpoint in ProGet's Common Packages API that will return a JSON array of PackageVersion objects describing the latest version of each package in the feed that match the specified query arguments.

GET /api/packages/«feed-name»/latest

Request Specification

To query a package version, simply GET to the URL with a feed name, group name, package name and stableOnly parameters, and an appropriate API Key.

GET /api/packages/«feed-name»/latest[?group=«group»][&name=«name»][&stableOnly=«true/false»]

Note that all parameters are optional. The stableOnly parameter defaults to false, but when set true, latest stable versions of packages are returned instead of absolute latest versions

Response Specification

A successful (200) response body will contain an array of PackageVersion objects. For example, to querying the latest stable version of myNuGetPackage, the request would return this:

GET /api/packages/MyNuGetFeed/latest?name=myNugetPackage&stableOnly=true

[{
    "purl":"pkg:nuget/myNugetPackage@3.0.1",
    "group":null,
    "name":"myNugetPackage",
    "version":"3.0.1",
    "totalDownloads":0,
    "downloads":0,
    "published":"2023-10-11T03:22:50.517Z",
    "publishedBy":"Admin",
    "size":2441966,
    "md5":"94d7f4cce0663c6a30340fe6fec831da",
    "sha1":"f418efd4238eb069cf38d1c86f4edc3444477dd",
    "sha256":"872fc189e638ab1056555b03aaa38f68bcb54286e221aa646eb1129babf63c77",
    "sha512":"99b252bc77d1c5f5f7b51fd4ea7d5653e9961d7b3061cf9207f8643a9c7cc99
        65eebc84d6467f2989bb4723b1a244915cc232a78f894e8b748ca882a7c89fb92",
    "versions":["3.0.1"]
}]

Note that, if an API call is made on a package that does not exist, an empty object is returned. In addition, Package hash values may not be present for all packages because earlier versions of ProGet would typically only generate some of the hash types.

ResponseDetails
200 (Success)body will contain an array of PackageVersion objects
403 (Unauthorized API Key)indicates a missing, unknown, or unauthorized API Key; the body will be empty
500 (Server Error)indicates an unexpected error; the body will contain the messsage and stack trace, and this will also be logged

Sample Usage Scripts

Latest npm packages in scope (Powershell)

This script will print out the name and latest version number of all stable (non prerelease) packages in the @mygroup scope in the private-npm feed.

$feedName = "private-npm"
$apiUrl = "https://proget.corp.local/api/packages/$feedName/latest"
$apiKey = "a1b2c3d4e5"

$queryParams = @{
    group = "@mygroup"
    stableOnly = "true"
}

$headers = @{
    "X-API-Key" = $apiKey
}

$response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Get -Body $queryParams

foreach ($package in $response) {
    Write-Host "Package Name: $($package.name)"
    Write-Host "Latest Version: $($package.version)"
    Write-Host "--------------------"
}

Running this script will output something like this

Package Name: core
Latest Version: 16.2.6
--------------------
Package Name: material
Latest Version: 16.2.9
--------------------
Package Name: cdk
Latest Version: 16.2.9
--------------------

Check Latest version (Python)

This function checks to see if 4.2.1 is the latest version of the GeneralUtils.NET package in the private-nuget feed, and returns a message depending on the result.

import requests

base_url = "https://proget.corp.local.org"
feed_name = "MyNuGetFeed"
package_name = "MyPackage"
desired_version = "13.0.3"
stable_only = True 
api_key = "a1b2c3d4e5"

api_url = f"{base_url}/api/packages/{feed_name}/latest?name={package_name}&stableOnly={stable_only}"

def is_latest_version(desired_version, latest_version):
    return latest_version == desired_version

response = requests.get(api_url, headers={"Authorization": f"Bearer {api_key}"})

if response.status_code == 200:
    data = response.json()

    if data and len(data) > 0:
        latest_version = data[0]["version"]

        is_latest = is_latest_version(desired_version, latest_version)

        if is_latest:
            print(f"{package_name} {desired_version} is the latest version.")
        else:
            print(f"{package_name} {desired_version} is NOT the latest version.")
    else:
        print("Failed to retrieve package information.")
else:
    print(f"Failed to make the API request. Status code: {response.status_code}")

Running this script will output something like this:

GeneralUtils.NET 4.2.1 is the latest version.

Was this article helpful?