skip to content

In NuGet's v3 server API, what is the service index, and why must clients look up resource URLs there instead of hard-coding them?

level: middleimportance: nice to knowfreq 26%

answer

  1. one JSON entry point
  2. resources keyed by @type
  3. flat container, lowercase paths
  4. base URLs may move
  5. an off-host @id breaks isolation

basics

~20 s

The service index is the JSON entry point of a NuGet v3 package source: a version plus a resources list mapping each @type, such as PackageBaseAddress/3.0.0, to an @id URL. Clients look URLs up there because a source may move them.

solid answer

~40 s

A NuGet v3 package source is identified by its service index URL; nuget.org's is `https://api.nuget.org/v3/index.json`. The document has a `version` whose major number must be 3 and a `resources` array, and each resource has an `@id` URL, a `@type` such as `PackageBaseAddress/3.0.0`, `RegistrationsBaseUrl/3.6.0`, `SearchQueryService` or `PackagePublish/2.0.0`, and an optional `comment`. Clients hard-code only the `@type` strings they understand and read the URLs from the document, because the API says base URLs may change with the source's implementation or infrastructure. From the `PackageBaseAddress` URL a client builds `{@id}/{LOWER_ID}/index.json` to list versions and `{@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.{LOWER_VERSION}.nupkg` to download, all lowercased. A resource may live on another host, which is exactly how an internal feed breaks on a network that cannot reach that host.

code

json · 21 lines
json
{
  "version": "3.0.0",
  "resources": [
    {
      "@id": "https://nuget.internal.example.com/v3/flatcontainer/",
      "@type": "PackageBaseAddress/3.0.0"
    },
    {
      "@id": "https://nuget.internal.example.com/v3/registration/",
      "@type": "RegistrationsBaseUrl/3.6.0"
    },
    {
      "@id": "https://nuget.internal.example.com/v3/query",
      "@type": "SearchQueryService"
    },
    {
      "@id": "https://nuget.internal.example.com/api/v2/package",
      "@type": "PackagePublish/2.0.0"
    }
  ]
}

go deeper

for a junior

Recall that a NuGet v3 source is addressed by its service index URL, a JSON document listing resources by @type and @id.

for a middle

Explain how a client finds PackageBaseAddress in the service index and builds lowercase version-list and .nupkg URLs from it.

for a senior

When a feed restores on one network but not another, read its service index and check whether any resource @id points at a host that network cannot reach.

for a principal

Insist that internal feeds serve every resource from hosts the build network can reach, since an index that delegates to the public gallery quietly makes every build depend on it.

