Writing Cloud Functions

Google Cloud Functions are written in JavaScript, and execute in a Node.js runtime. When creating a Cloud Function, your function's source code must be exported in a Node.js module.

The Cloud Functions Node.js execution environment follows the Node "LTS" releases, starting with the v6 LTS release published on 2016-10-18. The current Node.js version running in Cloud Functions is Node v6.9.1.

Cloud Functions come in two distinct variants: foreground (HTTP) and background. You invoke HTTP functions from standard HTTP requests. These HTTP requests wait for the response and support handling of common HTTP request methods like GET, PUT, POST, DELETE and OPTIONS. When you use Cloud Functions, a TLS certificate is automatically provisioned for you, so all HTTP Functions can be invoked via a secure connection.

HTTP Function Example

Node.js

/**
 * HTTP Cloud Function.
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
exports.helloHttp = function helloHttp (req, res) {
  res.send(`Hello ${req.body.name || 'World'}!`);
};

You can use background functions to handle events from your Cloud infrastructure, such as messages on a Google Cloud Pub/Sub topic, or changes in a Google Cloud Storage bucket.

Background Function Example

Node.js

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

For details, see:

Send feedback about...

Cloud Functions Documentation