How to Invoke an External REST API from a Cloud Function
Tags: serverless, tutorial, api
Published: 2018-11-16 18:02:56

---
title: How to Invoke an External REST API from a Cloud Function
published: true
tags: #serverless, #tutorial, #api
canonical_url: https://maxkatz.org/2018/07/20/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](https://dev.to/maxkatz/build-a-serverless-hello-world-function-4kg1-temp-slug-9446025) (plus a [video](https://dev.to/maxkatz/video-build-your-first-cloud-function-3b4b-temp-slug-162187)). 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](http://bit.ly/2znrg5V)
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:

```javascript
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](https://developers.google.com/web/fundamentals/primers/promises) 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:

``` json
{ "joke": "Project managers never ask Chuck Norris for estimations... ever." }
```

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

![cloudfunctions-invoke-restapi](https://katzmax.files.wordpress.com/2018/07/cloudfunctions-invoke-restapi.png?w=1024&h=504)<figcaption>Cloud function code</figcaption>

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

For now, let’s expose this cloud function as a REST API so we can invoke it outside the console. In fact, you will be able to invoke it directly from the browser once we make it a [Web Action](https://console.bluemix.net/docs/openwhisk/openwhisk_webactions.html).

1. On the left-hand side, click **Endpoints**
2. Check **Enable as Web** Action and click **Save**
3. Copy the URL and enter into a browser’s address bar

Here is how it looks in Firefox:

![cloudfunctions-test-browser](https://katzmax.files.wordpress.com/2018/07/cloudfunctions-test-browser.png?w=1024&h=179)<figcaption>Invoking a cloud function</figcaption>

That was easy, right?

In this blog post you learned how to create a cloud function which invokes an external (3rd party) API. It’s very likely that even the simplest application will need to get data from an external API so this a good example/template to have.