---
url: https://docs.tailor.tech/reference/api/js-scripting.md
---

# JavaScript

Using JavaScript, implement robust validation logic, manipulate data structures, and perform efficient data transformations in resolvers, Triggers and TailorDB.

Here are some examples of where you can utilize JavaScript:

* `PreHook`, `PostHook` blocks in [resolvers](/guides/resolver)
* `Condition`, `URL`, `Body`, `Variables` blocks in [Triggers and Targets](/guides/executor/overview)
* `Validate` and `Hooks` blocks in [TailorDB](/guides/tailordb/overview)
* `create`, `update` hooks in [TailorDB Hooks](/guides/tailordb/hooks)
* `script` validation in [TailorDB Validations](/guides/tailordb/validations)

## Resolver

You can use `PostHook` field in the resolver to return the object that can be used in the later steps.

Throwing an `Error` in a resolver step halts the execution. The API returns the error in the format `<name>: <message>` (e.g., `Error: No heroes found`).

**Example**:

:::tabs resolver-example
:::

## Triggers and Targets

You can write Javascript to check the condition for event based triggers.

:::tabs trigger-condition
:::

In the target type `#TargetTailorGraphql`, you can pass the input variables to the Graphql query as shown below.

:::tabs trigger-variables
:::

You can write custom logic using JavaScript in the `URL` and `Body` fields in the target type `#TargetWebhook`.

:::tabs webhook-url
:::

:::tabs webhook-body
:::

## TailorDB

The example below demonstrates how to use JavaScript in `Hooks` to set a default value for the field. You can add custom logic to validate the input data and set default values.

:::tabs tailordb-hooks
:::

Here's an example on how you can leverage the built-in support for console messages in JavaScript to debug and troubleshoot your code.

:::tabs tailordb-validate
:::

Run the Tailor platform in your local environment to debug the application and view console messages in the log. Contact us if you need more information about running the platform locally.

The `Action` will be evaluated when the `Expr` returns true.

The `_value` and `_data` arguments to the function represent the `description` field and the input data object for the new record.

Additionally, you can access user object which contains information about the user who created or updated the record.

The user object has the following fields:

| Field          | Description                                                                         |
| -------------- | ----------------------------------------------------------------------------------- |
| `id`           | ID of the user.                                                                     |
| `type`         | Type of the user, e.g., `machine_user`.                                             |
| `workspace_id` | Current workspace ID.                                                               |
| `attributes`   | List of IDs; e.g., if the current user has admin rights, the admin ID will be here. |
| `tenant_id`    | Tenant ID.                                                                          |
