Retrieve docker image digest from ghcr.io

Is there any reliable way to retrieve docker image digest from ghcr api ?
I am using this endpoint https://ghcr.io/v2/IMAGE/manifests/TAG but sometimes, it returns a json containing image digest information like following example and sometimes there is no manifests field

{
   "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
   "schemaVersion": 2,
   "manifests": [
      {   
         "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
         "digest": "sha256:d7da3b5e843d0a8a713edec59fae7ecf6de06755e656c18aa6f3a73b84262cfb",
         "size": 1574,
         "platform": {
            "architecture": "amd64",
            "os": "linux"
         }
      },  
      {   
         "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
         "digest": "sha256:da90d7d694e42b46ea021ab6c26c23c39dd374638288ab8d7aadd522670d3c62",
         "size": 1574,
         "platform": {
            "architecture": "arm64",
            "os": "linux"
         }
      }   
   ]
...
}

get a token in settings page (https://github.com/settings/tokens),
then you could try

curl -i -u username:token https://ghcr.io/v2/IMAGE/manifests/TAG

The difference is that in your example there’s a multi-arch image, in which case the endpoint returns a list of manifests for the different architectures. That’s why the mediaType is application/vnd.docker.distribution.manifest.list.v2+json.

If the image is available only for one architecture you get the manifest for that architecture directly, with a mediaType of application/vnd.docker.distribution.manifest.v2+json. In that case you’ll get the manifest digest in the Docker-Content-Digest HTTP response header. Note however that you shouldn’t trust that over a locally known digest.

See also: