require method to import preloaded libraries or global helpers directly inside your Requestly Script Tab in API Client requests.
The script environment supports JavaScript-based transformations, validations, and dynamic request logic for HTTP and GraphQL APIs.
require
Therequire method lets you import supported modules into your script. Declare the result as a variable to access functions or objects from that module.
Syntax
Example
Use Global Objects
Requestly script sandbox provides several global variables and utilities you can access directly.Available Globals
Use External Libraries
Therequire method enables you to use preloaded libraries in the Requestly script environment.
All supported modules are sandboxed for safe execution.
Supported Libraries
The following external libraries are available:Call uuid and csv-parse as functions
require('uuid') and require('csv-parse/lib/sync') can each be called directly. Their named members work as
well, such as uuid.v4(), uuid.validate() and const { parse } = require('csv-parse/lib/sync').
auto_parse, auto_parse_date,
rowDelimiter and relax are accepted too. A row with a different number of columns than the first is
dropped when it is the last row and is an error anywhere else.
Install a package from npm
You are not limited to the libraries above. On the desktop app,require() any package from npm and
Requestly installs it when the request is sent:
npm packages and Custom Packages are desktop-only. On the web app,
require() resolves the built-in
libraries listed above but has no user-package tier.If your organization restricts external packages
An organization Owner or Admin can limit which external packages scripts may import, or block them entirely, from Settings > Organization > Resources. Where a package is not permitted, installing it is refused and a script that requires it is blocked when the request runs, with a message naming the package. Requestly’s built-in libraries are exempt and always resolve. See Organization resources.Require your own code as a package
To reuse a helper across requests without repeating it, author it in the Package Library and require it by name with a.js suffix:
How a Custom Package loads
A Custom Package is a CommonJS module. Setmodule.exports, or add to exports:
module. Inside the package,module.idis the package name, andmodule.exportsis the same object asexportsuntil you replace it.require()always returns whatmodule.exportsis at that moment.- One run per script. The package runs the first time a script requires it. Requiring it again in the same script returns the same exports without running it again. The next script gets a fresh copy, so a variable declared in the package starts over.
- A package that fails. If the package throws while it loads, the error reads
Error in package 'signing-helpers': <message>. In the current desktop app the package stays as far as it got, so requiring it again in the same script returns what it had exported and does not run it again. A package that does not exist throwsCannot find package 'signing-helpers'. - Packages that use each other. A package can require other packages, and itself. If two packages require each other, the one that is asked for while it is still loading returns the exports it has set so far.
- What a package sees. A package can use
rq,console,fetch, the timer functions,require(), and variables a script assigned without a keyword (auth = 'abc';). It does not see the old global names (environment,tests,responseBodyand so on),requestly,_,CryptoJSorxml2Json: each reads asundefinedthere. Load a library withrequire()instead.require.cacheis not available. - Packages from npm. A package you installed from npm does not see
rq: it reads asundefined. Pass the values it needs as arguments.
Use Built-in Globals
Requestly also provides a few helper functions and contextual variables for working with request and response data.Examples
xml2Jsondoes not throw on text that is not valid XML. It writes the warningxml2Json could not parse the XML.to the DevTools Console, so check the Console when a script that converts XML fails on a later line.JSON.parseignores a byte-order mark at the start of the text (UTF-8, or UTF-16 in either byte order), so text from a server that writes one parses the same as text without it.
Notes
- A set of Node.js built-in modules is also available via
require(). In Safe mode, the default, these resolve:assert,buffer,crypto,events,path,punycode,querystring,stream,string_decoder,timers,url,utilandzlib.pathis the browser version: it has noposixorwin32, andresolve()starts from/. - In Safe mode,
fsloads as an empty object andosas a neutral stand-in, so a library that requires them still loads, but neither reaches your machine. Modules that need a network socket or a child process (net,child_process) are never available. - The built-in libraries need no installation or internet access; they are pre-bundled. Packages you install from npm are fetched once, then cached.
- The sandbox is isolated for security:
require()cannot reach system-level Node APIs.
These restrictions apply in Safe Mode, the default. A package that needs a native add-on or
file-system access works only in Developer Mode. Asymmetric signing itself is not restricted — it is
require('crypto')’s asymmetric functions (createSign, generateKeyPair and friends) that are
unavailable in Safe mode, while a pure-JavaScript library such as jsrsasign signs RS256 in either mode.
See Script execution modes.
