Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

gcp-metadata

googleapis56.4mApache-2.06.1.1TypeScript support: included

Get the metadata from a Google Cloud Platform environment

google cloud platform, google cloud, google, app engine, compute engine, metadata server, metadata

readme

Google Cloud Platform logo

GCP Metadata: Node.js Client

release level npm version

Get the metadata from a Google Cloud Platform environment

A comprehensive list of changes in each version may be found in the CHANGELOG.

Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.

Table of contents:

Quickstart

Installing the client library

npm install gcp-metadata

Using the client library

const gcpMetadata = require('gcp-metadata');

async function quickstart() {
  // check to see if this code can access a metadata server
  const isAvailable = await gcpMetadata.isAvailable();
  console.log(`Is available: ${isAvailable}`);

  // Instance and Project level metadata will only be available if
  // running inside of a Google Cloud compute environment such as
  // Cloud Functions, App Engine, Kubernetes Engine, or Compute Engine.
  // To learn more about the differences between instance and project
  // level metadata, see:
  // https://cloud.google.com/compute/docs/storing-retrieving-metadata#project-instance-metadata
  if (isAvailable) {
    // grab all top level metadata from the service
    const instanceMetadata = await gcpMetadata.instance();
    console.log('Instance metadata:');
    console.log(instanceMetadata);

    // get all project level metadata
    const projectMetadata = await gcpMetadata.project();
    console.log('Project metadata:');
    console.log(projectMetadata);
  }
}

quickstart();

Check to see if the metadata server is available

const isAvailable = await gcpMetadata.isAvailable();

Access all metadata

const data = await gcpMetadata.instance();
console.log(data); // ... All metadata properties

Access specific properties

const data = await gcpMetadata.instance('hostname');
console.log(data); // ...Instance hostname
const projectId = await gcpMetadata.project('project-id');
console.log(projectId); // ...Project ID of the running instance

Access nested properties with the relative path

const data = await gcpMetadata.instance('service-accounts/default/email');
console.log(data); // ...Email address of the Compute identity service account

Access specific properties with query parameters

const data = await gcpMetadata.instance({
  property: 'tags',
  params: { alt: 'text' }
});
console.log(data) // ...Tags as newline-delimited list

Access with custom headers

await gcpMetadata.instance({
  headers: { 'no-trace': '1' }
}); // ...Request is untraced

Take care with large number valued properties

In some cases number valued properties returned by the Metadata Service may be too large to be representable as JavaScript numbers. In such cases we return those values as BigNumber objects (from the bignumber.js library). Numbers that fit within the JavaScript number range will be returned as normal number values.

const id = await gcpMetadata.instance('id');
console.log(id)  // ... BigNumber { s: 1, e: 18, c: [ 45200, 31799277581759 ] }
console.log(id.toString()) // ... 4520031799277581759

