Introduction:
There are lot of tools to perform API Testing in the current market such as Postman, SoapUI, REST Assured etc. There are other similar types of tools available. When we are currently switching from mainframe tools to JS based tools even if UI based or API based, there are lots of JS based libraries also available for us such as Axios, Request, Supertest and fetch. Cypress is an emerging tool for E2E UI testing. There is also one major advantage that it provides API testing without installing any new library from npm package. Yes! You heard it right!
We can use Cypress for API testing directly after installing cypress main package.
Why Cypress for API?
It is an open-source, front-end testing tool usually used to automate the test cases of web-based browser applications. It is important and necessary in our project to test API’s before testing an entire regression suite to ensure that the application is stable and to prepare data or to interact with 3rd party servers.
Pre-requisites:
- Install Node.js and npm from given site https://www.npmjs.com/get-npm
- Install Cypress
- We will be using Go Rest APIs https://gorest.co.in
Installation:
- We will need cypress to be ready.
- Create or open project directory and navigate into its cypress-API-test.
- Setup a new npm package by using command in terminal: $ npm init
- To install cypress, use command in terminal: npm i cypress
- Let’s add the following lines to the package.Json for scripts to run our tests.

- To start with cypress run cypress:open command in terminal.
Syntax:

Arguments:
- url (String): The URL is used to make HTTP requests to server.
- body (String, Object): A request body is used as a payload to our request. Cypress sets the ‘Accepts’ request header and serializes the response body by ‘encoding’ option.
- method (String): It is an actual HTTP method. We used to make a request using specific methods. If no method is defined, Cypress uses the ‘GET’ method by default if we didn’t provide any method. There are other main methods available such as:
- POST
- PUT
- DELETE
- UPDATE
- PATCH
- GET
4. options (Object): It is used to change the usual behavior of cy.request() by passing options as Object data type.
Some basic options we can send such as:
- log
- url
- method
- body
- headers
5. Yields: cy.request() provides the response object after getting response body which gives properties such as:
- status
- body
- headers
- duration
Examples:
Let’s start with some code samples with each HTTP method.
- POST: Let’s create users with specific request payload using GoREST, through which userId will be retrieved for future purposes. Insert authorization token with mandatory data of user.

- GET: The GET method is used for reading code from API responses, along with passing parameters for getting specific data. In this example GET method is used to read a specific user by using user-id.

- PUT: The put method is used to update specific data. It uses some request payload along with bearer token for authorization.

- DELETE: The delete method is used for deleting the created record. It needs only one id in base-url along with authorization token as header object.

Ok, so as we have tried basic CRUD operations using Cypress. Let’s talk about the Pros and cons. Can we use Cypress for API testing along with UI-E2E tests or not?
PROS:
- Easy setup and installation.
- Live reloads (whenever you make changes to your code, the test will automatically run so you can keep the Cypress Test Runner open, no need to restart or rerun your test manually).
- Great plugins of choice that you can easily install Plugins | Cypress Documentation.
- Using Cypress, you can verify server response, even manipulate the method implementation as per your need, and can add the required timeout to the response.
- Great documentation available for Cypress. They documented very clearly and explained from basics to advanced level. Community support is also good.
- Cypress provides a customized dashboard feature, where it will show different types of reports and charts, although it is a paid product and there are certain packages available for enterprise level.
- Specifically for API, it will open the browser window and run the entire test case headed, due to this it reduces some execution time which is needed faster in case of API.
CONS:
- Cypress is currently only supported for Chrome, Firefox, Edge, Brave, and Electron browsers. As a result, Cypress is a less favored option for cross-browser testing.
- Except JavaScript, it doesn’t support other global languages like java, python, C# etc.
- Cypress doesn’t support remote execution.
- Doesn’t support other browsers such as IE11 or Safari.
- You can’t test cross domains so sometimes 2FA can be an issue if the login provider is on a separate domain. There are workarounds but it’s trickier to implement.
- It doesn’t support multiple tabs but again there are some workarounds.
- It has limited support for iframe/popup handling.
- It doesn’t allow to run tests on two browsers at the same time, and restricts multi-threading execution.