---
title: "ScriptBuilder Execute Code Instruction"
slug: "execute-code"
tags: ["Test Automation"]
updated: 2025-05-19T07:57:41Z
published: 2025-05-19T07:57:41Z
canonical: "success.panaya.com/execute-code"
---

> ## Documentation Index
> Fetch the complete documentation index at: https://success.panaya.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ScriptBuilder Execute Code Instruction

The Execute code instruction can run a Javascript code during the automated test run.

You can use the Execute code instruction to -

- Run generic [JavaScript code functions](https://www.tutorialspoint.com/javascript/javascript_builtin_functions.htm), such as API calls
- Access or modify the browser DOM and display alerts with Web API functions such as document.getElementById, alert, and others.

You can use the following shortcuts in your JavaScript code - [httpGet](/v1/docs/execute-code#httpget), [httpPost](/v1/docs/execute-code#httppost), and [PanayaVars](/v1/docs/execute-code#panayavars).

This instruction is available for web application scripts only.

> [!WARNING]
> Note
> 
> Due to security restrictions calling API including login credentials or api keys is not permitted. Instead, execute a program that will handle such calls.

> [!WARNING]
> Important!
> 
> The instruction will fail if any of the following occur while running the JavaScript code -
> 
> - An exception is thrown
> - Invalid JavaScript syntax is detected

## httpGet

Our httpGet shortcut for **HTTP**`GET` requires a URL and returns a string.

### Example -Pull Exchange Rate Value

```javascript
var currencyJsonResult = httpGet('https://api.frankfurter.app/latest?from=USD&to=GBP'); //Receive the currency exchange rate value

      //Example of result:  {"amount":1.0,"base":"USD","date":"2024-06-25","rates":{"GBP":0.78836}}
 

var jsobObj = JSON.parse(currencyJsonResult);

var rateGbpToUsd = jsobObj.rates.GBP; //Extract the GBP to USD exchange rate value from results using JS parser

if (rateGbpToUsd > 0) //If the rate is larger than 0, then 

{

    panayaVars.productPrice = rateGbpToUsd*panayaVars.productPrice; //Populate the <productPrice> parameter. 
//productPrice is a predefined Panaya parameter

}
```

## httpPost

Our httpPost shortcut for **HTTP**`POST` requires the following two parameters and returns a string. -

- URL of the page that contains the data
- Data sent as application/x-www-form-urlencoded (the keys and values are encoded in key-value tuples separated by ‘&', with a ‘=' between the key and the value. Non-alphanumeric characters in both keys and values should be URL encoded) E.g.: id=123&category=book

> [!NOTE]
> **Good to Know!**
> 
> If you need to extract a Javascript object from the response, use - JSON.parse(response).

## PanayaVars

The script can access Panaya Variables (Read or Write) using the following syntax - panayaVars.parameterName.

In the example above, the **parameterName**is the name of the parameter defined in the script using the [Set parameter](/v1/docs/automation-parameters) instruction. When the script execution is complete, Panaya variables are sent back to the ScriptBuilder / Agent application with their current value.

Use the syntax below to **read**a Panaya variable -

JSON.parse(response)

var scriptPrice = panayaVars.price * 5; The price is the parameter defined in the application. Use the syntax below to **write**a Panaya variable - panayaVars.price = 100.25;

The price is the parameter defined in the application.

> [!WARNING]
> Important!
> 
> When utilizing panayaVars in Javascript, use the exact capitalization case for the parameter names.

## AI-generated JavaScript Code

You can use AI to automatically generate JavaScript code that can be used inside your automated test scripts.

![](https://cdn.document360.io/f404076c-de23-4609-848e-2dfd4ef701b0/Images/Documentation/image-ED9LOI1Y.png)

In the Execute Code pop-up, click the Create button and use plain English to write a prompt for Panaya to generate the JavaScript code using AI.

![](https://cdn.document360.io/f404076c-de23-4609-848e-2dfd4ef701b0/Images/Documentation/image-213BCUDN.png)

![](https://cdn.document360.io/f404076c-de23-4609-848e-2dfd4ef701b0/Images/Documentation/image-RYKLXBRG.png)

> [!NOTE]
> Good to know!
> 
> - You can use [internal](/v1/docs/automation-parameters) or [input parameters](/v1/docs/automation-input-parameters-data-sets) in your AI prompts.
> - ScriptBuilder functions are not supported in AI prompts.

## AI-generated JavaScript Code Explanation

You can use the new “Explain” button for JavaScript in your automated scripts. Panaya uses AI-generated explanations that will make your work a whole lot easier.

In the execute code pop-up with existing JavaScript code, click the Explain button for Panaya to use AI and generate a clear explanation of what the code does.

![](https://cdn.document360.io/f404076c-de23-4609-848e-2dfd4ef701b0/Images/Documentation/image-VS9HXHTL.png)

![](https://cdn.document360.io/f404076c-de23-4609-848e-2dfd4ef701b0/Images/Documentation/image-S5R3BOB7.png)

## Related

- [ScriptBuilder Instruction Types](/script-builder-instruction-types.md)
