-
Notifications
You must be signed in to change notification settings - Fork 114
Accessing the REST API
Table of Contents
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/ │
├────────────────┼───────────────────────────┤
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.
Install the googleapis package:
npm install --save googleapis
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);
});
}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);
});
});
}The following lists deployed functions:
functionsService.projects.locations.functions.list({
location: 'projects/YOUR_PROJECT_ID/locations/us-central1'
}, (err, result) => {
console.log(err, result);
});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);
});Disclaimer: This is not an official Google product.
@google-cloud/functions-emulator is currently in pre-1.0.0 development. Before the 1.0.0 release, backwards compatible changes and bug fixes will bump the patch version number and breaking changes will bump the minor version number.