0
0
CypressHow-ToBeginner ยท 4 min read

How to Test API Using Cypress: Simple Guide with Examples

To test an API using cypress, use the cy.request() command to send HTTP requests and then assert the response status and body. This lets you verify your API works as expected in an automated way.
๐Ÿ“

Syntax

The basic syntax to test an API in Cypress is using cy.request(). You provide the HTTP method, URL, and optionally the request body or headers. Then you use .then() or should() to check the response.

  • cy.request(method, url, body): Sends an HTTP request.
  • method: HTTP method like GET, POST, PUT, DELETE.
  • url: The API endpoint URL.
  • body: Optional data sent with the request.
  • Assertions: Check response status, headers, or body.
javascript
cy.request('GET', 'https://api.example.com/items')
  .then((response) => {
    expect(response.status).to.eq(200)
    expect(response.body).to.have.property('items')
  })
๐Ÿ’ป

Example

This example shows how to test a GET API endpoint that returns a list of users. It sends a request, checks the status code is 200, and verifies the response body contains an array of users.

javascript
describe('API Test with Cypress', () => {
  it('GET /users returns list of users', () => {
    cy.request('GET', 'https://jsonplaceholder.typicode.com/users')
      .then((response) => {
        expect(response.status).to.eq(200)
        expect(response.body).to.be.an('array')
        expect(response.body.length).to.be.greaterThan(0)
        expect(response.body[0]).to.have.property('id')
        expect(response.body[0]).to.have.property('name')
      })
  })
})
Output
Test passes if status is 200 and response body is a non-empty array with user objects.
โš ๏ธ

Common Pitfalls

Common mistakes when testing APIs with Cypress include:

  • Not waiting for the request to complete before asserting results.
  • Using incorrect HTTP methods or URLs.
  • Failing to handle asynchronous responses properly.
  • Not checking both status code and response body.
  • Ignoring authentication or headers required by the API.

Always chain assertions inside .then() to ensure the response is ready.

javascript
/* Wrong way: Assertions outside .then() - may run before response */
cy.request('GET', '/api/data')
expect(response.status).to.eq(200) // This will fail

/* Right way: Assertions inside .then() callback */
cy.request('GET', '/api/data').then((response) => {
  expect(response.status).to.eq(200)
})
๐Ÿ“Š

Quick Reference

CommandDescriptionExample
cy.request()Send HTTP requestcy.request('POST', '/login', {user: 'abc'})
expect(response.status).to.eq()Assert HTTP status codeexpect(response.status).to.eq(200)
expect(response.body).to.have.property()Check response body propertyexpect(response.body).to.have.property('token')
cy.request().then()Handle async responsecy.request('/api').then(res => { /* assertions */ })
โœ…

Key Takeaways

Use cy.request() to send HTTP requests and test APIs in Cypress.
Always assert response status and body inside .then() callback.
Check for required headers or authentication when testing secured APIs.
Avoid making assertions outside asynchronous callbacks to prevent false failures.
Use clear and specific assertions to verify API behavior accurately.