---
title: Library scripts
description: To use an existing script in a next-generation script, create a library script containing the functionality you want to reuse and reference it from your next-generation script.
component: pingam
version: 8.1
page_id: pingam:am-scripting:library-scripts
canonical_url: https://docs.pingidentity.com/pingam/8.1/am-scripting/library-scripts.html
keywords: ["Scripts"]
page_aliases: ["scripting-guide:library-scripts.adoc"]
section_ids:
  create-library-script: Create a library script
  import-library-script: Import a library script
---

# Library scripts

To use an existing script in a next-generation script, create a *library* script containing the functionality you want to reuse and reference it from your [next-generation](next-generation-scripts.html) script.

You can't use library scripts in a custom scripted node.

A library script can take the format of any JavaScript code. You can also import functionality from another library script.

For example:

* Create a library script using a minified third-party JavaScript utility library, such as [`lodash.js`](https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js).

  |   |                                                                                                                                                                               |
  | - | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  |   | Only import scripts from trusted third parties that you know take security seriously. It is your responsibility to ensure that third-party code is secure and to maintain it. |

* Write your own reusable snippet that enhances AM debugging functionality.

|   |                                                                                                                                                                                                   |
| - | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|   | Modules that use file systems, such as `node:fs` or `XMLHTTPRequest`, are not supported. Only modules that are self-contained and don't use a file system explicitly or indirectly are supported. |

## Create a library script

1. In the AM admin UI, [create a script](manage-scripts-console.html#create-scripts-with-console) of type `Library`.

   The Evaluator Version is automatically set to Next Generation.

   |   |                                                                                                                                                       |
   | - | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
   |   | To make a library script accessible from all realms, [create a global script over REST](rest-api-scripts-create.html#rest-api-scripts-create-global). |

2. In the Script field, paste the contents of a third-party JavaScript or write your own JavaScript code.

   Expose the reusable functions of your library script by defining properties on the `exports` object.

   For similar functionality, refer to the [CommonJS modules](https://nodejs.org/api/modules.html#modules-commonjs-modules).

   For this example, `myExampleLibrary` defines and exports three functions:

   ```javascript
   function add(i, j) {
     return i + j;
   }

   function logTotal(i) {
     logger.info("Total so far: " + i);
   }

   // export functions
   exports.add = add;
   exports.logTotal = logTotal;

   // export a constant
   exports.PI = 3.14;

   // direct export using an inline declaration
   exports.encodeURL = (url) => {
     return utils.base64url.encode(url);
   }
   ```

   |   |                                                                                        |
   | - | -------------------------------------------------------------------------------------- |
   |   | You can't create or export *classes* in library scripts, only functions and constants. |

   As a [next-generation](next-generation-scripts.html) script, a library script has access to all the next-generation [common bindings](script-bindings.html). You can also pass in parameters.

   |   |                                                                                                                                                                                                                                                                                                                                                                                        |
   | - | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
   |   | Make sure you don't use the same name for a local variable as that of a common binding in your script. These names are reserved for common bindings only.If you have already defined a local variable with the same name as one that's added to common bindings in a more recent version of AM; for example, `utils`, you must rename the variable in your scripts before you upgrade. |

3. Save your changes.

## Import a library script

1. In the AM admin UI, [create or edit a next-generation script](manage-scripts-console.html#create-scripts-with-console).

2. In the Script field, load the library using the `require(LIBRARY_SCRIPT)` notation; for example:

   `var mylib = require('myExampleLibrary');`

3. Access the exported functions and constants using the library variable; in this case, `mylib`:

   ```javascript
   var i = mylib.add(10, mylib.PI);
   mylib.logTotal(i);

   var encoded = mylib.encodeURL("http://maths.example.com");
   ```
