---
title: Script execution sequence
description: All onRequest and onResponse scripts are executed in sequence. First, the onRequest scripts are executed from the top down, then the onResponse scripts are executed from the bottom up.
component: pingoneaic
page_id: pingoneaic:idm-scripting:script-sequence
canonical_url: https://docs.pingidentity.com/pingoneaic/idm-scripting/script-sequence.html
---

# Script execution sequence

All `onRequest` and `onResponse` scripts are executed in sequence. First, the `onRequest` scripts are executed from the top down, then the `onResponse` scripts are executed from the bottom up.

```
client -> filter 1 onRequest -> filter 2 onRequest -> resource
client <- filter 1 onResponse <- filter 2 onResponse <- resource
```

The following sample router configuration *(tooltip: You can edit the router configuration over REST at the config/router endpoint.)* shows the order in which the scripts would be executed:

```json
{
    "filters" : [
        {
            "onRequest" : {
                "type" : "text/javascript",
                "source" : "require('router-authz').testAccess()"
            }
        },
        {
            "pattern" : "^managed/realm-name_user",
            "methods" : [
                "read"
            ],
            "onRequest" : {
                "type" : "text/javascript",
                "source" : "console.log('requestFilter 1');"
            }
        },
        {
            "pattern" : "^managed/realm-name_user",
            "methods" : [
                "read"
            ],
            "onResponse" : {
                "type" : "text/javascript",
                "source" : "console.log('responseFilter 1');"
            }
        },
        {
            "pattern" : "^managed/realm-name_user",
            "methods" : [
                "read"
            ],
            "onRequest" : {
                "type" : "text/javascript",
                "source" : "console.log('requestFilter 2');"
            }
        },
        {
            "pattern" : "^managed/realm-name_user",
            "methods" : [
                "read"
            ],
            "onResponse" : {
                "type" : "text/javascript",
                "source" : "console.log('responseFilter 2');"
            }
        }
    ]
}
```

This configuration would produce a log as follows:

```none
requestFilter 1
requestFilter 2
responseFilter 2
responseFilter 1
```

> **Collapse: Example Filter Configuration**
>
> This example executes a script after a managed user object is created or updated:
>
> ```json
> {
>     "filters": [
>         {
>             "pattern": "^managed/realm-name_user",
>             "methods": [
>                 "create",
>                 "update"
>             ],
>             "onResponse": {
>                 "type": "text/javascript",
>                 "source": "inlineScript"
>             }
>         }
>     ]
> }
> ```
