How to Pass Parameters to a Cloud Function

In my previous blog post I showed how to invoke an external REST API from a cloud function. The API that I used returns a random (Chuck Norris 💪) joke. In this blog post I want to show you to how pass a parameter to the cloud function. We can pass a joke number to the API and get that particular joke back 🤣.

Using the code from the previous blog post:

var request = require("request");

function main(params) {
   var options = {
      url: "https://api.icndb.com/jokes/random",
      json: true
   };

   return new Promise(function (resolve, reject) {
      request(options, function (err, resp) {
         if (err) {
            console.log(err);
            return reject({err: err});
         }
      return resolve({joke:resp.body.value.joke});
      });
   });
}

to get a particular joke number, change the URL to look like this:

url: "https://api.icndb.com/jokes/" + params.joke

params – passed to main function is a JSON object that holds parameters (input) to this cloud function.

To tests this cloud function with a parameter, click Change Input button above the code editor. There you can enter parameters in JSON form. For this example enter:

{"joke": "12"}

Click the Apply button.

Now click Invoke button to test this function.

If you entered 12 (you can enter any other joke number), the joke you should see is:

{
  "joke": "Chuck Norris sheds his skin twice a year."
}

Once you tested the function inside the console you can test it outside as well. With Web Actions option enabled, you can add ?joke=100 (or any other number) at the end of the URL and get this in the browser:

cloudfunctions-testing-param-browser
Testing a cloud function with a parameter

If you want to see all the available jokes and what numbers are available, enter this URL in the browser: https://api.icndb.com/jokes/.

If you want to make it a little bit more interesting, you can update the URL like this:

url: "https://api.icndb.com/jokes/" + (params.joke?params.joke:"random")

In this case if no joke number is provided, the function will return a random joke.

Okey, here is something else you can try. Turns out instead of a Chuck Norris name, you can pass a first name and last name to the API. Update the URL to like
this:

https://api.icndb.com/jokes/random?firstName="+params.first+"&lastName="+params.last,

Now click Change Input button and set the parameters:

{"first": "Arnold", "last": "Schwarzenegger"}

We switched the API to return a random joke so you should get a random joke with Arnold Schwarzenegger 🏋️‍♂️.

To test the function from the browser, add the following at the end of the URL:

?first=Arnold&last=Schwarzenegger

In this blog post I showed you how to pass parameters to a cloud function. I hope this was helpful. Let me know if there is anything else you would like to see in the comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s