# executejavascript: Execute Javascript Code

> executejavascript (Execute Javascript Code) is a 1001fx API function in the group Code: POST https://api.1001fx.com/code/executejavascript, 5 credits per item. It works as a ready-made module in Make and n8n or as a plain HTTP call.

Execute JavaScript code in a sandbox on our server. The following npm packages can be used: momentjs, cheerio, handlebars, ... (see https://1001fx.com for full list). Exceptions will be caught and returned. Strict is forced and imports are not allowed.

- Group: Code
- Endpoint: `POST https://api.1001fx.com/code/executejavascript`
- Authentication: header `X-API-KEY: YOURSECRETAPIKEY`
- Content type: application/json
- Credits: 5 credits per item
- Ready-made in: Make and n8n
- Docs: https://1001fx.com/functions/executejavascript

## Parameters

- `code` (string, required) – Code: Code to be executed. Has to be valid javascript code. Store result in a variable and just write the variable in a single line at the end to return the value.

## Use cases

- Run a calculation or data transformation that your workflow tool has no module for
- Parse HTML with cheerio or render a Handlebars or Mustache template
- Validate data with zod, yup or joi before you send it to another system
- Generate UUIDs or ULIDs for new records

## Example: Simple calculation

```bash
curl -X POST 'https://api.1001fx.com/code/executejavascript' \
  -H 'X-API-KEY: YOURSECRETAPIKEY' \
  -H 'Content-Type: application/json' \
  -d '{"code":"const a = 2; const b = 3;return a * b;"}'
```

Answer (HTTP 200):

```json
{
  "result": 6
}
```

## FAQ

**How do I return a value?**

Store the result in a variable and write only the variable name in the last line of your code. Its value is returned as result.

**Which libraries can I use?**

Among others moment, moment-timezone, cheerio, handlebars, mustache, lodash, underscore, axios, fetch, crypto-js, uuid, ulid, joi, yup and zod. Imports are not allowed, the libraries are already available.

**What happens if my code throws an error?**

The code runs in a sandbox on the 1001fx servers in strict mode. Exceptions are caught and returned in the answer, so your workflow can react to them.
