API Versioning

Node js 9 min min read Updated: Mar 30, 2026 Intermediate
API Versioning
Intermediate Topic 10 of 10

API Versioning in Express.js

API versioning is a technique used to manage changes in your API without breaking existing client applications. As your application grows, you may need to update endpoints, modify responses, or add new features. Versioning ensures that older clients can still work with previous versions of your API.

Without versioning, even small changes in your API can break frontend applications, mobile apps, or third-party integrations.

Key Concept: API versioning allows you to introduce changes while maintaining backward compatibility for existing users.

What is API Versioning?

API versioning is the practice of assigning versions to your API endpoints so that multiple versions can coexist. Each version represents a different state or evolution of the API.

For example:

  • /api/v1/users → Version 1
  • /api/v2/users → Version 2

Why API Versioning is Important

  • Prevents breaking changes for existing users
  • Allows gradual feature updates
  • Supports multiple client versions
  • Improves maintainability

URL Versioning (Most Common)

The most widely used approach is adding the version in the URL:

javascript app.get("/api/v1/users", (req, res) => { res.json({ version: "v1", users: [] }); }); app.get("/api/v2/users", (req, res) => { res.json({ version: "v2", users: [] }); });

This approach is simple, clear, and easy to manage.

Using Express Router for Versioning

For better structure, you can separate versions using routers:

javascript const express = require("express"); const v1Routes = express.Router(); v1Routes.get("/users", (req, res) => { res.json({ version: "v1 users" }); }); const v2Routes = express.Router(); v2Routes.get("/users", (req, res) => { res.json({ version: "v2 users" }); }); app.use("/api/v1", v1Routes); app.use("/api/v2", v2Routes);

Other Versioning Strategies

1. Header Versioning

The version is passed in the request headers.

javascript const version = req.headers["api-version"];

2. Query Parameter Versioning

The version is passed as a query parameter.

javascript /api/users?version=1

3. Accept Header Versioning

Uses content negotiation with headers:

javascript Accept: application/vnd.myapi.v1+json

Best Practices for API Versioning

  • Use versioning from the beginning
  • Prefer URL versioning for simplicity
  • Avoid frequent breaking changes
  • Maintain older versions until clients migrate
  • Document each version clearly

Common Mistakes

  • Not versioning APIs early
  • Removing old versions too quickly
  • Mixing different versions in the same route
  • Not documenting API changes

Real-World Example

Suppose your API initially returns basic user data in v1:

javascript /api/v1/users → { id, name }

Later, you add email and phone fields in v2:

javascript /api/v2/users → { id, name, email, phone }

Both versions can run simultaneously without breaking existing applications.

When to Create a New Version

  • Changing response structure
  • Removing fields
  • Changing data types
  • Altering API behavior

Conclusion

API versioning is essential for building scalable and maintainable backend systems. It allows you to evolve your API without affecting existing users.

By using versioning strategies like /api/v1/, you can ensure smooth updates and better developer experience.

Quick Summary: API versioning helps manage changes by maintaining multiple versions of your API without breaking existing clients.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators