Skip to content
This repository was archived by the owner on May 15, 2019. It is now read-only.

Accessing the REST API

Jason Dobry edited this page Jan 3, 2017 · 21 revisions

Table of Contents

The Emulator's REST API

The Emulator implements the Google Cloud Functions REST API as defined in https://cloudfunctions.googleapis.com/$discovery/rest?version=v1beta2. The Emulator exposes it's own discovery document at http://EMULATOR_HOST:EMULATOR_PORT/$discovery/rest?version=v1beta2.

When the Emulator starts, it's REST API listens on the host and port as configured in the restHost and restPort settings, which default to localhost and 8008, respectively. You can see the hostname on which the Emulator's REST API is listening by running:

functions status

which should print an entry like this:

├────────────────┼───────────────────────────┤
│ Rest Service   │ http://localhost:8008/    │
├────────────────┼───────────────────────────┤

Creating a client

Just as the Google APIs Node.js Client can be used to communicate with the production Google Cloud Functions REST API, it can be used to communicate with the Emulator's REST API.

Installing the Google APIs Node.js Client

Install the googleapis package:

npm install --save googleapis

Creating a client for the Emulator's REST API

Create a client for the Emulator's REST API:

const google = require('googleapis');
const DISCOVERY_URL = 'http://localhost:8008/$discovery/rest?version=v1beta2';

function buildService (callback) {
  google.discoverAPI(DISCOVERY_URL, (err, functionsService) => {
    if (err) {
      throw err;
    }

    callback(null, functionsService);
  });
}

Creating a client for the production Google Cloud Functions REST API

Create a client for the production Cloud Functions REST API:

const google = require('googleapis');
const DISCOVERY_URL = 'https://cloudfunctions.googleapis.com/$discovery/rest?version=v1beta2';

function buildService (callback) {
  // Obtain credentials. This requires the following environment variables:
  //   - GCLOUD_PROJECT
  //   - GOOGLE_APPLICATION_CREDENTIALS
  google.auth.getApplicationDefault((err, authClient) => {
    if (err) {
      throw err;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      authClient = authClient.createScoped([
        'https://www.googleapis.com/auth/cloud-platform'
      ]);
    }

    const options = {
      // Supply the credentials to the new service
      auth: authClient
    };

    google.discoverAPI(DISCOVERY_URL, options, (err, functionsService) => {
      if (err) {
        throw err;
      }

      callback(null, functionsService);
    });
  });
}

Making requests

Listing functions

The following lists deployed functions:

functionsService.projects.locations.functions.list({
  location: 'projects/YOUR_PROJECT_ID/locations/us-central1'
}, (err, result) => {
  console.log(err, result);
});

Deleting a function

The following deletes a function named helloWorld:

functionsService.projects.locations.functions.delete({
  name: 'projects/YOUR_PROJECT_ID/locations/us-central1/functions/helloWorld'
}, (err, result) => {
  console.log(err, result);
});
Clone this wiki locally