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”

Learn How to Convert Audio to Text

Converting speech or audio to text has a large number of applications and can bring advanced capabilities to applications.

Image you are running a call center with thousands of simultaneous calls. You would like to identify some trends/analytics such as if the callers are having problems with a particular product or feature. Or if the callers sound frustrated or unhappy about something.

You might also be looking for particular words in the conversation that are being repeated and also need to know the frequency. Being able to analyze such information is vital to businesses. For example, if you identified that callers sound frustrated and the word “broken” is repeated all the time – you can take actions to improve the user experience. First, you can quickly teach the support team how to help with this particular problem, offer a solution or a workaround. Second, you can fix or improve the product.

In general, almost any audio can be converted to text, where the text is then analyzed for trends, analytics that are important to you. One tool that you can use to analyze text is the Watson Tone Analyzer service.

Continue reading “Learn How to Convert Audio to Text”

How to Invoke the Watson Language Translator API from an Appery.io App

In this blog post you will learn how to invoke the Watson Language Translator API from an Appery.io app. Before you will learn how to do that, let me give you a quick overview of the Appery.io platform.

Appery.io is a cloud platform for building and running mobile web and hybrid apps. The platform provides the following major benefits:

  • A visual, drag and drop UI builder (based on jQuery Mobile, Ionic or Bootstrap)
  • A cloud database for storing app data.
  • Node.js-like environment (called Server Code) for running any custom app code (written in JavaScript).
  • API Express for connecting to any external/enterprise data sources such as a SQL database, web service or a REST API.
  • Sending Push Notifications.

If you are looking for more Appery.io content, simply look for posts tagged with Appery.io.

Appery.io makes it very easy to invoke any 3rd party REST APIs using Server Code or API Express. In this blog post you will learn how to invoke the Watson Language Translator API from Server Code. Let’s start.

Watson Language Translator API

In this section you will learn how to start with the IBM Cloud and launch the Watson Language Translator API. The Language Translator API allows to translate one language to another language. Other API features include detecting the what language was entered. To learn more about the API here.

Continue reading “How to Invoke the Watson Language Translator API from an Appery.io App”

Using Geolocation in a Mobile App: A Video Guide

Geolocation is one of the most widely used APIs in mobile apps today. From weather to Uber and Lyft, the device location is crucial information and many APIs use it to determine what services and products are available in a given location. When building a mobile app in Appery.io, using the Geolocation API is simple. Apache Cordova library is built-in in every new Appery.io app. Invoking Geolocation will return the latitude, longitude (and other) information which can then be passed to any REST API. The following two short videos show how to use and invoke Geolocation in an Ionic and jQuery Mobile apps:

Looking for more videos like that? Check Appery.io YouTube channel for many more videos.

Video Tutorial: How to Build and Test a Complete Mobile App with API Backend

This video shows how to build and test a complete Ionic app with an API backend. The tutorial starts by showing how to create an app backend with API Express by connecting to an external REST API. Then, the tutorial shows how to build an Ionic app and import the backend service into the app. The tutorial also shows how to make a small change to the generated code. The tutorial then shows how to test the app in the browser and also on a device using the Appery.io Tester app. Lastly, the tutorial shows where to build a binary file for iOS or Android for submission to an app store.

Check out other videos on how to build mobile apps fast.

Learn How to Perform Basic Database Operations: Create, Read, Update, and Delete

The Appery.io Backend Services consists of the following components:

  • Database – for storing any app data.
  • Server Code – for writing server-side app logic using JavaScript.
  • API Express – integrating with external systems and APIs.
  • Push Notifications – sending push messages to devices.
  • Web Hosting – publishing and hosting mobile web apps.

In this post, we will show you how a Server Code script integrates with the Database.

One of the most common questions we get is how to work with the database, how to create, edit and delete data. This makes sense as virtually any mobile app needs to store data in a database and perform these basic operations. To access the database, we are going to use Server Code script. Server Code allows writing app logic using JavaScript which will be executed on the server. For example, a script can access the database, send a Push Notification message, sort data and invoke an external REST API. We will start with the most basic operation: how to read data from the database.

The database collection used in this post looks like this:

crud_collection
Database collection.

This can be data collected from a form inside an app – a form that collects information about the user.

Reading Data

Server Code script has out-of-the-box API to access the database to perform all the basic operations. The following script reads all objects from the above collection.

var dbApiKey = "fe7c124b-f7c5-4764-9274-173b56a97102";
var result = Collection.query(dbApiKey, "Data");
Apperyio.response.success(result, "application/json");

One line 2, the script retrieves all the data.

One line 3, the script response is set. A Server Code script is invoked as a REST API. The code on this line defines the API response.

A script can be quickly tested from the Run tab where you can see the JSON data:

 

crud_test
Script testing.

 

Reading data from a database collection is fast and simple. Next, we are going to save data into the database.

Continue reading “Learn How to Perform Basic Database Operations: Create, Read, Update, and Delete”

Video Tutorial: How to Build a REST API for a Custom SQL Query

This video shows how to use the SQL and Script components to build a Login service. The SQL component runs a custom SQL query to check if a user exists in a database. The Script component adds custom logic to check what the SQL component returned and defines the response (user found or user not found).

Looking for more videos on enterprise integration? Watch the API Express playlist.