API Academy
🌐 English
  • 🌐 English
  • 🌐 繁體中文
HomePetstore APIExplore more APIs
HomePetstore APIExplore more APIs
🌐 English
  • 🌐 English
  • 🌐 繁體中文
🌐 English
  • 🌐 English
  • 🌐 繁體中文
  1. Working with APIs
  • Introduction
  • Table of Contents
  • API Academy
    • Get Started
      • What is an API?
      • How Does an API Work?
      • How to Call an API?
      • How to Read an API Documentation?
      • Chapter Summary
      • Get realtime weather
    • API Fundamentals
      • API Funtamentals: Overview
      • Method & Path
      • Parameters
      • Request Body
      • Responses
      • API Specification & OAS
      • Chapter Summary
    • Working with APIs
      • Working with APIs: Overview
      • Making Requests from Spec
      • Environments and Variables
      • Chaining Multiple Endpoints
      • Handling Authentication
      • Handling API Signatures
      • Introduction to Scripts
      • Chapter Summary
    • Mocking APIs
      • Mocking APIs: Overview
      • Smart Mock
      • Mock Expectations
      • Cloud Mock
      • Mock Scripts
      • Chapter Summary
    • Designing APIs
      • Designing APIs: Overview
      • Introduction to API Design
      • Creating Your First API Project
      • Analyzing Requirements and Planning Your API
      • Designing Data Models
      • Designing Endpoints
      • Using Components and Reusability
      • Setting Up Authentication
      • API Design Guidelines
      • Chapter Summary
    • Developing APIs
      • Developing APIs: Overview
      • Setup: Install Your AI Coding Assistant
      • Quick Start: From Spec to Running API in 30 Minutes
      • Understanding the Generated Code
      • Testing Your API with Apidog
      • Deployment: Put Your API Online
      • Chapter Summary
    • Testing APIs
      • Testing APIs: Overview
      • Getting Started: Your First Test Scenario
      • Integration Testing and Data Passing
      • Dynamic Values
      • Assertions and Validations
      • Flow Control: If, For, ForEach
      • Data-Driven Testing
      • Performance Testing
      • Test Reports and Analysis
      • CI/CD Integration
      • Scheduled Tasks and Automation
      • Advanced Testing Strategies
      • Chapter Summary
    • API Documentations
      • API Documentations: Overview
      • Publishing Your First API Doc
      • Customizing Documentation Appearance
      • Interactive Features for Consumers
      • Advanced Publishing Settings
      • Managing API Versions
      • Chapter Summary
    • Advanced API Technologies
      • API Technologies: Overview
      • GraphQL
      • gRPC
      • WebSocket
      • Socket.IO
      • Server-Sent Events (SSE)
      • SOAP
      • Chapter Summary
    • API Lifecycle
      • API Lifecycle: Overview
      • Stages of the API Lifecycle
      • API Governance
      • API Security Best Practices
      • Monitoring and Analytics
      • API Versioning Strategies
      • The Future of APIs
      • Chapter Summary
    • API Security
      • API Security: Overview
      • API Security Fundamentals
      • Authentication vs Authorization
      • Understanding OAuth 2.0 and OpenID Connect
      • JSON Web Tokens (JWT)
      • OWASP API Security Top 10
      • Encryption and HTTPS
      • Chapter Summary
    • API Tools
      • API Tools: Overview
      • The Evolution of API Tools
      • API Clients
      • Command Line Tools (cURL, HTTPie)
      • API Design and Documentation Tools
      • API Mocking Tools
      • API Testing Tools
      • All-in-One API Platforms
      • Chapter Summary
    • API Gateway
      • API Gateway: Overview
      • What is an API Gateway?
      • Key Features of API Gateways
      • API Gateway vs Load Balancer vs Service Mesh
      • Popular API Gateway Solutions
      • The BFF (Backend for Frontend) Pattern
      • Chapter Summary
  • Modern Pet Store
    • Pet
      • Get Pet
      • Update Pet
      • Delete Pet
      • Create Pet
      • List Pets
      • Upload Pet Image
    • User
      • Update User
      • Get User
      • Delete User
      • Login
      • Logout
      • Create User
    • Store
      • List Inventory
      • Create Order
      • Get Order
      • Delete Order
      • Callback Example
      • Pay for an Order
    • Payments
      • Pay Order
    • Chat
      • Create Chat Completion
    • Webhooks
      • Pet Adopted Event
      • New Pet Available Event
  • Schemas
    • Pet
    • Category
    • User
    • ApiResponse
    • OrderPayment
    • Tag
    • Order
    • Links-Order
    • PetCollection
    • Bank Card
    • Bank Account
    • Links
    • Error
HomePetstore APIExplore more APIs
HomePetstore APIExplore more APIs
🌐 English
  • 🌐 English
  • 🌐 繁體中文
🌐 English
  • 🌐 English
  • 🌐 繁體中文
  1. Working with APIs

Introduction to Scripts

Advanced Feature
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
Preparing request data

Example: Adding a Timestamp Header#

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.
2.
Switch to the "Run" tab.
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.
Modified atΒ 2025-12-26 04:31:53
Previous
Handling API Signatures
Next
Chapter Summary
Built with