Articles

Cypress API Testing: Complete Guide with Examples

Rishi Singh

September 5, 2025

Introduction

Cypress is one of the most popular testing frameworks today, known for its developer-friendly setup and powerful capabilities for end-to-end (E2E) UI testing. But beyond UI testing, Cypress also performs API testing, making it a versatile tool for modern applications where frontend and backend need to work seamlessly.

Unlike traditional testing approaches where teams switch between tools like Postman (for APIs) and Selenium (for UI), Cypress bridges the gap by allowing you to test both layers in one framework. This means you can write fast, reliable, and maintainable tests for your web applications while keeping everything in JavaScript.

Understanding Cypress API Testing

API testing using Cypress focuses on verifying backend services directly without relying on the UI. Cypress provides the cy.request() command, which allows developers to send HTTP requests (GET, POST, PUT, DELETE) and validate responses.

Here’s why Cypress stands out compared to traditional API testing tools:

  • Postman is great for manual or collection-based API tests, but lacks automation hooks for CI/CD pipelines.
  • REST Assured is powerful for Java developers, but not as beginner-friendly and requires a Java-based environment.
  • Cypress for API testing is JavaScript-first, lightweight, and tightly integrated with browser-based workflows.

This makes it an ideal choice for teams already working with JavaScript/TypeScript.

Key Features of Cypress API Testing

Cypress comes packed with features that make API automation testing using Cypress highly efficient:

  • Real-time reloading – See changes instantly as you write tests.
  • Automatic waiting – No need for manual sleep or wait commands.
  • Built-in assertions – Validate status codes, headers, and response data effortlessly.
  • Browser-based debugging – Inspect API requests and responses directly in the Cypress Test Runner.
  • Integration with UI tests – Test end-to-end workflows that combine API and UI layers in one suite.

How to Set up a Cypress API Test

Getting started with Cypress is straightforward:

  1. Install Cypress
    npm install cypress --save-dev
  2. Open Cypress
    npx cypress open
  3. Project Structure
    Inside the cypress/e2e/ folder, create a new folder api-tests/ for your API test files.

Configuration (cypress.config.js)
const { defineConfig } = require('cypress')

module.exports = defineConfig({

  e2e: {

    baseUrl: 'https://jsonplaceholder.typicode.com',

  },

})

This setup ensures your API test suite is well-organized and scalable.

Writing the First API Test using Cypress

Here’s a simple API test with Cypress that validates a GET request:

describe('Cypress API Testing - GET Example', () => {

  it('Should fetch a list of posts', () => {

    cy.request('/posts').then((response) => {

      expect(response.status).to.eq(200)

      expect(response.body).to.have.length.greaterThan(0)

    })

  })

})

This test checks whether the API returns a 200 status code and verifies that the response body is not empty.

Execution through the Cypress Tool

You can run tests in two ways:

Cypress Test Runner (GUI):
npx cypress open

  •  Select your test file and run it interactively.

Command Line (Headless):

npx cypress run --spec "cypress/e2e/api-tests/*.cy.js"

POST Method Example

it('Should create a new post', () => {

  cy.request('POST', '/posts', {

    title: 'foo',

    body: 'bar',

    userId: 1

  }).then((response) => {

    expect(response.status).to.eq(201)

    expect(response.body).to.have.property('id')

  })

})

PUT Method Example

it('Should update a post', () => {

  cy.request('PUT', '/posts/1', {

    id: 1,

    title: 'updated title',

    body: 'updated body',

    userId: 1

  }).then((response) => {

    expect(response.status).to.eq(200)

    expect(response.body.title).to.eq('updated title')

  })

})

DELETE Method Example

it('Should delete a post', () => {

  cy.request('DELETE', '/posts/1').then((response) => {

    expect(response.status).to.eq(200)

  })

})

Advantages of Cypress for API Testing

Using Cypress for API testing offers several benefits:

  • Unified framework for UI + API testing
  • Fast execution with real-time reloading
  • Built-in retries reduce flakiness in tests
  • Simple JavaScript-friendly syntax
  • Easy integration into CI/CD pipelines

Conclusion

Cypress is more than just a UI testing tool—it’s a powerful API automation framework that enables developers to build reliable and scalable tests quickly. With support for all HTTP methods, real-time debugging, and seamless integration into frontend workflows, Cypress ensures faster feedback loops and better developer productivity.

👉 Automate Your Cypress API Tests with BaseRock.ai to take your testing to the next level.

FAQ

1. Why use Cypress for API testing instead of traditional tools?
Cypress offers unified automation for UI and API tests, making it easier to maintain test suites compared to tools like Postman or REST Assured.

2. What does the cy.api() command do?
cy.api() is a community plugin that provides enhanced API testing utilities in Cypress, offering more intuitive commands for making API requests.

3. What are the best practices for scalable Cypress API tests?

  • Organize test files logically
  • Use environment variables for URLs and credentials
  • Combine API and UI tests for end-to-end coverage
  • Run tests in CI/CD for continuous feedback

4. Can I write API tests using Cypress?
Yes. Cypress supports cy.request() for sending API calls directly, allowing you to validate responses and integrate API tests into your automation suite.

Related posts

Articles
September 5, 2025

Cypress API Testing: Complete Guide with Examples

Articles
August 8, 2025

Pact Testing Explained: Contract Testing for Reliable Microservices

Articles
July 28, 2025

System Testing vs Integration Testing: Understanding the Key Differences

Flexibility, Security, and Transparency with Baserock

Flexible deployment - Self hosted or on BaseRock Cloud