26

How can I set some parameters on my HTTPS functions in Firebase? I am building an app, and while building the app, I have managed to grow my mailing list. Now I want to send mails out, but I want to make sure that they can unsubscribe before I send anything out.

I am using Firebase for everything, and I have managed to make a function that sends mails out to every subscribed mail.

I am also able to "unsubscribe" a specific mail, but that is hardcoded, and not at all an optimal solution.

exports.testUnsub = functions.https.onRequest((req, res) => {
  var db = admin.database();
  var ref = db.ref("mailingList/-KhBOisltrOmv57Mrzus");
  ref.child("subscribed").set(false);
  console.log("-KhBOisltrOmv57Mrzus has unsubscribed from mailing list.");
});

In the mail I send there is an URL, which triggers this HTTPS function. I want to set a parameter on that URL so it becomes dynamic. Something like:

https://us-central1-<project-id>.cloudfunctions.net/testUnsub?listID=xxxxxxxxxxx

I am looking for anything that can get me on the right direction.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jaafar Mahdi
  • 683
  • 1
  • 8
  • 15

1 Answers1

47

It's important to know that the req and res parameters to your https function are Express.js Request and Response objects.

The Request object contains all the data about the request coming from the client, including the query that the client sent in the URL. It will take the form req.query.name_of_the_parameter.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • But i get "Your client does not have permission to get URL" when i use `req.query.titl` – Mustafa Sep 23 '17 at 07:57
  • 3
    Doug is there any way with Cloud Functions to define and extract route parameters, similar to Express where you would do something like `app.get('/users/:userId/books/:bookId', function (req, res) { ... }`? – Pat Needham Jan 04 '18 at 16:11
  • 1
    @PatNeedham you can check this [github link](https://github.com/firebase/functions-samples/blob/master/authorized-https-endpoint/functions/index.js#L66) or [firebase doc](https://firebase.google.com/docs/functions/http-events#using_existing_express_apps) which utilizes the firebase functions as a normal express router. So that you can match with the endpoints in the ways you prefer. – peteroid Mar 22 '18 at 15:12
  • For those that don't understand http request format, do `https://us-central1-.cloudfunctions.net/testUnsub?exampleParam=x&anotherExampleParam=y`, and then use the `req.query.exampleParam` to access the contents. – Joe Moore Aug 16 '22 at 14:35
  • @JoeMoore The question from Pat is about route parameters like `/users/123/` whereas your answer is about `/?user=123`. Different types of parameters. – user823447 Apr 29 '23 at 05:59