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:
- In your conduit step, click "Add Extraction"
- Configure:
- From: Response body
- Path:
id - Save as:
userId
- 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→ saves200
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→1items[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:
- Extract
data.user.id→{{userId}} - Extract
data.user.email→{{userEmail}} - 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→50orders[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:
- Extract
price→100 - Add variable step:
- Name:
priceWithTax - Value:
{{price}} * 1.08(if scripts supported)
- Name:
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:
userIdnotid1 - 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
- Extract raw value
- Transform if needed (variable step)
- 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:
- Extract
users→{{usersList}} - For each user in
{{usersList}}:- Extract
id - Make request with ID
- Extract
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:
- Extract
firstName→"John" - Extract
lastName→"Doe" - Variable step:
fullName→"{{firstName}} {{lastName}}"
Next Steps
- Master assertions → to validate extracted data
- Build complete conduits → with extraction
- Check the variables reference →