## Why NuGet has a service index A Maven repository is addressed by one path rule. NuGet's v3 server API instead splits a package source into separate **resources** (package download, package metadata, search, publishing), each of which may live at its own URL, even on its own host. A client needs one place to find them, and that place is the **service index**: a JSON document at a well-known URL that *is* the package source's address. nuget.org's is `https://api.nuget.org/v3/index.json`; an internal feed's is whatever URL its repository manager gives it. The service index answers `GET` and `HEAD`. ## Anatomy of the document The service index is a JSON object with two required properties: - **`version`**: the schema version of the index. Its major number must be `3`; the current schema is `3.0.0`. - **`resources`**: an array of resource objects, each with: - **`@id`**: an absolute HTTP or HTTPS URL for the resource; - **`@type`**: a string, usually `{RESOURCE_NAME}/{RESOURCE_VERSION}`, saying which protocol the resource speaks; - **`comment`**: optional, human-readable. Clients **hard-code the `@type` values they understand** and look them up; they never hard-code the URLs. Each resource is versioned on its own, and the same type may appear more than once, in which case the client chooses, for example round-robin on failure. The API overview marks four resources as required: | Resource | Required | Purpose | |---|---|---| | `PackageBaseAddress` | yes | package content (`.nupkg`), the "flat container" | | `PackagePublish` | yes | push, and delete or unlist | | `RegistrationsBaseUrl` | yes | package metadata ("registrations") | | `SearchQueryService` | yes | search by keyword | | `Catalog`, `SearchAutocompleteService`, `RepositorySignatures`, `VulnerabilityInfo` and others | no | optional capabilities | ## The flat container: versions and downloads The `PackageBaseAddress/3.0.0` resource is what a restore actually downloads from. Its `@id` is the base, and two URLs are built from it: 1. **List versions**: `GET {@id}/{LOWER_ID}/index.json` returns `{"versions": [...]}`, every version the source holds, **listed and unlisted**, as lowercased normalized version strings. It returns `404` when the source has no versions of that ID. 2. **Download**: `GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.{LOWER_VERSION}.nupkg`. Both the ID and the version are **lowercased** (invariant-culture rules), and the version is normalized, so `Billing.Core` 2.0.1 is fetched from `{@id}/billing.core/2.0.1/billing.core.2.0.1.nupkg`. The API says the base URL "must be dynamically fetched from the service index", so a client that hard-codes a flat-container path written down from one server is relying on something the API does not promise. ## Registrations: the metadata side `RegistrationsBaseUrl` serves JSON "registration" documents per package ID: each version's `catalogEntry` (`description`, `dependencyGroups`, a `listed` flag that counts as true when absent) and a `packageContent` URL pointing at the `.nupkg`. Its `@type` versions matter: `RegistrationsBaseUrl/3.4.0` serves gzipped responses, and only `RegistrationsBaseUrl/3.6.0` includes SemVer 2.0.0 packages, so a client reading an older registration hive does not see them. ## When the index points somewhere else Because `@id` is just a URL, a resource can sit on a different host from the index. The NuGet docs warn about the obvious failure: - An internal feed whose service index lists resources pointing **directly at nuget.org**, instead of proxying them, does not work where nuget.org is blocked. - On such a network the index itself loads, because it lives on the internal host, and then downloads or metadata requests fail against the unreachable host. - The documented advice is to make such delegation configurable so the direct nuget.org entries can be removed when the feed is deployed. How a proxy caches what it fetches from nuget.org is a proxy concern; what this leaf owns is that the service index decides where clients go. ## Debugging a feed from its service index 1. `GET` the service index and write down the host of every `@id`; each one must be reachable from the build network. 2. Take the `PackageBaseAddress` `@id` and request `{@id}/{LOWER_ID}/index.json` for the failing package; a `404` means the source holds no version of that ID. 3. Request the `.nupkg` URL for the exact version and confirm it downloads. 4. If a SemVer 2.0.0 version is missing only from metadata, check which `RegistrationsBaseUrl` type is being read. ## Delete, unlist and what the server decides `PackagePublish` accepts delete requests, but the meaning is the server's: nuget.org treats a delete as an **unlist** (the package stays available to existing consumers but leaves search results), and other implementations may hard-delete, soft-delete or unlist, some by configuration. An unlisted version still appears in the flat container's version list.

  • Why does a NuGet flat container's version list include versions that search does not show?
    `{@id}/{LOWER_ID}/index.json` lists every version the source can serve, listed and unlisted, and every version in it must be downloadable. Search hides unlisted versions, but a build that names an unlisted version exactly can still restore it from the flat container.
  • What changes between RegistrationsBaseUrl/3.4.0 and RegistrationsBaseUrl/3.6.0 in a NuGet service index?
    Both serve registration metadata. `3.4.0` added gzipped responses; `3.6.0` is the only one of the three registration types whose hive includes SemVer 2.0.0 packages. A client reading an older registration type does not see packages with SemVer 2.0.0 versions, which looks like missing packages when it is really the wrong hive.

The service index is like the directory board in an office lobby: visitors read which floor each department is on today instead of memorising it, so departments can move. If the board sends you to a building across town that you are not allowed to enter, the visit still fails, even though you got into the lobby.

  • The flat container path is fixed, so clients can hard-code it.
  • Package IDs keep their original letter case in flat container paths.
  • Every resource in a service index must live on the index's own host.
  • A delete sent to any NuGet server permanently removes the .nupkg.
  • Unlisted versions disappear from the flat container's version list.