Inline vs. Stored Scripts

CustomJS offers two ways to execute JavaScript code for processing data or generating PDFs: Inline Scripts and Stored Scripts. Both methods have advantages depending on your use case.

Inline Scripts

Inline Scripts are sent directly in the API request. This method is ideal for quick tests or one-off executions.

AdvantagesDisadvantages
No need to save the script beforehandCode must be sent with every request
Perfect for ad-hoc requestsCan become cumbersome for complex workflows
Directly test HTML, templates, or logic

Example (PDF Generator)

In this example, an HTML string is passed via input.html and converted into a PDF. The JavaScript code is sent inline with the request.

Example

curl -X POST 'https://e.customjs.io/__js1-YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-H 'customjs-origin: inline/pdf-generator' \
--data-raw '{"input":{"html":"<h1>Hello World</h1>"},"code":"const { HTML2PDF } = require(\"./utils\"); return HTML2PDF(input.html);","returnBinary":"true"}' \
> customjs-output.pdf

Output

PDF file generated successfully: customjs-output.pdf

Stored Scripts

Stored Scripts are saved once in the CustomJS platform and can be reused multiple times.
This is ideal for recurring workflows or more complex scripts.

AdvantagesDisadvantages
Reusable for multiple requestsScript must be saved in the platform first
Code is centrally managedUpdating the script requires changes in the platform
Easier maintenance and versioning
Reduces request size

Workflow in the CustomJS Platform

  1. Open the Code Editor in the CustomJS app.
  2. Create a new script and enter your code.
    Stored Script Example
    Stored Script Example
  3. Save the script and note the Script ID.
  4. Send API requests using the Script ID instead of the full code:
curl -X POST 'https://e.customjs.io/5CbuoC' -H 'Content-Type: application/json' -H 'x-api-key: YOU_API_KEY' -d '{
  "Users": "[{\"name\":\"John\", \"lastName\": \"Doe\"}, {\"name\":\"Luke\", \"lastName\": \"Freeman\"}]"
}'

This approach sends only the input data, while the stored script executes the logic on the platform.

HTML Template View
HTML Template View

Comparsion

Inline JavaScriptStored JavaScript
ImplementationCode is sent directly in the HTTP request body.Functions are saved in CustomJS and referenced by ID.
Best forQuick tests or dynamic, one-off executions.Production use, versioning, and sharing logic across workflows.
Setup effortMinimal: Write and run immediately.Requires initial setup to save and reference functions.
ReusabilityLimited: Code must be redefined each time.High: Reusable across multiple workflows.
MaintainabilityHarder to manage changes across uses.Easier to update and maintain in one place.