chetbaker
chetbaker

Reputation: 29

Dynamic callback endpoints with cloud functions

When a user triggers a function there’s a POST request going away to a partner. Within the body I need to include a unique endpoint callbackURL with an Id so they can send me status updates linked with a specific user. How can I accomplish that? I know how to setup static endpoints, but not create new ones for every request.

Upvotes: 0

Views: 346

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83181

As Doug said in his comment above, you don't need a new URL (i.e. a new endpoint) for each different id. You can deploy only one HTTP Cloud Function (which exposes one endpoint) and, in the Cloud Function, you extract the value of id from the Request object with its originalUrl property, as follows:

exports.myWebhook = functions.https.onRequest((req, res) => {

    const urlArray = req.originalUrl.split('/');

    console.log(urlArray);
    console.log(urlArray[1]);

    const id = urlArray[1]; 
    //Do whatever you need with id
    //.....

    //If you want to test that it works with a browser, you can send it back as a response to the browser
    res.send(urlArray[1]);

});

You then call this Cloud Function with the following URI:

https://us-central1-yourprojectname.cloudfunctions.net/myWebhook/id/callback


Note that it is also possible to extract values from the Request body, see https://firebase.google.com/docs/functions/http-events?authuser=0#read_values_from_the_request.

Upvotes: 1

Related Questions