Direct Triggers

To support quick iteration and debugging, Cloud Functions provides a call command in the CLI and a test function in the UI. This allows you to manually invoke a function to ensure it is behaving as expected. This causes the function to execute in the foreground even though it may have been deployed as a background function.

Calling HTTP Functions

If the function was deployed as an HTTP function, the value of the --data argument is sent as the HTTP POST body. The following gcloud command manually calls an HTTP function and sends it some data:

gcloud beta functions call helloHttp --data '{"name":"Keyboard Cat"}'

You can also use the commonly available cURL command line utility. The following cURL command makes a POST request with some data to the HTTP trigger URL of a deployed function:

curl -X POST https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/helloHttp -H "Content-Type:application/json" --data '{"name":"Keyboard Cat"}'

replacing <YOUR_REGION> with the region where your function is deployed and <YOUR_PROJECT_ID> with your Google Cloud Platform project ID.

Calling Background Functions

If your function was deployed as a background function, you can invoke it synchronously via the call command. The data argument passed to the call command will be set as the data property on the event object sent to the function. The following gcloud command manually calls a background function and sends it some data:

gcloud beta functions call helloBackground --data '{"name":"Keyboard Cat"}'

Node.js

/**
 * Background Cloud Function.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.helloBackground = function helloBackground (event, callback) {
  callback(null, `Hello ${event.data.name || 'World'}!`);
};

When calling background functions in this way you don't need to include the event envelope—we will add that for you. But you do need to ensure that the data being sent is what your function expects.

For example, in the case of background functions triggered from a Pub/Sub topic, you need to send a PubsubMessage, and functions with a Cloud Storage trigger should expect a Storage Object event.

Here is an example of how to directly call a Pub/Sub function:

DATA=$(printf 'Pubsub!'|base64) && gcloud beta functions call helloPubSub --data '{"data":"'$DATA'"}'

Node.js

/**
 * Background Cloud Function to be triggered by Pub/Sub.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.helloPubSub = function (event, callback) {
  const pubsubMessage = event.data;
  const name = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World';

  console.log(`Hello, ${name}!`);

  callback();
};

Send feedback about...

Cloud Functions Documentation