Environment variables

  • GCE_METADATA_HOST: provide an alternate host or IP to perform lookup against (useful, for example, you're connecting through a custom proxy server).

    For example:

    export GCE_METADATA_HOST='169.254.169.254'
  • DETECT_GCP_RETRIES: number representing number of retries that should be attempted on metadata lookup.

  • DEBUG_AUTH: emit debugging logs

  • METADATA_SERVER_DETECTION: configure desired metadata server availability check behavior.

    • assume-present: don't try to ping the metadata server, but assume it's present
    • none: don't try to ping the metadata server, but don't try to use it either
    • bios-only: treat the result of a BIOS probe as canonical (don't fall back to pinging)
    • ping-only: skip the BIOS probe, and go straight to pinging

Samples

Samples are in the samples/ directory. Each sample's README.md has instructions for running its sample.

Sample Source Code Try it
Quickstart source code Open in Cloud Shell

The GCP Metadata Node.js Client API Reference documentation also contains samples.

Supported Node.js Versions

Our client libraries follow the Node.js release schedule. Libraries are compatible with all current active and maintenance versions of Node.js. If you are using an end-of-life version of Node.js, we recommend that you update as soon as possible to an actively supported LTS version.

Google's client libraries support legacy versions of Node.js runtimes on a best-efforts basis with the following warnings:

  • Legacy versions are not tested in continuous integration.
  • Some security patches and features cannot be backported.
  • Dependencies cannot be kept up-to-date.

Client libraries targeting some end-of-life versions of Node.js are available, and can be installed through npm dist-tags. The dist-tags follow the naming convention legacy-(version). For example, npm install gcp-metadata@legacy-8 installs client libraries for versions compatible with Node.js 8.

Versioning

This library follows Semantic Versioning.

This library is considered to be stable. The code surface will not change in backwards-incompatible ways unless absolutely necessary (e.g. because of critical security issues) or with an extensive deprecation period. Issues and requests against stable libraries are addressed with the highest priority.

More Information: Google Cloud Platform Launch Stages

Contributing

Contributions welcome! See the Contributing Guide.

Please note that this README.md, the samples/README.md, and a variety of configuration files in this repository (including .nycrc and tsconfig.json) are generated from a central template. To edit one of these files, make an edit to its templates in directory.

License

Apache Version 2.0

See LICENSE

changelog

Changelog

npm history

6.1.1 (2025-01-30)

Bug Fixes

6.1.0 (2023-11-10)

Features

Bug Fixes

6.0.0 (2023-07-17)

⚠ BREAKING CHANGES

  • upgrade to Node 14, and update gaxios, ts, and gts (#571)

Miscellaneous Chores

  • Upgrade to Node 14, and update gaxios, ts, and gts (#571) (88ff3ff)

5.3.0 (2023-06-28)

Features

  • Metadata Server Detection Configuration (#562) (8c7c715)

5.2.0 (2023-01-03)

Features

5.1.0 (2022-12-07)

Features

  • Extend GCP Residency Detection Support (#528) (2b35bb0)

5.0.1 (2022-09-09)

Bug Fixes

5.0.0 (2022-04-22)

⚠ BREAKING CHANGES

  • drop node 10, update typescript to 4.6.3 (#519)

Build System

  • drop node 10, update typescript to 4.6.3 (#519) (688749b)

4.3.1 (2021-09-02)

Bug Fixes

  • build: switch primary branch to main (#481) (8a7965c)

4.3.0 (2021-06-10)

Features

  • add gcf-owl-bot[bot] to ignoreAuthors (#450) (6a0f9ad)

4.2.1 (2020-10-29)

Bug Fixes

  • deps: update dependency gaxios to v4 (#420) (b99fb07)

4.2.0 (2020-09-15)

Features

  • add support for GCE_METADATA_HOST environment variable (#406) (eaf128a)

4.1.4 (2020-07-15)

Bug Fixes

  • deps: update dependency json-bigint to v1 (#382) (ab4d8c3)

4.1.3 (2020-07-13)

Bug Fixes

  • deps: update dependency json-bigint to ^0.4.0 (#378) (b214280)

4.1.2 (2020-07-10)

Bug Fixes

  • deps: roll back dependency gcp-metadata to ^4.1.0 (#373) (a45adef)

4.1.1 (2020-07-09)

Bug Fixes

4.1.0 (2020-05-05)

Features

  • Introduces the GCE_METADATA_IP to allow using a different IP address for the GCE metadata server. (#346) (ec0f82d)

Bug Fixes

  • do not check secondary host if GCE_METADATA_IP set (#352) (64fa7d6)
  • warn rather than throwing when we fail to connect to metadata server (#351) (754a6c0)

4.0.1 (2020-04-14)

Bug Fixes

4.0.0 (2020-03-19)

⚠ BREAKING CHANGES

  • typescript@3.7.x has breaking changes; compiler now targets es2015
  • drops Node 8 from engines field (#315)

Features

Build System

3.5.0 (2020-03-03)

Features

  • add ECONNREFUSED to list of known errors for isAvailable() (#309) (17ff6ea)

3.4.0 (2020-02-24)

Features

  • significantly increase timeout if GCF environment detected (#300) (8e507c6)

3.3.1 (2020-01-30)

Bug Fixes

  • isAvailable: handle EHOSTDOWN and EHOSTUNREACH error codes (#291) (ba8d9f5)

3.3.0 (2019-12-16)

Features

  • add environment variable for configuring environment detection (#275) (580cfa4)
  • cache response from isAvailable() method (#274) (a05e13f)

Bug Fixes

  • fastFailMetadataRequest should not reject, if response already happened (#273) (a6590c4)

3.2.3 (2019-12-12)

Bug Fixes

  • deps: pin TypeScript below 3.7.0 (e4bf622)

3.2.2 (2019-11-13)

Bug Fixes

3.2.1 (2019-11-08)

Bug Fixes

3.2.0 (2019-10-10)

Features

  • add DEBUG_AUTH for digging into authentication issues (#254) (804156d)

3.1.0 (2019-10-07)

Features

3.0.0 (2019-09-17)

⚠ BREAKING CHANGES

  • isAvailable now tries both DNS and IP, choosing whichever responds first (#239)

Features

  • isAvailable now tries both DNS and IP, choosing whichever responds first (#239) (25bc116)

2.0.4 (2019-09-13)

Bug Fixes

  • IP address takes 15 seconds to timeout, vs., metadata returning immediately (#235) (d04207b)
  • use 3s timeout rather than 15 default (#237) (231ca5c)

2.0.3 (2019-09-12)

Bug Fixes

2.0.2 (2019-08-26)

Bug Fixes

  • allow calls with no request, add JSON proto (#224) (dc758b1)

2.0.1 (2019-06-26)

Bug Fixes

2.0.0 (2019-05-07)

Bug Fixes

  • deps: update dependency gaxios to v2 (#191) (ac8c1ef)

Build System

BREAKING CHANGES

  • upgrade engines field to >=8.10.0 (#194)

v1.0.0

02-14-2019 16:00 PST

Bug Fixes

  • fix: ask gaxios for text and not json (#152)

Documentation

  • docs: update links in contrib guide (#168)
  • docs: add lint/fix example to contributing guide (#160)

Internal / Testing Changes

  • build: use linkinator for docs test (#166)
  • chore(deps): update dependency @types/tmp to v0.0.34 (#167)
  • build: create docs test npm scripts (#165)
  • test: run system tests on GCB (#157)
  • build: test using @grpc/grpc-js in CI (#164)
  • chore: move CONTRIBUTING.md to root (#162)
  • chore(deps): update dependency gcx to v0.1.1 (#159)
  • chore(deps): update dependency gcx to v0.1.0 (#158)
  • chore(deps): update dependency gcx to v0.0.4 (#155)
  • chore(deps): update dependency googleapis to v37 (#156)
  • build: ignore googleapis.com in doc link check (#153)
  • build: check broken links in generated docs (#149)
  • chore(build): inject yoshi automation key (#148)

v0.9.3

12-10-2018 16:16 PST

Dependencies

  • chore(deps): update dependency googleapis to v36 (#135)
  • chore(deps): use gaxios for http requests (#121)
  • chore(deps): update dependency gts to ^0.9.0 (#123)

Internal / Testing Changes

  • fix(build): fix Kokoro release script (#141)
  • Release v0.9.2 (#140)
  • build: add Kokoro configs for autorelease (#138)
  • Release gcp-metadata v0.9.1 (#139)
  • chore: always nyc report before calling codecov (#134)
  • chore: nyc ignore build/test by default (#133)
  • Sync repo build files (#131)
  • fix(build): fix system key decryption (#128)
  • refactor: use execa, move post install test to system (#127)
  • chore: add a synth.metadata
  • test: add a system test (#126)
  • chore: update eslintignore config (#122)
  • chore: use latest npm on Windows (#120)
  • chore: update CircleCI config (#119)
  • chore: include build in eslintignore (#115)

v0.9.2

12-10-2018 14:01 PST

  • chore(deps): update dependency googleapis to v36 (#135)
  • chore: always nyc report before calling codecov (#134)
  • chore: nyc ignore build/test by default (#133)
  • chore: Re-generated to pick up changes in the API or client library generator. (#131)
  • fix(build): fix system key decryption (#128)
  • chore(deps): use gaxios for http requests (#121)
  • refactor: use execa, move post install test to system (#127)
  • chore: add a synth.metadata
  • test: add a system test (#126)
  • chore(deps): update dependency gts to ^0.9.0 (#123)
  • chore: update eslintignore config (#122)
  • chore: use latest npm on Windows (#120)
  • chore: update CircleCI config (#119)
  • chore: include build in eslintignore (#115)
  • build: add Kokoro configs for autorelease (#138)

v0.9.1

12-10-2018 11:53 PST

  • chore(deps): update dependency googleapis to v36 (#135)
  • chore: always nyc report before calling codecov (#134)
  • chore: nyc ignore build/test by default (#133)
  • chore: Re-generated to pick up changes in the API or client library generator. (#131)
  • fix(build): fix system key decryption (#128)
  • chore(deps): use gaxios for http requests (#121)
  • refactor: use execa, move post install test to system (#127)
  • chore: add a synth.metadata
  • test: add a system test (#126)
  • chore(deps): update dependency gts to ^0.9.0 (#123)
  • chore: update eslintignore config (#122)
  • chore: use latest npm on Windows (#120)
  • chore: update CircleCI config (#119)
  • chore: include build in eslintignore (#115)

v0.9.0

10-26-2018 13:10 PDT

  • feat: allow custom headers (#109)
  • chore: update issue templates (#108)
  • chore: remove old issue template (#106)
  • build: run tests on node11 (#105)
  • chores(build): do not collect sponge.xml from windows builds (#104)
  • chores(build): run codecov on continuous builds (#102)
  • chore(deps): update dependency nock to v10 (#103)
  • chore: update new issue template (#101)
  • build: fix codecov uploading on Kokoro (#97)
  • Update kokoro config (#95)
  • Update CI config (#93)
  • Update kokoro config (#91)
  • Re-generate library using /synth.py (#90)
  • test: remove appveyor config (#89)
  • Update kokoro config (#88)
  • Enable prefer-const in the eslint config (#87)
  • Enable no-var in eslint (#86)

New Features

A new option, headers, has been added to allow metadata queries to be sent with custom headers.

v0.8.0

This release has breaking changes. Please take care when upgrading to the latest version.

Dropped support for Node.js 4.x and 9.x

This library is no longer tested against versions 4.x and 9.x of Node.js. Please upgrade to the latest supported LTS version!

Return type of instance() and project() has changed

The instance() and project() methods are much more selective about which properties they will accept.

The only accepted properties are params and properties. The instance() and project() methods also now directly return the data instead of a response object.

Changes in how large number valued properties are handled

Previously large number-valued properties were being silently losing precision when returned by this library (as a number). In the cases where a number valued property returned by the metadata service is too large to represent as a JavaScript number, we will now return the value as a BigNumber (from the bignumber.js) library. Numbers that do fit into the JavaScript number range will continue to be returned as numbers. For more details see #74.

Breaking Changes

  • chore: drop support for node.js 4 and 9 (#68)
  • fix: quarantine axios config (#62)

Implementation Changes

  • fix: properly handle large numbers in responses (#74)

Dependencies

  • chore(deps): update dependency pify to v4 (#73)

Internal / Testing Changes

  • Move to the new github org (#84)
  • Update CI config (#83)
  • Retry npm install in CI (#81)
  • Update CI config (#79)
  • chore(deps): update dependency nyc to v13 (#77)
  • add key for system tests
  • increase kitchen test timeout
  • add a lint npm script
  • update npm scripts
  • add a synth file and run it (#75)
  • chore(deps): update dependency assert-rejects to v1 (#72)
  • chore: ignore package-log.json (#71)
  • chore: update renovate config (#70)
  • test: throw on deprecation
  • chore(deps): update dependency typescript to v3 (#67)
  • chore: make it OSPO compliant (#66)
  • chore(deps): update dependency gts to ^0.8.0 (#65)