cypress-api-logger
v1.3.3
Published
Logs API Request and Response in Cypress Test Runner.
Maintainers
Readme
Cypress API Logger
cypress-api-logger is a Cypress plugin that logs detailed information about API requests and responses directly in the Cypress Test Runner. This tool helps you monitor and debug API interactions efficiently during your test execution.
Preview
What you'll see in the Cypress Test Runner:
LOGGER --- LOGGING STARTED FOR GET : https://api.example.com/users
| Status: 200
| Request Headers: { "Content-Type": "application/json" }
| Response Body:
| [{ "id": 1, "name": "Alice" }, ...]
| Duration: 142msFeatures
- Logs HTTP request details: method, URL, headers, body, etc.
- Logs HTTP response details: status, headers, body, duration, etc.
- Easy integration into existing Cypress projects.
- Provides visibility into API interactions, helping to identify issues faster.
- GraphQL support: auto-detects and logs queries, variables, and responses.
cy.interceptsupport: logs browser-level intercepted requests automatically.- Exclude URLs from logging via substring or wildcard patterns.
- Include URLs (allowlist) — log only the services you care about.
- Log only failures — reduce noise in CI by logging only status ≥ 400.
- Mask sensitive fields — redact tokens, passwords, and API keys from logs.
- Slow request detection — flag requests that exceed a configurable threshold.
onLogcallback — pipe log data to Slack, Datadog, or any external reporter.
Installation
To install the cypress-api-logger plugin, run the following npm command:
npm install cypress-api-loggerUsage
- After installation, import the logger into your
e2e.js(orsupport/index.js) file.import 'cypress-api-logger'; - The plugin will automatically log API request and response details in the Cypress Test Runner.
Example
After importing the plugin, simply use cy.request() in your tests. The plugin will log the relevant details automatically.
Sample Test:
describe('API Request Logging', () => {
it('should log the request and response details', () => {
cy.request('GET', 'https://jsonplaceholder.typicode.com/posts')
.then((response) => {
expect(response.status).to.eq(200);
});
});
});Output in Cypress Test Runner:
- Request Method: GET
- Request URL: https://jsonplaceholder.typicode.com/posts
- Response Status: 200
- Response Body:
<Response body content> - Duration: 123ms
Configuration
Currently, the plugin logs all API requests and responses by default. Custom configurations, such as filtering or modifying the log details, can be added in future versions.
Global Configuration:
You can set global configurations for the plugin usingCypress.env('apiLoggerConfig', {...}). These configurations will apply across all requests in the project.Default Configuration:
maxBodyLines:50
Controls the maximum number of lines displayed for the response body.displayFields:['method', 'url', 'status', 'requestBody', 'requestHeaders', 'responseBody', 'responseHeaders', 'duration', 'graphqlQuery']
Specifies which fields should be displayed in the logs.enableApiLogging:true
Toggles API logging. When set tofalse, no logs will be displayed.enableGraphQLLogging:true
Toggles GraphQL API logging. When set tofalse, no logs will be displayed.Example Usage:
To customize the configuration, add the following to your Cypress test or environment file:
Cypress.env('apiLoggerConfig', { maxBodyLines: 100, displayFields: ['method', 'url', 'status'], enableApiLogging: false, enableGraphQLLogging: true, });excludeUrls:[]
Array of URL patterns to skip logging. Supports exact substrings and*wildcards. Useful for ignoring health checks, analytics, or CDN requests.Cypress.env('apiLoggerConfig', { excludeUrls: ['/health', '/analytics', '*.cdn.com*'], });logOnlyFailures:false
Whentrue, only requests with a response status>= 400are logged. Ideal for CI pipelines where you only care about what broke.Cypress.env('apiLoggerConfig', { logOnlyFailures: true, });maskFields:[]
Array of header and body field names to redact. Matching is case-insensitive. Matched values are replaced with***MASKED***in the log output — keeps tokens and passwords out of your test logs.Cypress.env('apiLoggerConfig', { maskFields: ['authorization', 'x-api-key', 'password'], });includeUrls:[]
URL allowlist — the opposite ofexcludeUrls. When set, only requests matching at least one pattern are logged. Everything else is silently skipped. Supports substrings and*wildcards.Cypress.env('apiLoggerConfig', { includeUrls: ['/api/payments', '/api/orders'], });slowThreshold:null
Duration threshold in milliseconds. Requests that exceed it are flagged with a⚠ SLOWindicator in the Cypress log display name and log message. Set tonull(default) to disable.Cypress.env('apiLoggerConfig', { slowThreshold: 1000, // flag requests slower than 1 second });onLog:null
Custom callback fired after every logged request. Receives the full (masked) log data object. Use it to pipe logs to Slack, Datadog, a file, or any external reporter.Cypress.env('apiLoggerConfig', { onLog: (data) => { // data: { method, url, status, duration, requestBody, // requestHeaders, responseBody, responseHeaders, // isGraphQL, isSlow } if (data.status >= 400) { cy.task('sendSlackAlert', data); } }, });
Per-Request Configuration: Users can also customize the logging behavior on a per-request basis by passing a
configobject inside thecy.request()options.
cy.intercept Support
cypress-api-logger also automatically logs requests intercepted with cy.intercept() when you provide a route handler function.
cy.intercept('GET', '/api/users', (req) => {
req.continue((res) => {
// your assertions here
expect(res.statusCode).to.eq(200);
});
}).as('getUsers');The logger will output the same structured log (method, URL, status, headers, body, duration) for intercepted requests — no extra setup required.
Note: Logging only activates when a handler function is provided. Static stubs (
cy.intercept('/api', { body: {} })) are passed through unchanged.
Contributing
Feel free to contribute to this project! Fork the repository, create a new branch, and submit a pull request with your improvements.
- Fork the repository
- Create your feature branch (git checkout -b feature-name)
- Commit your changes (git commit -m 'Add new feature')
- Push to the branch (git push origin feature-name)
- Create a new pull request
Bugs or Issues?
If you encounter any bugs or have suggestions, please open an issue in the GitHub repository.
About
Author: Hari Prasath VS
GitHub Repository: https://github.com/hariprasathvs/cypress-api-logger
License
This project is licensed under the MIT License .
