|
| 1 | +--- |
| 2 | + id: overview |
| 3 | + title: Overview |
| 4 | + sidebar_label: Overview |
| 5 | + sidebar_position: 1 |
| 6 | +--- |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | + |
| 10 | +# <div style={{display:"flex",alignItems:"center"}}> Voiden Scripting <small style={{alignSelf:"start",fontSize:"12px",marginLeft:"10px",padding:"5px",background:"#8a5cf67d",display:"flex",alignItems:"cetner",gap:"5px",borderRadius:"10px"}}><img src="/img/flask-conical.svg" width="14" /> Beta only</small></div> |
| 11 | + |
| 12 | +> **Note:** This feature is currently in **Beta**. |
| 13 | +
|
| 14 | +Voiden Scripting lets you run **JavaScript** or **Python** code as part of your request lifecycle, using the exposed `voiden` API. |
| 15 | + |
| 16 | +Scripts run at two stages: |
| 17 | + |
| 18 | +- [**Pre Script**](/docs/plugins/core-plugins/pre-post%20script/pre-script) — runs before the request is sent |
| 19 | +- [**Post Script**](/docs/plugins/core-plugins/pre-post%20script/post-script) — runs after the response is received |
| 20 | + |
| 21 | +Both stages share the same `voiden` API. |
| 22 | + |
| 23 | +Scripts run in an isolated environment. You have full freedom to use any library or perform any operation — Voiden simply exposes the `voiden` API on top of that. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Blocks |
| 28 | + |
| 29 | +Voiden Scripting introduces two blocks you can add to your `.void` file: |
| 30 | + |
| 31 | +| Block | Description | |
| 32 | +|---|---| |
| 33 | +| [**Pre-Script**](/docs/plugins/core-plugins/pre-post%20script/pre-script) | Runs before the request is sent. Use it to modify headers, body, params, or cancel the request entirely. | |
| 34 | +| [**Post-Script**](/docs/plugins/core-plugins/pre-post%20script/post-script) | Runs after the response is received. Use it to assert values, extract data, and store variables for later requests. | |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## How Scripts Run |
| 39 | + |
| 40 | +Voiden runs your scripts in an isolated environment so they can't interfere with the app. The way this works differs slightly between languages. |
| 41 | + |
| 42 | +**JavaScript** scripts are sent to a separate Node.js process. The `voiden` API (env, variables) communicates back to the app via message passing, so every API call returns a Promise. This is why you need `await`. |
| 43 | + |
| 44 | +**Python** scripts also run in a separate process, but before the script starts, Voiden pre-loads all your environment variables and runtime variables and passes them in directly. So the `voiden` API is just plain synchronous method calls — no `await` needed. |
| 45 | + |
| 46 | +<Tabs> |
| 47 | + <TabItem value="js" label="JavaScript" default> |
| 48 | + ```js |
| 49 | + const token = await voiden.env.get("TOKEN"); |
| 50 | + const userId = await voiden.variables.get("userId"); |
| 51 | + ``` |
| 52 | + </TabItem> |
| 53 | + <TabItem value="py" label="Python"> |
| 54 | + ```py |
| 55 | + token = voiden.env.get("TOKEN") |
| 56 | + user_id = voiden.variables.get("userId") |
| 57 | + ``` |
| 58 | + </TabItem> |
| 59 | +</Tabs> |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Environment Variables |
| 64 | + |
| 65 | +Environment variables are read-only values configured in your active environment. |
| 66 | + |
| 67 | +<Tabs> |
| 68 | + <TabItem value="js" label="JavaScript" default> |
| 69 | + ```js |
| 70 | + const token = await voiden.env.get("API_TOKEN"); |
| 71 | + ``` |
| 72 | + </TabItem> |
| 73 | + <TabItem value="py" label="Python"> |
| 74 | + ```py |
| 75 | + token = voiden.env.get("API_TOKEN") |
| 76 | + ``` |
| 77 | + </TabItem> |
| 78 | +</Tabs> |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Runtime Variables |
| 83 | + |
| 84 | +Runtime variables are dynamic values you can create, read, and update during execution. They are useful for passing data between requests. |
| 85 | + |
| 86 | +<Tabs> |
| 87 | + <TabItem value="js" label="JavaScript" default> |
| 88 | + ```js |
| 89 | + await voiden.variables.set("key", "value"); |
| 90 | + const val = await voiden.variables.get("key"); |
| 91 | + ``` |
| 92 | + </TabItem> |
| 93 | + <TabItem value="py" label="Python"> |
| 94 | + ```py |
| 95 | + voiden.variables.set("key", "value") |
| 96 | + val = voiden.variables.get("key") |
| 97 | + ``` |
| 98 | + </TabItem> |
| 99 | +</Tabs> |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Logging |
| 106 | + |
| 107 | +Logs appear in Voiden's UI and help you debug, trace flow, or monitor values during script execution. |
| 108 | + |
| 109 | +<Tabs> |
| 110 | + <TabItem value="js" label="JavaScript" default> |
| 111 | + ```js |
| 112 | + voiden.log("message"); |
| 113 | + voiden.log("info", "pre-script ran"); |
| 114 | + voiden.log("warn", "token missing"); |
| 115 | + voiden.log("error", "request aborted"); |
| 116 | + voiden.log("debug", "body:", voiden.request.body); |
| 117 | + ``` |
| 118 | + </TabItem> |
| 119 | + <TabItem value="py" label="Python"> |
| 120 | + ```py |
| 121 | + voiden.log("message") |
| 122 | + voiden.log("info", "pre-script ran") |
| 123 | + voiden.log("warn", "token missing") |
| 124 | + voiden.log("error", "request aborted") |
| 125 | + voiden.log("debug", "body:", voiden.request.body) |
| 126 | + ``` |
| 127 | + </TabItem> |
| 128 | +</Tabs> |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +**Supported levels:** `log` · `info` · `debug` · `warn` · `error` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Assertions |
| 137 | + |
| 138 | +Assertions let you validate values and surface pass/fail results in the Response panel. |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +```js |
| 143 | +voiden.assert(actual, operator, expectedValue, message); |
| 144 | +``` |
| 145 | + |
| 146 | +| Parameter | Description | |
| 147 | +|---|---| |
| 148 | +| `actual` | The value to test (e.g. `voiden.response.status`) | |
| 149 | +| `operator` | Comparison operator (e.g. `"=="`, `"contains"`, `"greater"`) | |
| 150 | +| `expectedValue` | The value you expect | |
| 151 | +| `message` | (Optional) Label shown in the assertion result | |
| 152 | + |
| 153 | +<Tabs> |
| 154 | + <TabItem value="js" label="JavaScript" default> |
| 155 | + ```js |
| 156 | + voiden.assert(voiden.response.status, "==", 200, "Status should be 200"); |
| 157 | + voiden.assert(voiden.response.body.name, "contains", "Voiden", "Name check"); |
| 158 | + ``` |
| 159 | + </TabItem> |
| 160 | + <TabItem value="py" label="Python"> |
| 161 | + ```py |
| 162 | + voiden.assert(voiden.response.status, "==", 200, "Status should be 200") |
| 163 | + voiden.assert(voiden.response.body.name, "contains", "Voiden", "Name check") |
| 164 | + ``` |
| 165 | + </TabItem> |
| 166 | +</Tabs> |
0 commit comments