NuGet V3 - Find and Download a Package
While working on a side project, I've run into a need to discover the latest version of a given NuGet package and download it. Being a side project, I've decided rather that using NuGet v2 API to try out v3. I was hoping to find some documentation at the official site, but that didn't turn out to be as successful as I was expecting. All my attempts to figure it out on my own were failing and had to admit it felt nasty. After fiddling with it, trying to get some information on the interwebs, posting a question on SO, cursing at the dozens of NuGet packages required just to query, almost gave up. The hope came from Maarten Balliauw at MyGet. He suggested rather than going through something that is not quite an API and frankly way too complicated, to just go through the raw NuGet REST API.
NuGet v3 feed is a JSON file (f.e. the official NuGet v3 feed https://api.nuget.org/v3/index.json) containing all the operations you can perform and URLs you need to invoke to get those operations performed.
First, we need to search for the package by ID. SearchQueryService
@type
element will give us the URL to invoke for the search. That would be https://api-v2v3search-0.nuget.org/query in the case of the original NuGet v3 feed or https://api-v2v3search-1.nuget.org/query. To narrow down the scope to a particular package, package ID has to be provided. Assuming I'm interested in Newtonsoft.Json package, the search URL to use becomes https://api-v2v3search-0.nuget.org/query?q=packageid:newtonsoft.json.
Note: if you need to query pre-released packages as well, pass prerelease=true
in the query.
Next step is to find the PackageBaseAddress
@type
in the original feed and use its URL to download the required package version. Assuming, 8.0.3
is the version we're interested in. The download link would be https://api.nuget.org/v3-flatcontainer/newtonsoft.json/8.0.3/newtonsoft.json.8.0.3.nupkg
This is a direct link to Azure blob storage that contains the package.
Huge thank you to Maarten Balliauw who has helped me to get this information. The sample project is available here.