Google Compute Engine: Node.js Client

release level npm version

Google Compute Engine Client Library for Node.js

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

Before you begin

  1. Select or create a Cloud Platform project.
  2. Enable billing for your project.
  3. Enable the Google Compute Engine API.
  4. Set up authentication with a service account so you can access the API from your local workstation.

Installing the client library

npm install @google-cloud/compute

Using the client library

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';

const compute = require('@google-cloud/compute');

// List all instances in the specified project.
async function listAllInstances() {
  const instancesClient = new compute.InstancesClient();

  //Use the `maxResults` parameter to limit the number of results that the API returns per response page.
  const aggListRequest = instancesClient.aggregatedListAsync({
    project: projectId,
    maxResults: 5,
  });

  console.log('Instances found:');

  // Despite using the `maxResults` parameter, you don't need to handle the pagination
  // yourself. The returned object handles pagination automatically,
  // requesting next pages as you iterate over the results.
  for await (const [zone, instancesObject] of aggListRequest) {
    const instances = instancesObject.instances;

    if (instances && instances.length > 0) {
      console.log(` ${zone}`);
      for (const instance of instances) {
        console.log(` - ${instance.name} (${instance.machineType})`);
      }
    }
  }
}

listAllInstances();

Samples

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

Sample Source Code Try it
Accelerator_types.aggregated_list source code Open in Cloud Shell
Accelerator_types.get source code Open in Cloud Shell
Accelerator_types.list source code Open in Cloud Shell
Addresses.aggregated_list source code Open in Cloud Shell
Addresses.delete source code Open in Cloud Shell
Addresses.get source code Open in Cloud Shell
Addresses.insert source code Open in Cloud Shell
Addresses.list source code Open in Cloud Shell
Addresses.set_labels source code Open in Cloud Shell
Autoscalers.aggregated_list source code Open in Cloud Shell
Autoscalers.delete source code Open in Cloud Shell
Autoscalers.get source code Open in Cloud Shell
Autoscalers.insert source code Open in Cloud Shell
Autoscalers.list source code Open in Cloud Shell
Autoscalers.patch source code Open in Cloud Shell
Autoscalers.update source code Open in Cloud Shell
Backend_buckets.add_signed_url_key source code Open in Cloud Shell
Backend_buckets.delete source code Open in Cloud Shell
Backend_buckets.delete_signed_url_key source code Open in Cloud Shell
Backend_buckets.get source code Open in Cloud Shell
Backend_buckets.insert source code Open in Cloud Shell
Backend_buckets.list source code Open in Cloud Shell
Backend_buckets.patch source code Open in Cloud Shell
Backend_buckets.set_edge_security_policy source code Open in Cloud Shell
Backend_buckets.update source code Open in Cloud Shell
Backend_services.add_signed_url_key source code Open in Cloud Shell
Backend_services.aggregated_list source code Open in Cloud Shell
Backend_services.delete source code Open in Cloud Shell
Backend_services.delete_signed_url_key source code Open in Cloud Shell
Backend_services.get source code Open in Cloud Shell
Backend_services.get_health source code Open in Cloud Shell
Backend_services.get_iam_policy source code Open in Cloud Shell
Backend_services.insert source code Open in Cloud Shell
Backend_services.list source code Open in Cloud Shell
Backend_services.patch source code Open in Cloud Shell
Backend_services.set_edge_security_policy source code Open in Cloud Shell
Backend_services.set_iam_policy source code Open in Cloud Shell
Backend_services.set_security_policy source code Open in Cloud Shell
Backend_services.update source code Open in Cloud Shell
Disk_types.aggregated_list source code Open in Cloud Shell
Disk_types.get source code Open in Cloud Shell
Disk_types.list source code Open in Cloud Shell
Disks.add_resource_policies source code Open in Cloud Shell
Disks.aggregated_list source code Open in Cloud Shell
Disks.create_snapshot source code Open in Cloud Shell
Disks.delete source code Open in Cloud Shell
Disks.get source code