Instantly Build REST APIs with Server Code

The Server Code allows to build any app logic and instantly expose it via REST APIs. You use Javascript to write the script which runs on the server. You don’t need to worry about deploying or running any servers, all is done in the cloud. An example is the best way to show it, so let’s do that.

To keep the example simple, let’s say we wan to build an API (script) that takes a developer’s name returns a greeting and also returns the time when the greeting occurred. It’s basically your HelloWorld example.

To create a new Server Code script you just need to click Create script button:

Screen Shot 2015-04-16 at 2.29.54 PM

When the script editor opens, on the left-hand side is the area to write the script and on the right-hand side is the area where you can test the script (you can hide either area by clicking Hide Editor or Hide Test buttons).

Screen Shot 2015-04-16 at 2.33.57 PM

Let’s look closer at the script.

var devName = request.get("devName");
var greeting = "Hello " + devName + "!";
var time = moment().format();

response.success ({"result":greeting, "time":time}, "application/json");

  • The 1st line looks in the request and retrieves the devName parameter passed in the API call
  • The 2nd line creates a greeting string.
  • The 3rd line uses Moment.js library to get the current time.
  • The 4th line returns the response in JSON format. This is the REST API service response.

You might be wondering how can we use Moment.js if the library is not loaded/referenced anywhere? In addition to writing scripts, you can also use libraries. You can load an existing 3rd party library or you can write your own library. Then you can make the script be dependent on one or more libraries.

Screen Shot 2015-04-16 at 2.43.19 PM

As you can see, moment.js is available in Server Code out-of-the-box. You only need to mark the library as dependency for this script:

Screen Shot 2015-04-16 at 2.45.06 PM

The script is simple and the logic is instantly exposed via a REST API. There is nothing extra you need to do. Just write the script logic.

The beauty is that you just built a new REST API.

Click REST information tab to see the API URL:

Screen Shot 2015-04-16 at 2.49.26 PM

  • The Service URL section shows the end point you can use to invoke the service.
    • Optionally, you can also give the script an alias. The alias will replace the script id in the URL.
  • The Description URL is an end point to service documentation. If someone wants to learn what the service, they cna invoke this API to get information about the service.

Before using the service in an app you want to test the API. That’s easy to do. On the right-hand side you will see two tabs: Run and Script parameters.

Screen Shot 2015-04-16 at 2.54.20 PM

As our script takes one parameter: devName you will add the parameter and specify a default value for testing. Now you are ready to test:

Screen Shot 2015-04-16 at 2.54.52 PM

Clicking Save and run button invokes the script and shows the result. Now that you know the script works, it’s time to use the API.

There are many ways you can invoke the script. You can use a curl command, or use the Postman plugin in Chrome browser:

Screen Shot 2015-04-16 at 4.06.21 PM

You can as easily use it when building a mobile app in In fact, you can automatically generate the service on the client from inside the App Builder:

Screen Shot 2015-04-16 at 4.08.50 PM

Once the service is generated, you can test the service (same way you test the service in Postman):

Screen Shot 2015-04-16 at 4.09.56 PM

You can also quickly build a mobile app with the API you just designed:

Screen Shot 2015-04-16 at 4.19.32 PM

With Server Code, you can quickly build APIs that wrap any custom app logic in Javascript. Server Code offers extensive API to help you develop the script. There is API to connect to Database, send push notifications, and you can also integrate with any external systems or 3rd party APIs.

Published by

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.