Skip to main content

Extracting Variables from Responses

One of the most powerful features of Conduits is the ability to extract data from API responses and use it in subsequent requests. This guide covers everything you need to know about variable extraction.

Why Extract Variables?​

Variable extraction enables:

  • Chaining requests: Use response from one request in another
  • Dynamic workflows: Adapt to API responses in real-time
  • Data-driven testing: Use generated data in tests
  • Complex scenarios: Build realistic multi-step flows

Basic Extraction​

Simple Field Extraction​

Given this response:

{
"id": 12345,
"name": "John Doe",
"email": "john@example.com"
}

To extract the id:

  1. In your conduit step, click "Add Extraction"
  2. Configure:
    • From: Response body
    • Path: id
    • Save as: userId
  3. Save

Now {{userId}} equals 12345 and can be used in later steps!

Using Extracted Variables​

In subsequent requests:

GET {{baseUrl}}/users/{{userId}}/orders

Resolves to:

GET https://api.example.com/users/12345/orders

Extraction Sources​

You can extract from:

Response Body​

Most common source:

{
"data": {
"token": "abc123"
}
}

Path: data.token → saves "abc123"

Response Headers​

Extract from response headers:

Set-Cookie: session=xyz789; Path=/
X-Request-ID: req-42
  • Path: Set-Cookie → saves full cookie string
  • Path: X-Request-ID → saves "req-42"

Status Code​

Extract the status code:

  • Path: $statusCode → saves 200

Use this for conditional logic based on status.

Response Time​

Extract performance metrics:

  • Path: $responseTime → saves time in ms

Useful for performance assertions.

Extraction Paths​

Dot Notation (Simple)​

For simple JSON structures:

{
"user": {
"profile": {
"name": "John"
}
}
}

Path: user.profile.name → "John"

JSONPath (Advanced)​

For complex queries:

{
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
}

Examples:

  • $.users[0].id → 1 (first user's ID)
  • $.users[*].name → ["Alice", "Bob"] (all names)
  • $.users[?(@.id==2)].name → "Bob" (conditional)

Array Access​

Extract from arrays:

{
"items": [
{"id": 1, "name": "Item 1"},
{"id": 2, "name": "Item 2"}
]
}
  • items[0].id → 1
  • items[1].name → "Item 2"
  • items → entire array

Nested Extraction​

Deep Nesting​

{
"data": {
"user": {
"profile": {
"contact": {
"email": "user@example.com"
}
}
}
}
}

Path: data.user.profile.contact.email → "user@example.com"

Multiple Extractions​

Extract multiple values from one response:

  1. Extract data.user.id → {{userId}}
  2. Extract data.user.email → {{userEmail}}
  3. Extract data.user.profile.phone → {{userPhone}}

All available in subsequent steps!

Extracting Arrays​

Entire Array​

{
"orderIds": [101, 102, 103]
}

Path: orderIds → [101, 102, 103]

Use in loops or script steps.

Array Element​

Path: orderIds[0] → 101

Array Properties​

{
"orders": [
{"id": 101, "total": 50},
{"id": 102, "total": 75}
]
}
  • orders[0].total → 50
  • orders[1].id → 102

Transforming Extracted Data​

Type Conversion​

Sometimes you need to convert types:

Extraction:

  • Path: id (extracts "12345" as string)

In Request:

{
"userId": {{userId}} // Use without quotes for number
}

String Manipulation​

Use extracted values in templates:

{{baseUrl}}/users/{{userId}}/profile

Computed Values​

Use variable steps to transform:

  1. Extract price → 100
  2. Add variable step:
    • Name: priceWithTax
    • Value: {{price}} * 1.08 (if scripts supported)

Real-World Examples​

Example 1: Authentication Flow​

Step 1: Login

Request:

POST {{baseUrl}}/auth/login

Body:
{
"username": "{{username}}",
"password": "{{password}}"
}

Response:

{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expiresIn": 3600,
"userId": 42
}

Extractions:

  • token → {{accessToken}}
  • userId → {{currentUserId}}
  • expiresIn → {{tokenExpiry}}

Step 2: Use Token

Request:

GET {{baseUrl}}/users/{{currentUserId}}/profile

Headers:
Authorization: Bearer {{accessToken}}

Example 2: Resource Creation​

Step 1: Create Resource

POST {{baseUrl}}/articles

Body:
{
"title": "My Article",
"content": "Article content here"
}

Response:

{
"id": "article-123",
"slug": "my-article",
"publishedAt": "2024-03-06T10:00:00Z"
}

Extractions:

  • id → {{articleId}}
  • slug → {{articleSlug}}

Step 2: Add Comment

POST {{baseUrl}}/articles/{{articleId}}/comments

Body:
{
"text": "Great article!",
"author": "{{username}}"
}

Step 3: View Article Page

GET {{baseUrl}}/articles/{{articleSlug}}

Example 3: Pagination​

Step 1: Get First Page

GET {{baseUrl}}/users?page=1&limit=10

Response:

{
"users": [...],
"pagination": {
"currentPage": 1,
"totalPages": 5,
"nextPage": 2
}
}

Extraction:

  • pagination.nextPage → {{nextPage}}

Step 2: Get Next Page

GET {{baseUrl}}/users?page={{nextPage}}&limit=10

Best Practices​

Naming​

  • Use descriptive names: userId not id1
  • Follow conventions: camelCase or snake_case
  • Prefix by source: responseUserId, headerRequestId
  • Avoid collisions: Don't overwrite environment variables

Validation​

  • Add assertions: Verify extracted values are valid
  • Check for null: Ensure field exists before extracting
  • Validate format: Check extracted data matches expectations

Organization​

  • Extract early: Get variables as soon as available
  • Extract once: Don't re-extract the same value
  • Clean up: Clear temporary variables when done
  • Document extractions: Explain what's being extracted and why

Common Patterns​

Extract-Transform-Use​

  1. Extract raw value
  2. Transform if needed (variable step)
  3. Use in request

Conditional Extraction​

Extract different values based on response:

  • If status is 200, extract data.result
  • If status is 202, extract data.jobId

Loop with Extraction​

Extract array, then loop over elements:

  1. Extract users → {{usersList}}
  2. For each user in {{usersList}}:
    • Extract id
    • Make request with ID

Troubleshooting​

Variable Not Set​

Check:

  • Is the path correct?
  • Does the field exist in the response?
  • Is the JSON structure as expected?
  • Was the request successful?

Wrong Value Extracted​

  • Verify the extraction path
  • Check for typos in field names
  • Review the actual response structure
  • Use JSONPath tester for complex queries

Variable Not Available​

  • Ensure extraction is in earlier step
  • Check step execution order
  • Verify step completed successfully
  • Check variable scope (conduit vs environment)

Advanced Techniques​

Regular Expression Extraction​

Extract using regex patterns:

Response:

User ID: 12345, Status: Active

Regex: User ID: (\d+) → extracts 12345

XML/HTML Extraction​

For non-JSON responses, use XPath:

<user>
<id>12345</id>
<name>John</name>
</user>

XPath: //user/id/text() → 12345

Combining Multiple Values​

Extract and combine:

  1. Extract firstName → "John"
  2. Extract lastName → "Doe"
  3. Variable step: fullName → "{{firstName}} {{lastName}}"

Next Steps​