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.

Continue reading “How to Pass Parameters to a Cloud Function”

How to Invoke an External REST API from a Cloud Function

In a previous blog post I showed how to create your first cloud function (plus a video). It’s very likely that your cloud function will need to invoke an external REST API. The following tutorial will show you how to create such function (it’s very easy).

  1. Sign into an IBM Cloud account
  2. Click Catalog
  3. Remove the label:lite filter and type functions
  4. Click on Functions box
  5. Click Start Creating button
  6. Click Create Action
  7. For Action Name enter ajoke and click the Create button.  A new cloud function will be created with Hello World message
  8. Replace the function code with the following code which invokes a 3rd party REST API which returns a random joke:
    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});
          });
       });
    }
    
    • The code is simple. It uses the request Node.js package to connect to an external REST API
    • The external REST API returns a random joke
    • A JavaScript Promise is used for invoking the REST API
    • At the end the cloud function returns a response in JSON format
  9. Now click the Save button to save the code. Once the code is saved the button will change to Invoke. Click the button to invoke the function. In the right-hand panel you should see output with a random joke:
    {
      "joke": "Project managers never ask Chuck Norris for estimations... ever."
    }
    

This is how it looks inside the IBM Cloud Functions editor:

cloudfunctions-invoke-restapi
Cloud function code

Of course you can also build and test a cloud function using the CLI. I’ll cover that in another blog post.

Continue reading “How to Invoke an External REST API from a Cloud Function”

Video: Build Your First Cloud Function

Last week I showed you how to build your first cloud function using IBM Cloud Functions. I also recorded a 5-minute video that shows how to build your first function and a number of ways to invoke it via a REST API. Check it out below and let me know what you think.

Build a Serverless “Hello World” Function

Serverless, Function as a Service (FaaS) or just cloud functions allows you to write code that will run in the cloud. You can use a number of different languages such as JavaScript (Node.js), Swift, Python, Java, PHP and others to write the function code.  What’s nice is that you don’t need to worry about servers, containers, deployment, etc. You write the code and a cloud platform will make sure it executes!

In this blog post you will learn how to build a Hello World function. You will use IBM Cloud Functions to build and run the function (more information about this at the end). For now, let’s jump to creating your first function.

Continue reading “Build a Serverless “Hello World” Function”