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

Calling functions

Jason Dobry edited this page Sep 6, 2017 · 9 revisions

Table of Contents

Calling HTTP functions

The Emulator CLI's call commands mirrors that of the Cloud SDK. To call a helloWorld HTTP function that's been deployed to the production Google Cloud Functions service you might run one of the following commands:

gcloud beta functions call helloWorld

or

curl https://us-central1-YOUR_PROJECT_ID.cloudfunctions.net/helloWorld

With the Emulator it's very similar:

functions call helloWorld

or

curl http://localhost:8010/YOUR_PROJECT_ID/local/helloWorld

To get help on the call command run:

functions call --help

To get the exact Trigger URL of a locally deployed helloWorld HTTP function, you would run the following:

functions describe helloWorld

To pass data to an HTTP function using the call command, use either the --data or --file flags:

On Linux/Mac:

functions call helloWorld --data='{"message":"Hello, World"}'

On Windows:

functions call helloWorld --data '{\"message\":\"Hello, World\"}'

or

functions call helloWorld --file=/path/to/data.json

If your HTTP function sends a response body then it will be printed to your terminal. If your function logs any data then you can view those logs with:

functions logs read

Calling background functions

To call a helloWorld background function that's been deployed to the production Google Cloud Functions service you might run the following command:

gcloud beta functions call helloWorld

With the Emulator it's very similar:

functions call helloWorld

To pass data to a background function using the call command, use either the --data or --file flags:

On Linux/Mac:

functions call helloWorld --data='{"message":"Hello, World"}'

On Windows:

functions call helloWorld --data '{\"message\":\"Hello, World\"}'

or

functions call helloWorld --file=/path/to/data.json

If your background function synchronously returns data, returns a promise that resolves with data, or passes data to the callback, then that data will be printed to your terminal. If your function logs any data then you can view those logs with:

functions logs read

Cloud Storage bucket trigger

You can manually simulate a Cloud Storage file when you call the function, for example:

On Linux/Mac:

functions call helloWorld --data='{"name":"test.txt","bucket":"my-bucket"}'

On Windows:

functions call helloWorld --data '{\"name\":\"test.txt\",\"bucket\":\"my-bucket\"}'

Cloud Pub/Sub topic trigger

You can manually simulate a Cloud Pub/Sub message when you call the function, for example:

On Linux/Mac:

functions call helloWorld --data='{"data":"Sm9obg=="}'

On Windows:

functions call helloWorld --data '{\"data\":\"Sm9obg==\"}'

where "Sm9obg==" is "John" encoded as a base64 string.