HTTP Triggers

You can trigger a 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 function with an HTTP request

Use functions.https to create a 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 Request and Response objects managed by the Express web framework. The Request object gives you access to the properties of the HTTP request sent by the client, and the Response object gives you a way to send a response back to the client.

exports.date = functions.https.onRequest((req, res) => {
  // ...
});

Invoke an HTTP 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 function is deployed
  • Your Firebase project ID
  • cloudfunctions.net
  • The name of your 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:

// Enable CORS using the `cors` express middleware.
cors(req, res, () => {
  // ...
});

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:

let format = req.query.format;
format = req.body.format;

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:

const formattedDate = moment().format(format);
console.log('Sending Formatted date:', formattedDate);
res.status(200).send(formattedDate);

Connecting HTTP Functions to Firebase Hosting

You can connect an HTTP function to Firebase Hosting. Requests on your Firebase Hosting site can be proxied to specific HTTP functions. This also allows you to use your own custom domain with an HTTP function. Learn more about connecting Cloud Functions to Firebase Hosting.

Send feedback about...

Need help? Visit our support page.