---
updatedAt: 2025-07-15T08:40:22.000Z
agentTools:
  projectIndex: https://docs.appery.io/llms.txt
---

# Script

This page explains how to write, test and configure scripts.

Selecting any [**Server Code** script](https://appery.io/servercode) opens the **Script** tab with script editor and script settings.

# *Script* Tab

Under the **Script** tab, both the ***Script Editor*** and ***Script Test*** options are enabled by default.\
To disable/enable back one of them, click the **Hide/Show Editor** or **Hide/Show Test** buttons correspondingly:

<Image title="Script.jpg" alt={1857} align="center" border={true} src="https://files.readme.io/e9e444d-Script.jpg">
  Server Code Script tab
</Image>

## *Script Editor*

The script editor (**Edit** and **API Information** tabs) is where you can add, write, and/or edit the script code and view/define its API information.\
When [creating](https://docs.appery.io/docs/servercode-overview#creating-scripts) a script you can select whether you would like to upload a previously saved file (click ***Load from file***) or use one of the ready-to-go [snippets](https://docs.appery.io/docs/servercode-plugins) created to simplify the development process (select the snippet you need and click **insert** to add it to the code). Then, save. After you click the **Save** button, you will see other options available for this script.\
You can also download the ready script for later use:

<Image align="center" className="border" border={true} src="https://files.readme.io/49eb41b-2023-02-27_23-42-08.png" />

### Editor Tips

There are a number of very useful coding features that you get inside the editor. They are:

* [Code formatting](https://docs.appery.io/docs/servercode-script#code-formatting);
* [Code collapsible blocks](https://docs.appery.io/docs/servercode-script#code-collapsible-blocks);
* [Autocomplete](https://docs.appery.io/docs/servercode-script#autocomplete);
* [Help hints](https://docs.appery.io/docs/servercode-script#help-box);
* [Toggling to/from full-screen mode](https://docs.appery.io/docs/servercode-script#toggling-tofrom-full-screen-mode);
* [Find and replace](https://docs.appery.io/docs/servercode-script#find-and-replace).

#### Code Formatting

You can make your code easier to read by clicking the **Auto format code** button or by using the  `<Ctrl>+<Shift>+<F>` shortcut:

<Image title="Auto format.jpg" alt={631} align="center" width="smart" border={true} src="https://files.readme.io/ffba10e-Auto_format.jpg">
  Auto Formatting Option
</Image>

#### Code collapsible blocks

This option allows collapsing code blocks. To use it, click the arrow button against the code block to be collapsed:

<Image title="Collapse.jpg" alt={601} align="center" width="smart" border={true} src="https://files.readme.io/48a7a3b-Collapse.jpg">
  Code Collapsible Blocks Option
</Image>

#### Autocomplete

The ***Autocomplete*** option (`<Ctrl>+<Space>`) allows you to choose from a list of base JavaScript constructions and API keywords:

<Image title="Autocomplete.jpg" alt={810} align="center" border={true} src="https://files.readme.io/4764dd9-Autocomplete.jpg">
  Autocomplete Option
</Image>

For example, if you start typing *Database* and press `<Ctrl>+<Space>`, you will find *DatabaseUser* completed in the editor. You can also use ***Autocomplete*** for object members. For the *DatabaseUser* object, you will see available methods after you press `<Ctrl>+<Space>`.

#### Help Box

By using the ***Help Box*** option, you can see additional useful information about the selected methods:

<Image title="Hint.jpg" alt={1130} align="center" border={true} src="https://files.readme.io/07c2a7d-Hint.jpg">
  Help Box Option
</Image>

#### Toggling to/from Full Screen Mode

The ***Toggling to/from Full Screen Mode*** option allows you to switch to the Full Screen mode for comfortable editing and then return to the code editor:

<Image title="FullScreenMode.jpg" alt={633} align="center" width="smart" border={true} src="https://files.readme.io/f24899b-FullScreenMode.jpg">
  Toggling to/from Full Screen Mode Option
</Image>

#### Find and Replace

By pressing `<Ctrl>+<H>`, you can open the find-and-replace window that will be useful when editing multiple lines of code:

<Image title="CtrlH.jpg" alt={624} align="center" width="smart" border={true} src="https://files.readme.io/a49f88a-CtrlH.jpg">
  Find and Replace Option
</Image>

#### Saving Code

There are a number of ways to save your code.

* Clicking the **Save** button above the script editor;
* Clicking the **Save and run** button inside the **Script > Run** tab;
* Using the `<Ctrl+S>` shortcut.

#### Keyboard Shortcuts:

***Windows***

* `<Ctrl>+<Shift>+<F>` – format code.;
* `<Ctrl>+<Space>` – autocomplete.;
* `<Ctrl>+<H>` – find and replace.;
* `<Ctrl>+<Q>` – show hint;
* `<Ctrl>+<S>` – save script;
* `<Ctrl>+<D>` – remove current string;
* `<F11>` – open script code (or request body) in Full-Screen mode.

***Mac OS***

* `<Ctrl>+<Shift>+<F>` – format code;
* `<Ctrl>+<Space>` – autocomplete;
* `<⌘>+<S>` – save script.

### *API Information*

Every script is automatically exposed via a REST API. The **API Information** section provides information about the script API.\
Under the **API information** tab, you can also load the script from the file that was prepared beforehand or download the script when ready if you plan to reuse it:

<Image align="center" className="border" border={true} src="https://files.readme.io/1a8f712-2023-02-27_23-49-35.png" />

#### Service URL

Here, the REST API URL is displayed:

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/e8c1a16-Screenshot_126.png" />

On this tab, you can also turn off script invocation. If script invocation is turned off and the script is invoked, the following message will be returned:

```json Script not accessible
{"code":"SCSX047","description":"Script not accessible"}
```

The **Alias** is a unique friendly name you can set for this script. When [invoking](https://docs.appery.io/reference/servercode-scriptcall) the script you can replace the script ID with the alias name.

#### Maintenance mode

In the **Maintenance mode** section, you can manage public access to this script by enabling or disabling this option.

#### Description URL

Under **Description URL**, you can enter script short documentation or description. The description has a separate URL that will return information about this script. This can be useful for documentation purposes or if someone wants to learn about the script by invoking an API. The **MIME**  type allows you to set the content type for the description.

### JavaScript API Supported

**Server Code** scripts have support for all standard JavaScript APIs. You can create custom APIs by using or creating [Libraries](https://docs.appery.io/docs/servercode-libraries).

### Snippets

Snippets are located to the right of the script editor:

<Image align="center" className="border" border={true} src="https://files.readme.io/5cfda57-Screenshot_125.png" />

Snippets are short code samples that can be quickly inserted into a script. Snippets make it faster to write your script. Currently, we have snippets that cover most of the out-of-the-box API provided by **Server Code**.

## Test

The **Test** tab allows testing of the scripts added to your app.

### Run (testing)

Testing **Server Code** script is simple. Go to the **Run** tab and click the **Save and Run** button. You should see an output that looks like this:

<Image align="center" className="border" border={true} src="https://files.readme.io/9a8283d-948c3e4-TestScript.jpg" />

Please check [this](https://docs.appery.io/docs/servercode-quickstart-creating-your-first-app-backend#testing-script) example of testing the working script is illustrated.

The **Trace info** area will show any debug output such as when using `console.log(..)` statement in the script.

## *Script* parameters

Your script most likely will have parameters (input) that it requires. This tab allows you to enter any parameters that the script might need for testing. Enter a parameter name and click the **Add** button. Once a parameter is added you set its value.\
You can also select if the test should be performed as HTTP ***GET*** or ***POST***.

For a ***POST*** request, you can also enter the request body.

> 📘 Obtaining *X-Appery-Session-Token* from **Server Code**
>
> The **[sessionToken](https://docs.appery.io/docs/x-appery-session-token)** can be obtained from under the **Server Code > Script > Script parameters** tab.\
> Please check [this](https://docs.appery.io/docs/working-with-apperyio-database-and-server-code#obtaining-x-appery-session-token) link to learn how to do it.

# *Trace* Tab

The **Trace** tab keeps a history of the debug output from the script:

<Image align="center" className="border" border={true} src="https://files.readme.io/5ade531-Screenshot_119.png" />

# *Statistics* Tab

The **Statistics** tab shows information about this script. The page shows:

* Created - date and time the script was created;
* Updated - date and time the script was updated;
* Runtime - total time this script has run;
* Launched - the number of times this script has been launched;
* Script size - script size;
* Versions - the number of saved versions (**Versions** tab) of this script.

<Image align="center" className="border" border={true} src="https://files.readme.io/b2c51fd-Screenshot_118.png" />

# *Dependencies* Tab

The **Dependencies** tab lists all the available [libraries ](https://docs.appery.io/docs/servercode-libraries) and allows adding a library dependency for this script:

<Image align="center" className="border" border={true} src="https://files.readme.io/754fac6-Screenshot_120.png" />

Simply check a library to make it available in this script. When a library is made available a script, the library variables, and functions are available in this script.

# *Versions* Tab

The **Versions** feature allows creating a version of your current script:

<Image align="center" className="border" border={true} src="https://files.readme.io/335a1f5-Screenshot_121.png" />

Open the **Versions** tab and click **New Version** to create a new script version. It is a good idea to enter a version comment to help you identify the version later.

You can restore a script to a certain version or delete a version from the list. Restoring the script to a certain version will restore all data and settings that were in the script at the version creation time.

> 🚧 Restoring a Version
>
> When you restore a version, the current script and all its settings will be replaced by the restored version.

> 📘 Good practice
>
> It's a good idea to create versions during your development. This way if you break something, you will be able to rollback (restore) to script version that was working.

# *Permissions* Tab

The following access permissions can be set:

* **View**;
* **Edit**;
* **Delete**.

<Image align="center" className="border" border={true} src="https://files.readme.io/22d03ba-Screenshot_122.png" />

# *Settings* Tab

You can also delete or clone the scripts and/or libraries of your choice. To do this, under the **[Server Code](https://appery.io/servercode/)** tab, open the **Settings** tab of the needed resource, and select the necessary option:

<Image align="center" className="border" border={true} src="https://files.readme.io/8cca506-Screenshot_108.png" />

You can also configure the script by adding the database for managing user access\
and share it with support:

<Image align="center" className="border" border={true} src="https://files.readme.io/7878257-Screenshot_124.png" />

## Work with *Apperyio DB API Keys*

Make sure you select the database for managing user access:

<Image align="center" className="border" border={true} src="https://files.readme.io/79bce38-2023-02-19_21-52-37.jpg" />

Add the line `var dbId = Apperyio.env.apiKeys[0];` to the needed script and save:

<Image align="center" className="border" border={true} src="https://files.readme.io/97276e4-2023-02-20_12-27-42.jpg" />

Now, the database is linked and can be used for managing access to the script.\
For example, here is what the **Trace** tab looks like after linking ***StripeShop*** DB:

<Image align="center" className="border" border={true} src="https://files.readme.io/a68468f-2023-02-19_21-46-37.jpg" />

> 👍
>
> Note the selected database API key under its **Settings** tab: it should be the same as the key found in the **Trace** tab of the script:

<Image align="center" className="border" border={true} src="https://files.readme.io/f15987b-2023-02-19_21-49-52.jpg" />

# *Script* Authentication

To add security to a script, you can require a session token to be provided in order to invoke the script. The token is passed to the script via the ***X-Appery-Session-Token*** header parameter.

The security feature works with the **[Users](https://docs.appery.io/docs/database-collections#users)** collection from the database. In other words, a user from the **Users** collection will do login and his/her session token is then passed to the script for invocation.

In this section, select the database to use (where the **Users** collection is) and then check the **Allow only authenticated users to call this script** option.

# Organizing **Server Code** *Scripts* in Folders

To help organize **Server Code** scripts, you can place them in folders. To create a folder, click the **Create folder** link (below the **Create script** button):

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/24a250e-Screenshot_110.png" />

A folder menu can have the following actions:

* New folder;
* New script;
* Rename;
* Delete;
* Clone;
* Enable maintenance;
* Disable maintenance.

<Image title="NewFolder.jpg" alt={473} align="center" width="smart" border={true} src="https://files.readme.io/29a8002-NewFolder.jpg">
  SC Script Folder Menu
</Image>

To place a script into a folder, click the **gear** icon when hovering over the folder name.  You can also drag and drop a script inside a folder.\
To create a folder inside another folder, hover the mouse over the folder and click the gear icon.