API testing with Cypress

Siddharth Murugan
2 min readSep 5, 2021


Make API call with Cypress

When I getting started with Cypress, it was very difficult to call API from Cypress spec file. When we migrate from one testing framework to another we may feel very difficult to make use of already existing API call functions which we have written in Javascript. I thought of sharing a way which we can reuse the same Javascript code in Cypress. Lets get started!!

NPM package:

npm install cypress
npm install axios

Sign up weather API

To demonstrate this API call, I have made use of public weather API and you can sign up here.

Once you are done, you will get a secret API access key which you can use it to access the data. Try to call the same API with POSTMAN manually to confirm whether it is working fine.

I got 200 status code from postman

Creating Javascript function

It’s time to create a API function to capture the response from the GET request. I am using axios to make GET request. Below is the piece of code created by me for making API call,

When you execute above JavaScript file with node command, you will get the temperature of the provided city inside query.

node weatherAPI.js

Adding API function into Cypress

Once you installed Cypress, lets create a new folder inside Cypress folder called helper and add the same function inside it but don’t forget to export it!

folder structure

Adding inside index.js of Plugins folder

Once you have added the function, you may need import it inside plugins as below,

Making API call inside spec file

Now our goal will be executing this function from spec file and performing assertion by expecting temperature to be greater than 5. We will call function with cy.task() function then creating an alias name for it with .as() function to make use of response in other part of spec file. Refer below piece of spec file

That’s it!! You are done :-) Now you will get response if you execute the Cypress spec file,

Successful execution of testcase



Siddharth Murugan

Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs