Access OAuth2 tokens and other extended response attributes from other requests
A Yaak plugin that provides access to extended response attributes, including OAuth2 tokens and response metadata. This plugin replicates the functionality of the popular Insomnia insomnia-plugin-response-extensions.
Settings → PluginsInstallInstall the Yaak CLI:
npm install -g @yaakapp/cli
Clone or download this plugin
Install dependencies:
cd yaak-response-extensions-plugin
npm install
Build the plugin:
npm run build
Install the plugin in Yaak:
This plugin provides four template functions that you can use in any text field in Yaak:
responseExtensions.oauth2Extract OAuth2 token details from a request with OAuth2 authentication configured.
Syntax:
${[ responseExtensions.oauth2(request, filter) ]}
Example - Extract Access Token:
{
"Authorization": "Bearer ${[ responseExtensions.oauth2(auth_request, '$.accessToken') ]}"
}
Available OAuth2 Fields:
$.accessToken - The OAuth2 access token$.refreshToken - The OAuth2 refresh token$.identityToken - The identity token (if available)$.expiresAt - Token expiration timestamp$.error - Error message (if authentication failed)$.errorDescription - Detailed error description$ - Full OAuth2 objectresponseExtensions.responseExtract response metadata from a previous request.
Syntax:
${[ responseExtensions.response(request, filter) ]}
Example - Extract Status Code:
Status: ${[ responseExtensions.response(api_request, '$.statusCode') ]}
Available Response Fields:
$.statusCode - HTTP status code (e.g., 200, 404)$.statusMessage - Status message (e.g., “OK”, “Not Found”)$.contentType - Response content type$.url - The request URL$.headers - Array of response headers$.elapsedTime - Request duration in milliseconds$.bytesRead - Response size in bytes$ - Full response metadata objectresponseExtensions.bodyExtract data from the response body using JSONPath.
Syntax:
${[ responseExtensions.body(request, filter, behavior) ]}
Example - Extract Token from Response Body:
{
"Authorization": "Bearer ${[ responseExtensions.body(login_request, '$.access_token') ]}"
}
Example - Extract Nested Data:
User ID: ${[ responseExtensions.body(api_request, '$.data.user.id') ]}
Available Parameters:
request - The request whose response body you want to extract fromfilter - JSONPath to extract specific data (default: $ for entire body)behavior - When to send the request (default: “When no responses”)
responseExtensions (Generic)A generic function that accepts an attribute type parameter.
Syntax:
${[ responseExtensions(request, attribute_type, filter, behavior) ]}
Attribute Types:
'body' - Extract from response body (default)'oauth2' - Extract OAuth2 token data'response' - Extract response metadataExample:
{
"Body Token": "${[ responseExtensions(api_request, 'body', '$.token') ]}",
"OAuth Token": "${[ responseExtensions(oauth_request, 'oauth2', '$.accessToken') ]}",
"Status": "${[ responseExtensions(api_request, 'response', '$.statusCode') ]}"
}
All functions support a “behavior” parameter that controls when the source request is sent:
Example:
{
"token": "${[ responseExtensions.oauth2(auth, '$.accessToken', 'smart') ]}"
}
Set up a centralized OAuth2 authentication:
Step 1: Create an authentication request with OAuth2 configured
__auth or get_tokenStep 2: Create an environment variable:
{
"access_token": "${[ responseExtensions.oauth2(__auth, '$.accessToken') ]}"
}
Step 3: Use the token in your API requests:
Authorization: Bearer ${[ access_token ]}
Get an authentication token directly from a login response:
Step 1: Create a login request (e.g., POST to /auth/login)
Step 2: Extract token from response body:
{
"api_token": "${[ responseExtensions.body(login_request, '$.access_token') ]}"
}
Step 3: Use in subsequent requests:
Authorization: Bearer ${[ api_token ]}
This works for any API that returns tokens in the response body, like:
{
"success": true,
"access_token": "abc123xyz",
"expires_in": 3600
}
Check if a request succeeded before proceeding:
{
"previous_status": "${[ responseExtensions.response(previous_request, '$.statusCode') ]}",
"previous_url": "${[ responseExtensions.response(previous_request, '$.url') ]}"
}
Get a specific response header value:
${[ responseExtensions.response(api_request, '$.headers[?(@.name=="X-Request-ID")].value') ]}
You can define authentication at the folder level for better organization:
Collection/
├── Folder A/
│ ├── __auth (OAuth2 configured here)
│ ├── Request 1 (uses Folder A's token)
│ └── Request 2 (uses Folder A's token)
└── Folder B/
├── __auth (Different OAuth2 config)
├── Request 3 (uses Folder B's token)
└── Request 4 (uses Folder B's token)
The plugin includes a built-in JSONPath implementation that supports common query patterns:
Supported Patterns:
$ - Root object (returns everything)$.field - Access a specific field$.nested.field - Access nested fields$.array[0] - First element of an array$.nested.array[5] - Nested array accessExamples:
Supported Patterns:
$ - Root object (returns everything)$.field - Access a specific field$.nested.field - Access nested fields$.array[0] - First element of an array$.nested.array[5] - Nested array accessExamples:
$.accessToken → "abc123..."
$.statusCode → 200
$.headers[0] → First header object
$.oauth2Data.expiresAt → 1698765432
$ → Entire object as JSON
Note: The plugin uses a lightweight built-in JSONPath parser. For complex queries with filters (e.g., $.headers[?(@.name=="X-ID")]), consider using Yaak’s built-in response functions or preprocessing the data.
$ as the filter to see all available dataThis plugin provides equivalent functionality to insomnia-plugin-response-extensions:
| Insomnia | Yaak |
|---|---|
{% responseExtensions 'req_id', 'oauth2', '$.accessToken' %} |
${[ responseExtensions.oauth2(req_id, '$.accessToken') ]} |
{% responseExtensions 'req_id', 'response', '$.statusCode' %} |
${[ responseExtensions.response(req_id, '$.statusCode') ]} |
npm run build
npm run dev
This will watch for changes and automatically rebuild the plugin.
Contributions are welcome! Please feel free to submit issues or pull requests.
MIT License - See LICENSE file for details
Inspired by the insomnia-plugin-response-extensions by vajsm.
If you encounter any issues or have questions: