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”

Supercharge Your APIs with OpenAPI: Lunch and Learn – Video Recording

Last week the IBM Developer Advocates team hosted another Lunch-and-Learn event. Erin McKean covered the OpenAPI spec (formerly known as Swagger), what it is, and how using the spec makes it easier to plan, create, test, and share your APIs?
If you missed the event, you can watch the live-streamed version below.  The session covered:
  • What is the OpenAPI spec?
  • How can you create an OpenAPI description for your new or existing API?
  • What can you do with your OpenAPI description—including automated testing, code generation, creating interactive documentation, and more!
  • How you can get involved with the spec and how to share the benefits of OpenAPI with your team

Part 1:

Part 2:

Looking for more events like this – check out our upcoming events in San Francisco and the South Bay.

How to Send a Fax with Appery.io and Twilio Fax API

Twilio launched a new API that allows sending faxes. From the very start Appery.io platform made it very simple to connect to any external REST API. We love Twilio API and have an example and video on how to use the SMS API. The Fax API is as elegant and simple to use. In this blog post you will learn how to send a fax using the Twilio Fax API from a Server Code script.

Create a new Server Code script and copy the following script:

var url = "https://fax.twilio.com/v1/Faxes";
var accountSid = "AC3526fbeed7...............";
var token = "8703513246d3f445e............";
var to = "To number";
var from = "Your Twilio number"
var mediaUrl = "https://www.twilio.com/docs/documents/25/justthefaxmaam.pdf";

var XHRResponse = XHR2.send("POST", url, {
   "body": "To=" + encodeURIComponent(to) + "&From=" + encodeURIComponent(from) + "&MediaUrl="+mediaUrl,
   "headers": {
      Authorization: "Basic " + encodeBase64(accountSid + ":" + token),
      "Content-Type": "application/x-www-form-urlencoded"
   }
});

Apperyio.response.success(XHRResponse.body, "application/json");

The script is simple and does a POST request to the Twilio Fax API.

Line 1 is the Twilio Fax API endpoint.

Line 2 and 3 are Twilio Account SID and Auth Token. You can get both from the console dashboard.

Line 4 is where to send the fax (number).

Line 5 is your Twilio number.

Line 6 is a link to a document you want to fax. You can set it to any PDF file accessible via a direct link (or use the sample one provided by Twilio).

Line 8 is where the POST request is invoked with all the information.

Twilio Fax API uses Basic Authentication so the Account SID and Auth Token need to be encoded as Base64. There is an out-of-the-box plug-in that you can add to your account. The plug-in has a function to encode a string as Base64.

  1. Open the dropdown list (on the left) and select Back to list.
  2. Open the Plugins tab.
  3. Find the EncodeBase64 plugin and click insert to add it to your account. The plugin will be listed in the Library section. You can now use it in any other script.
  4. One more step is to add this library as a dependency for your script. Open that script you created. Go to Dependencies tab and click on EncodeBase64_Library.

You are ready to test the script.

Go back to the Script tab and open the Run tab (on the right). Click on Save and run button to invoke the API and send the fax.

The response should look like this:

{
   "date_updated": "2017-04-06T13:13:40Z",
   "date_created": "2017-04-06T13:13:40Z",
   "num_pages": null,
   "api_version": "v1",
   "media_url": null,
   "url":
   "https://fax.twilio.com/v1/Faxes/FX225bab5034cb8a335c45...............",
   "sid": "FX225bab5034cb8a335c45...............",
   "quality": "fine",
   "duration": null,
   "price_unit": null,
   "price": null,
   "account_sid": "AC3526fbeed7...............",
   "from": "+1415xxxxxxx",
   "to": "+1925xxxxxxx",
   "status": "queued",
   "direction": "outbound"
}

You can navigate to the URL set in the url attribute – there you will be able to see the status of the sent fax (the browser will ask you to login, use the Account SID and Auth Token values).

It can take a few minutes for the fax to be delivered, keep that in mind when testing. If you fax the pre-set PDF file then yon you will the following fax:

twilio-just-the-fax-man
Twilio sample fax

To learn more about Server Code check the Appery.io YouTube playlist. Happy faxing.

How to Expose a Database Stored Procedure and a Function as a REST API

Appery.io API Express makes it easy to expose a SQL query as a REST API. You can as easily expose a database stored procedure or a database function as a REST API. The following two blogs posts will teach you how to do that:

Want to learn more about API Express? Check our the Appery.io YouTube channel for videos on how to integrate with external data sources using API Express.

 

Video: How to Call WordPress REST API

Watch this short video to learn how to call the WordPress REST API from an Appery.io app. The video shows two approaches invoking the API.  First, it shows how to invoke an API using API Express. Second, it shows how to invoke an API from Server Code.

If you like this video, check the Appery.io YouTube channel for many more short videos to help you build apps faster.

Exposing a SQL Database via REST APIs: A Video Guide for Developers

Many organizations that are building mobile apps need to connect and reuse existing (internal) systems. One of the most common system is a relational (SQL) database. The question is — how to a relational database from a mobile app.

I have prepared two short videos that will show you how to do that using Appery.io API Express.

API Express connects to a relational database and exposes the database tables via REST APIs. Once the APIs are created, the mobile app can easily consume the APIs.

There are two approaches exposing a database:

The first approach automatically generates CRUD-like APIs to work with a database table. In many apps, this is close to 80% of what an app might need. This automatic approach also comes with very sophisticated offline support. In other words, you app can work offline and sync data with a remote database when a connection is reestablished.

With the second approach, you get full control as you write the actual SQL query (or Stored Procedure) that will be executed. With this approach, you can use a visual service editor to orchestrate the REST API. In addition to the SQL component (connector), you can use connectors such as Web Service (WSDL), REST  and others.

Check out the API Express video playlist to learn more.