Build a Serverless “Hello World” Function
Tags: api, serverless, tutorial
Published: 2018-11-15 19:55:27

---
title: Build a Serverless “Hello World” Function
published: true
tags: #api, #serverless, #tutorial
canonical_url: https://maxkatz.org/2018/07/13/build-a-serverless-hello-world-function/
---

[Serverless](https://en.wikipedia.org/wiki/Serverless_computing), 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](https://en.wikipedia.org/wiki/%22Hello,_World!%22_program) function. You will use [IBM Cloud Functions](https://console.bluemix.net/openwhisk/) to build and run the function (more information about this at the end). For now, let’s jump to creating your first function.

### Creating and testing a cloud function

In this section you will create and test a new function.

1. [Register for a free Lite IBM Cloud account](http://bit.ly/2znrg5V) or [sign into an existing account](http://bit.ly/2znrg5V)
2. Click on **Catalog**
3. Remove the **label:lite** filter and click **Enter** to update the service list
4. Type **functions** and click Enter
5. Click on **Functions** box

![cloudfunctions-catalog](https://katzmax.files.wordpress.com/2018/07/cloudfunctions-catalog.png?w=1024&h=445)<figcaption>Creating a new function</figcaption>

6. Click on **Start Creating**  button
7. Click on **Create Action** option
8. Enter **helloworld** for **Action Name**.
  1. Keep the **Enclosing Package** default value.
  2. For **Runtime** , also keep the default value of **Node.js**  
9. Click **Create** button. A code editor with the function code will load and look like this:

![clounfunctions-hello-world-created](https://katzmax.files.wordpress.com/2018/07/clounfunctions-hello-world-created.png?w=1024&h=243)<figcaption>Function code</figcaption>

  - You didn’t use a template but even the default function comes with the most basic JavaScript code which returns **Hello World**.
  - The function is called **main**.
  - The **params** objects allows to pass parameters into the function. I will cover that in another blog post.
  - **return** returns a JSON object with the function response. In this case it’s just a simple text.  
Next step is to test the function.
10. Click the **Invoke** button (upper right in the editor). The result should look like this:

![cloudfunctions-invoke](https://katzmax.files.wordpress.com/2018/07/cloudfunctions-invoke1.png?w=1100)<figcaption>Function response</figcaption>

You just created a new cloud function (you didn’t write any code but that’s OK for now) and you also were able to test the function right in the cloud.

It’s very likely that you would want to invoke this function as a REST API. Luckily that’s very easy to do.

### Invoking as a REST API

In this section you will invoke the function as a REST API.

1. On the left-hand side, click **Endpoints**
2. In the **CURL** section you will see a curl command to invoke this function. Click the **eye-icon** to show the username/password. Basic Authentication is used to protect this function

![cloudfunctions-api](https://katzmax.files.wordpress.com/2018/07/cloudfunctions-api.png?w=1024&h=126)<figcaption>curl command</figcaption>

3. Click on the **copy-icon** to copy the curl command
4. Open a **Terminal** window and paste the curl command. You should see full JSON response from the API (function):

![cloudfunctions-json-formatted](https://katzmax.files.wordpress.com/2018/07/cloudfunctions-json-formatted.png?w=1024&h=378)<figcaption>Function response (JSON formatted with jsonlint.com)</figcaption>

It’s also possible to invoke the function API directly from the browser address bar. In order to do that, format the URL in the following format:

```
https://username:password@openwhisk.ng.bluemix.net/api/v1/namespaces/maxkatzorg\_dev/actions/hello?blocking=true
```

Replace **username** and **password** with the actual values. You will be able to see these values when you click on the **eye-icon**. Here is how it looks when running from Chrome:

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

### Invoke as a Web Action

In this section you will learn how to invoke the function as a Web Action. A Web Action is very similar to API you invoked in the previous section just without authentication. In a Web Action, authentication is up to the developer.

1. Click on **Endpoints** tab
2. Check **Enable as Web Action** checkbox
3. Click the **Save** button
4. Copy the **URL** and then paste it in a browser address bar. You should see this in the browser:

```json
{    "message": "Hello World" }
```

If you use the latest version of Firefox you should see a formatted version of this output (due to Firefox’s built-in tools)

### IBM Cloud Functions

In this blog post I used IBM Cloud Functions to create the cloud function. IBM Cloud Functions is based on the popular open source [Apache OpenWhisk project](https://openwhisk.apache.org/).

### Summary

In this blog post you learned how build your first cloud function. It was fast and simple. Now, this space is not new (as most people might think). A cloud-based environment where you could execute code had been part of [Backend as a Service](https://en.wikipedia.org/wiki/Mobile_backend_as_a_service) (BaaS) or [Mobile Backend as a Service](https://en.wikipedia.org/wiki/Mobile_backend_as_a_service) (mBaas) type companies. Companies such as [Parse](http://parse.com) (acquired by Facebook and then open sourced), [StackMob](http://stackmob.com) (acquired by PayPal) [Kinvey](http://kinvey.com) (acquired by Progress) and [Appery.io](http://appery.io) provided (Appery.io and Kinvey provide today) the option to write and execute code in the cloud without worrying much about servers. You did have some limits on resources and different pricing structure. That’s a topic for a different blog post.