HTTP Triggers

You can trigger a Cloud Function through an HTTP request by using functions.https. This allows you to invoke a synchronous function through the following supported HTTP methods: GET, POST, PUT, DELETE, and OPTIONS.

Examples in this page are based on a sample function that triggers when you send an HTTP GET request to the Functions endpoint. The sample function retrieves the current server time, formats the time as specified in a URL query parameter, and sends the result in the HTTP response.

Trigger a Cloud Function with an HTTP request

Use functions.https to create a Cloud Function that handles HTTP events. The event handler for an HTTP function listens for the onRequest() event and supports two HTTP-specific arguments: request and response. These parameters are based off of the Express Request and Response objects, giving you access to their corresponding properties.

Invoke an HTTP Cloud Function

After an HTTP function is deployed, you can invoke it through its own unique URL. The URL includes the following, in order:

  • The region in which your Cloud Function is deployed
  • Your Firebase project ID
  • cloudfunctions.net
  • The name of your Cloud Function

For example, the URL to invoke date() looks like this:

https://us-central1-<project-id>.cloudfunctions.net/date

Use middleware modules with Cloud Functions

If you need to inject middleware dependencies for things like cookie support or CORS, call these within the function. For example, to enable CORS support, add the following block:

Read values from the request

The body of the request is automatically parsed based on the content-type and populated in the body of the request object. For example:

Content Type Request Body Behavior
application/json '{"name":"John"}' request.body.name equals 'John'
application/octet-stream 'my text' request.body equals '6d792074657874' (see Node.js Buffer docs)
text/plain 'my text' request.body equals 'my text'
application/x-www-form-urlencoded 'name=John' request.body.name equals 'John'

This parsing is done by the following body parsers:

Suppose your function is called with the following request:

curl -X POST -H "Content-Type:application/json" -H "X-MyHeader: 123" YOUR_HTTP_TRIGGER_ENDPOINT?foo=baz -d '{"text":"something"}'

then the sent data would be materialized under:

Property/Method Value
request.method "POST"
request.get('x-myheader') "123"
request.query.foo "baz"
request.body.text "something"

In the date() function example, the function tests both the URL parameter and the body for a format value to set the date/time format to use:

Terminate HTTP Functions

Always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might to continue to run and be forcibly terminated by the system. See also Sync, Async and Promises.

After retrieving and formatting the server time using the Node.js moment module, the date() function concludes by sending the result in the HTTP response:

Send feedback about...

Need help? Visit our support page.