If you find any errors, typos or have general feedback, select the text and click CTRL+ALT+ENTER.

What are we going to build?

In this tutorial, you’ll learn how to create custom scripts and libraries, and how to share them with other users. You’ll also learn about predefined JS libraries, and we’ll touch on security as well.

Before you begin

Tutorial level: intermediate.
Prerequisites: an Appery.io account.

Creating a script and library

1. Open an existing project or create a new one. Open the Server Code tab.

2. To the left, you should see the list of scripts and libraries. Click “Create script”:Start

3. Name it scriptWithLibrary and click “Save”.CreateScript

If successful, such operation will be notified of:
ScriptSaved

Now, you’ll create a library. We’ll use the popular JavaScript library, underscore.

1. Click the Server Code tab, and then click “Create library”. Name it underscore:CreateLib

2. In your browser, open underscorejs.org. On this site, find Downloads and then find Production version. Click it, and then copy the code.
3. Return to your project. In the code area, insert the copied code. Click “Save”:Underscore

The message ”Library saved” appears.

Now we’ll add our script to the newly created library.

1. Open your scriptWithLibrary script, and go to the Dependencies tab. You can see the list of all existing libraries. To connect to the added library, just check the box labeled “underscore”and you will get the message:ScriptSaved1

2. Go to the Script tab.

3. To be sure that our library works, let’s add some code and invoke a function from the library. For example, type:

This function should return the first element of the “array”:
ScriptAdded

4. Click “Save”.

5. Click “Save and run” on the right of the pane:SaveRun

Our script has been successfully executed (you should see status 200):

Now, we can see the result – the first element of array:
200

Sharing

In this part of the tutorial, we’ll learn how to share scripts and libraries with other users.

1. Open the underscore library.

2. Go to the Permissions tab.

3. Input the email(s) of the user(s) you want to share with, and click “Add”:AddUser

4. Open your script, open the Permissions tab, and perform the same actions.

Now, another user has access to the shared script and library.

Here you can find more information on managing team permission rights.

Predefined JS libraries

Every user has a set of predefined JavaScript libraries. They’re marked with the label system library. The user can also see the version of the library he/she is going to use:SystemLibrary

Remember, if you have read-only rights, you CANNOT:

  • Change system library source code:Handlebars
  • Edit its description:HandlebarsDescr
  • Add new dependencies:NoDeps
  • Delete or rename a system library.

In the description section, you’ll find useful information – homepage URL:
HandlebarsDescrHTML

Or repository URL:
HandlebarsDescrRepository

Let’s take a look at how this all works.

First, create a new script with the predefined system library Handlebar.js.

1. Create the script. Name it greeting and, in the code area, enter:

Greeting

2. Click “Save”.

3. Go to the Dependencies tab, and select the “Handlebars.js” checkbox:DepsGreeting

Now let’s execute our script:

1. Go to the Script tab, and click “Save and run”. You should see:Hello

You may have noticed the Alias input under the REST information tab:
Alias

Alias is a human-friendly synonym for the script, which can be used instead of “greeting.”

2. In the associated input field, type the alias name (e.g. greetings). ClickSave“.

The alias name should be unique.

Compare – before using alias:

And after:

Now, you have a shorter and friendlier name for your script. You can use this URL in your REST service:Alias1

Security

It’s possible to restrict access to your JavaScript:

1. Open your script. Go to the Settings tab.

2. Select a database for managing user access (you should have a database with at least one user. Find more information about this here or here).

3. Check “Allow only authenticated users to call this script”.SettingsTab

The option “Share with support” is placed at the bottom of the page.

When a user shares (turns the toggle “on”), the specified script appears in the admin section. This feature can be used by the support team for viewing and accessing all resources (apps, databases, server scripts, etc) shared:ShareOn

When a user unshares (turns the toggle “off”), the specified resource can’t be accessed for viewing by the support.

4. Go back to the Script parameters tab. You should see the session token input:SessionToken

5. Enter an access token or click “Obtain”.

6. In the popup, enter the username and password of the user you want to give access credentials:
ObtainST

7. Click “Obtain”.

You should see the token.

Running it without a token:

To test the script, under the Run tab, click “Save and run” again:Status200

Trying to execute the script without a session token:

Clear the access token (click “Clear”), switch to the Run tab and run the script again. You should see an “Access denied” error:Error

You can find more information about adding custom server code here.