Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions general/api-client/scripts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
---
***

Scripts in Requestly allow you to extend and customize your API requests and responses dynamically using JavaScript. These scripts enable you to manipulate requests before they are sent (Pre-request scripts) or process responses after they are received (Post-response scripts). With access to the full request and response objects, you can achieve advanced automation, validations, and transformations.

Check warning on line 15 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L15

Did you really mean 'Requestly'?

<iframe className="w-full aspect-video rounded-xl" src="https://www.youtube.com/embed/jh3Pu4XnbxQ" />

## **Pre-Request Scripts**

**Pre-Request Scripts** run before the API request is sent to the server. They allow you to modify request attributes, such as headers, body, query parameters, or even the URL. Pre Scripts are useful for adding authentication tokens, generating timestamps, or altering the request dynamically based on certain conditions.

Check warning on line 21 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L21

Did you really mean 'Pre'?

<img src="/images/scripts/f4d56b9b-e5d8-48c9-9d2d-8eb277b45b18.png" align="center" fullwidth="false" />

Lets try to understand the workings of pre-script using easy-to-follow examples.
Let's try to understand the workings of pre-script using easy-to-follow examples.

**Auto Increment Page Numbers**

Lets assume you have an endpoint that takes page number as query parameter, we use environment variable `{{page_number}}` to get value of page number.
Let's assume you have an endpoint that takes page number as query parameter, we use environment variable `{{page_number}}` to get value of page number.

```json
https://app.requestly.io/echo?page={{page_number}}
Expand All @@ -44,7 +44,7 @@

During development hitting an API with new data every time can be a pain, we can use Pre-Script to randomise the values and call the same API without getting duplicate entry error.

Lets setup our request with body as follows:
Let's setup our request with body as follows:

```json
POST: <https://app.requestly.io/echo>
Expand All @@ -54,7 +54,7 @@
{
"name": "{{name}}",
"email": "{{email}}",
"phone_number": "{{phone_number}}"

Check warning on line 57 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L57

Did you really mean 'phone_number'?
}
```

Expand All @@ -70,15 +70,15 @@

You can also use pre-script to generate access tokens, validate the requests, generate some random data for the request.

You can also access elements of the request, collection variables and environment variables, checkout Requestlys JavaScript API.
You can also access elements of the request, collection variables and environment variables, checkout Requestly's JavaScript API.

Check warning on line 73 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L73

Did you really mean 'Requestly's'?

## **Post-Response Scripts**

**Post-Response Scripts** run after the API response is received. They allow you to process response data, validate outputs, or log details for debugging. Post Scripts are useful for transforming the response body, validating response codes, or storing results for further use.

<img src="/images/scripts/e1391ebd-3048-48c6-97ce-6cf36276f8e5.png" align="center" fullwidth="false" />

Lets try to understand the working of post script using easy to follow examples.
Let's try to understand the working of post script using easy to follow examples.

**Validate Response Code**

Expand All @@ -90,13 +90,42 @@

We can also fetch and set API Keys or auth tokens, id, and other data from response of an API and use it in other APIs.

You can access elements of the request, response, collection variables and environment variables, checkout Requestly’s JavaScript API.
You can access elements of the request, response, collection variables and environment variables, checkout Requestly's JavaScript API.

Check warning on line 93 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L93

Did you really mean 'Requestly's'?

## **Console Log Filtering**

All console logs generated from your scripts are automatically prefixed with `#script` to help you easily identify and filter script-related logs in the browser console. This makes debugging and monitoring your scripts more efficient.

**Example:**

When you use `console.log()` in your script:

```jsx
console.log("Request sent successfully");
console.error("An error occurred");
```

The output in the browser console will appear as:

```
#script Request sent successfully
#script An error occurred
```

**Filtering Logs:**

To view only script-related logs in your browser console, use the filter feature:
- Open the browser DevTools console
- Type `#script` in the filter box
- Only logs from your API client scripts will be displayed

This prefix is applied to all console methods including `log`, `error`, `warn`, `info`, `debug`, and `trace`.

***

## Requestly JavaScript API `rq`

Check warning on line 126 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L126

Did you really mean 'Requestly'?

Requestly provides a robust set of JavaScript properties and methods to interact with API requests, responses, environments, and global variables. Below is a detailed documentation of these features, explaining each property and function with examples.

Check warning on line 128 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L128

Did you really mean 'Requestly'?

### **Request Object** `rq.request`

Expand All @@ -104,7 +133,7 @@

`rq.request.method`

Use this property to get the Requests method. The HTTP method of the request (e.g. `GET`, `POST`, `PUT`, `OPTION`, `DELETE`, `PATCH`, `HEAD`).
Use this property to get the Request's method. The HTTP method of the request (e.g. `GET`, `POST`, `PUT`, `OPTION`, `DELETE`, `PATCH`, `HEAD`).

**Example:**

Expand Down Expand Up @@ -156,7 +185,7 @@

### **Response Object** `rq.response`

These properties and methods let you access the details of the APIs response in scripts.
These properties and methods let you access the details of the API's response in scripts.

Check warning on line 188 in general/api-client/scripts.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/scripts.mdx#L188

Did you really mean 'API's'?

`rq.response.body`

Expand Down