Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including gcp-metadata with all npm packages installed. Try it out:

var gcpMetadata = require("gcp-metadata")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

gcp-metadata v4.1.0

Get the metadata from a Google Cloud Platform environment


Get the metadata from a Google Cloud Platform environment.

NPM Version codecov

$ npm install --save gcp-metadata
const gcpMetadata = require('gcp-metadata');

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

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


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free