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
| Command | Description | Example |
|---|---|---|
| cy.request() | Send HTTP request | cy.request('POST', '/login', {user: 'abc'}) |
| expect(response.status).to.eq() | Assert HTTP status code | expect(response.status).to.eq(200) |
| expect(response.body).to.have.property() | Check response body property | expect(response.body).to.have.property('token') |
| cy.request().then() | Handle async response | cy.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.