HTTP Triggers

You can invoke Cloud Functions with an HTTP request using the POST, PUT, GET, DELETE, and OPTIONS HTTP methods. To create an HTTP endpoint for your function, specify --trigger-http as the trigger type when deploying your function. From the caller's perspective, HTTP invocations are synchronous, meaning that the result of the function execution will be returned in the response to the HTTP request.

For example, the following gcloud command deploys a function to the Node.js 6 runtime that will be triggered by HTTP requests:

gcloud functions deploy FUNCTION_NAME --trigger-http

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

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

where

  • YOUR_REGION is the region where your function is deployed,
  • YOUR_PROJECT_ID is your Google Cloud Platform project ID, and
  • FUNCTION_NAME is the name of the HTTP function you deployed.

The trigger URL for an HTTP function can be seen after the function is deployed, or queried at any time using the gcloud functions describe command.

You can also trigger your HTTP functions directly through the gcloud tool. The following command calls a function and sends it some data in the body of an HTTP POST request:

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

Code sample

Node.js

/**
 * HTTP Cloud Function.
 *
 * @param {Object} req Cloud Function request context.
 *                     More info: https://expressjs.com/en/api.html#req
 * @param {Object} res Cloud Function response context.
 *                     More info: https://expressjs.com/en/api.html#res
 */
exports.helloHttp = (req, res) => {
  res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
};

Python (Beta)

from flask import escape

def hello_http(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <http://flask.pocoo.org/docs/0.12/api/#flask.Request>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>.
    """
    request_json = request.get_json()
    if request_json and 'name' in request_json:
        name = escape(request_json['name'])
    else:
        name = 'World'
    return 'Hello, {}!'.format(name)

Event structure

HTTP-triggered events receive events via language-idiomatic HTTP frameworks. See the code sample above for more information.

Next steps

See the HTTP Tutorial for an example of how to implement an HTTP function, or HTTP Functions to learn more about writing HTTP functions.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation