Scripts is an advanced feature that requires JavaScript knowledge. For most use cases, Apidog's visual processors (pre/post-processors) are sufficient. You can skip this article if you're just getting started with APIs.
In the previous article on Handling API Signatures, we used preprocessor scripts to automatically generate and add signatures to API requests. You might be wondering: what exactly are these scripts, and how do they work?In Apidog, scripts are JavaScript code snippets that allow you to add dynamic behavior to your API requests and responses. While Apidog provides many visual processors for common tasks, scripts give you the flexibility to implement custom logic that cannot be achieved through visual interfaces.In this article, we'll introduce the basics of using scripts in Apidog, including when to use them and simple examples to get you started.
1. What Are Scripts?#
Scripts in Apidog are JavaScript code snippets that execute at specific points during the API request lifecycle:Pre-processor scripts: Run before sending the request to the server
Post-processor scripts: Run after receiving the response from the server
Scripts can access and modify request data, environment variables, and response data, making them powerful tools for customizing your API workflow.
2. When to Use Scripts#
Apidog provides many visual processors for common tasks (like extracting variables, adding assertions, etc.). However, scripts are useful when you need:Custom logic: Complex calculations or conditional logic that visual processors can't handle
Dynamic modifications: Modifying request parameters or headers based on complex conditions
Custom assertions: Writing specific validation rules that go beyond standard assertions
Data transformation: Transforming request or response data in ways that require programming logic
Integration: Calling external programs or services written in other languages
Apidog script syntax is compatible with Postman script syntax. If you're familiar with Postman scripts, you can use them directly in Apidog.
3. Pre-Processor Scripts#
Pre-processor scripts run before the request is sent. They're useful for:Modifying request parameters dynamically
Adding or modifying request headers
Generating signatures or tokens
Setting up authentication
Let's say you want to add a timestamp to every request header:Example: Modifying Request Body#
You can also modify the request body before sending:
4. Post-Processor Scripts#
Post-processor scripts run after receiving the response. They're useful for:Validating response data (assertions)
Extracting values from responses
Storing data in environment variables
Logging response information
Example: Asserting Response Status#
Check if the response status code is 200:Example: Extracting and Storing a Token#
Extract a token from the response and save it to an environment variable:Example: Validating Response Data#
Check if the response contains expected data:
5. How to Add Scripts#
Adding scripts to your requests is straightforward:Step 1: Open Pre/Post Processors#
1.
Open any endpoint in your project.
3.
Scroll down to the Pre Processors or Post Processors section.
Step 2: Add a Custom Script#
1.
Click "Add PreProcessor" or "Add PostProcessor".
2.
Select "Custom Script" from the dropdown.
3.
Enter your JavaScript code in the script editor.
Step 3: Test Your Script#
1.
Click "Send" to execute the request.
2.
Check the Console tab to see any console.log() output.
3.
Verify that your script executed correctly.
6. Debugging Scripts#
You can debug your scripts using console.log() to output information:All console output appears in the Console tab after you send the request.
7. Common Use Cases#
Here are some common scenarios where scripts are helpful:Use Case 1: Dynamic Authentication#
Generate a token or signature before each request:Use Case 2: Conditional Logic#
Modify the request based on environment variables:Use Case 3: Response Validation#
Validate complex response structures:
8. Key Takeaways#
Scripts are JavaScript code snippets that run before (pre-processor) or after (post-processor) requests.
Pre-processor scripts modify requests before sending (e.g., add headers, modify body).
Post-processor scripts handle responses after receiving (e.g., assertions, extract variables).
Use scripts when you need custom logic that visual processors can't handle.
Debug scripts using console.log() to output information to the Console tab.
Apidog scripts are compatible with Postman script syntax.
Scripts provide the flexibility to implement custom logic in your API workflows. While visual processors handle most common tasks, scripts are there when you need more control and customization.This article provides a basic introduction to scripts in Apidog. For more detailed usage, advanced examples, and comprehensive documentation, please refer to the official Apidog Scripts documentation. Now that you have a handle on scripting, let's wrap up this chapter with a Chapter Summary.