Catch up on everything announced at Firebase Summit, and learn how Firebase can help you accelerate app development and run your app with confidence. Learn More

Call functions via HTTP requests

Stay organized with collections Save and categorize content based on your preferences.

As in v1, you can trigger a function through an HTTP request with the onRequest() handler. This allows you to invoke a function through the following supported HTTP methods: GET, POST, PUT, DELETE, and OPTIONS.

Additional HTTP options

Option Description
region HTTP functions may specify an array of regions as well as a single region. When multiple regions are specified, a separate function instance will be deployed for each region.
timeoutSeconds HTTP functions may specify a timeout of up to one hour.
cors HTTP functions may specify CORS policies. You can set this to true to allow all origins or a string, regex, or array to specify allowed origins. Defaults to false/no CORS policies if not explicitly set.

Configuring CORS (Cross-Origin Resource Sharing)

Use the cors option to control which origins can access your function. By default, HTTP functions don't have CORS configured, meaning that any cross-origin request to your function results in this error:

request has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

You can also explicitly disable CORS by setting the cors option to false for your function.

To allow some cross-origin requests, but not all, you can pass a list of specific domains or regular expressions that should be allowed. For example, if you own the domains firebase.com and flutter.com, and firebase.com can have many subdomains, you might want to set the cors option to [/firebase\.com$/, 'flutter.com'].

If your function should be openly available, for example if it's serving a public API or website, set the cors policy to true.

Trigger a function with an HTTP request

Use onRequest() from the firebase-functions/v2/https subpackage to create a function that handles HTTP events. The onRequest() event supports routers and apps managed by the Express web framework.

Examples in this page are based on a time server sample 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.

Using Express request and response objects

Used as arguments for onRequest(), 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 = onRequest(
    {timeoutSeconds: 1200, region: ["us-west1", "us-east1"]},
    (req, res) => {
  // ...
});

Using existing Express apps

Using App as the argument for onRequest(), you can pass a full Express app to an HTTP function:

const { onRequest } = require('firebase-functions/v2/https');

const express = require('express');
const app = express();

// Add middleware to authenticate requests
app.use(myMiddleware);

// build multiple CRUD interfaces:
app.get('/:id', (req, res) => res.send(Widgets.getById(req.params.id)));
app.post('/', (req, res) => res.send(Widgets.create()));
app.put('/:id', (req, res) => res.send(Widgets.update(req.params.id, req.body)));
app.delete('/:id', (req, res) => res.send(Widgets.delete(req.params.id)));
app.get('/', (req, res) => res.send(Widgets.list()));

// Expose Express API as a single Cloud Function:
exports.widgets = onRequest(app);

Invoke an HTTP function

After you deploy an HTTP function, you can invoke it through its own unique URL. Use the exact URL output from the CLI after deployment.

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

https://date-<random-hash>-<region>.a.run.app

With Express app routing, the function name is added as a prefix to the URL paths in the app you define.

Read values from the request

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 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}`);
logger.log("Sending formatted date:", formattedDate);
res.status(200).send(formattedDate);

Integrating with 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.