List subscriptions (v1)

List all Analytics Hub subscriptions in a project and location.

Code sample

Node.js

Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

'use strict';

const {AnalyticsHubServiceClient} = require('@google-cloud/bigquery-analyticshub');
const {status} = require('@grpc/grpc-js');

const client = new AnalyticsHubServiceClient();

/**
 * Lists subscriptions in a Google Cloud project and location.
 */
async function listSubscriptions() {
  try {
    const projectId = await client.getProjectId();
    const location = 'us';
    const parent = client.locationPath(projectId, location);

    const request = {
      parent,
    };

    const [subscriptions] = await client.listSubscriptions(request);
    console.log(`Retrieved ${subscriptions.length} subscription(s).`);
    for (const subscription of subscriptions) {
      console.log(`Subscription: ${subscription.name}`);
      console.log(`State: ${subscription.state}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument: ${err.message}`);
    } else {
      console.error(`API error: ${err.message}`);
    }
  }
}

module.exports = { listSubscriptions };

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.