> For the complete documentation index, see [llms.txt](https://docs.datajet-app.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datajet-app.com/scripts/creating-a-task.md).

# Creating a custom script

Built-in code editors give you a chance to build any integration you require. Possibilities are endless - your limit is your imagination.\
\
There are a couple of things to keep in mind when creating a task.\
\
**1. Permissions**

To maintain the highest level of security, the app requests only very basic permissions when installed. However, when creating a task, some additional permissions might be required. For example, you might need the `write_orders` permission to add a tag to an order. The app automatically detects the required permissions when the task code is compiled. However, you need to assist the compiler a bit.\
\
When defining GraphQL requests do it at very top of the script:

```javascript
{%- comment -%}Start GraphQL definitions{%- endcomment -%}
{% capture mutation %}
  mutation tagsAdd($id: ID!, $tags: [String!]!) {
    tagsAdd(id: $id, tags: $tags) {
      node {
        id
      }
      userErrors {
        field
        message
      }
    }
  }
{% endcapture %}

```

\
Next, you can use dummy input to tell compiler what variables it should expect.

```javascript
{%- comment -%} Feed compiler with dummy values to evaluate permissions required.{%- endcomment -%}
{% if mode.compiler %}
  {% json dummy_mutation_variables %}
    {
      "id": "gid://shopify/Order/123456",
      "tags": "test"
    }
  {% endjson %}
  {% graphql query:mutation, variables:dummy_mutation_variables as result %}
{% endif %}
{%- comment -%}END GraphQL definitions{%- endcomment -%}
```

Above code snippet won't be executed when your task actually runs. It is because it is wrapped in `if` condition. `mode.compiler` is set to `true` only when creating/editing task.

After adding the above code, the compiler knows what to expect from your task and can easily evaluate the permissions it needs to run. You can now save it and return to the app dashboard. You will be prompted to update app permissions.

<figure><img src="https://295845546-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQfDsLur3-X4nSbZ6jH%2Fuploads%2FxU24O8JFjH85FhRBxjEH%2Fimage.png?alt=media&amp;token=0edc3fcc-f8a7-40cb-95bc-cec100069e3e" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
After clicking the *Update* button, the app should prompt you for additional permissions. After granting permissions you should be redirected back to Scripts Console.
{% endhint %}

Here is how you would tell the compiler about a mutation that needs extra permissions, for example creating a fulfillment:

```javascript
{%- comment -%}Start GraphQL definitions{%- endcomment -%}
{% capture fulfillment_mutation %}
  mutation fulfillmentCreate($fulfillment: FulfillmentInput!) {
    fulfillmentCreate(fulfillment: $fulfillment) {
      fulfillment { id status }
      userErrors { field message }
    }
  }
{% endcapture %}

{%- comment -%} Feed compiler with dummy values to evaluate permissions required.{%- endcomment -%}
{% if mode.compiler %}
  {% json dummy_fulfillment_variables %}
    {
      "fulfillment": {
        "lineItemsByFulfillmentOrder": [
          { "fulfillmentOrderId": "gid://shopify/FulfillmentOrder/123456" }
        ],
        "notifyCustomer": false
      }
    }
  {% endjson %}
  {% graphql query:fulfillment_mutation, variables:dummy_fulfillment_variables as result %}
{% endif %}
{%- comment -%}END GraphQL definitions{%- endcomment -%}
```
