Cypress API request with aws4 signature

Bothi Palani
1 min readNov 7, 2023

I searched everywhere in the internet for how to make an API request using cypress with AWS version 4 signature authorisation but no luck then I referred few documents and found the solution.

So I am sharing how I have done cypress API request using aws4 sign, it might me saving someone’s time :)

package needs to be installed:

npm install aws4

Create a custom function inside Commands.js , I have used service parameter as lambda, it could be any of the aws services like s3 etc.

const baseUrl = 'Your base URL here' ;

Cypress.Commands.add('postRequestWithaws4', (resourcePath, body) => {

let options = {
method : 'POST',
body: JSON.stringfy(body),
url: `https://${baseUrl}${resourcePath}`,
data: body,
headers: {
'Content-Type': 'application/json',
},
service: lambda,
region : eu-west-2,
host: baseUrl
}

const awsCredentials = {
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
}

// Merge options with default headers and AWS credentials
options = aws4.sign(options, awsCredentials);

// Make the HTTP request with the signed headers
return cy.request(options);

});


Cypress.Commands.add('getRequestWithaws4', (resourcePath) => {

let options = {
method : 'GET',
url: `https://${baseUrl}${resourcePath}`,
service: lambda,
region : eu-west-2,
host: baseUrl,
path: resourcePath
}

const awsCredentials = {
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
}

options = aws4.sign(options, awsCredentials);

return cy.request(options);

});

send the API request from your test and check the status code and response

const resourcePath = '/resourcePath/resource'; something like /key/create-key/

const data = {
example : 'data',
something : 'something'
}

cy.postRequestWithaws4(resourcePath, data).then((response) => {
expecte(response.statusCode).to.be.eq(200);
});


cy.getRequestWithaws4(resourcePath).then((response) => {
expecte(response.statusCode).to.be.eq(200);
});

--

--