API Versioning with Node.js & Express

backplane

Lewis Sheridan

Posted on August 19, 2024

API Versioning with Node.js & Express

API versioning is crucial for maintaining and evolving a RESTful API without disrupting existing clients. In a Node.js environment, when coupled with OpenAPI, there are several strategies you can employ for API versioning. Below are the most common strategies:

1. URI Versioning

  • Description: The version number is included in the URL path.
  • Example:
    • GET /api/v1/users
    • GET /api/v2/users
  • Pros:
    • Easy to understand and implement.
    • Explicit versioning, easy to test different versions simultaneously.
  • Cons:

    • URL structure might become cumbersome over time.
    • Encourages breaking changes, as each version is a distinct endpoint.
  • Implementation in Node.js:

     const express = require('express');
     const app = express();
    
     app.use('/api/v1', require('./routes/v1/users'));
     app.use('/api/v2', require('./routes/v2/users'));
    
     app.listen(3000, () => console.log('Server running on port 3000'));
    
  • OpenAPI:
    You can create separate OpenAPI specifications for each version:

     openapi: 3.0.0
     info:
       version: 1.0.0
       title: API v1
     paths:
       /users:
         get:
           summary: Get users
           ...
    

2. Query Parameter Versioning

  • Description: The version number is passed as a query parameter in the request URL.
  • Example:
    • GET /api/users?version=1
    • GET /api/users?version=2
  • Pros:
    • Doesn’t change the endpoint structure.
    • Easier to manage within the same endpoint.
  • Cons:

    • Versioning might be less visible.
    • Clients need to remember to include the version parameter.
  • Implementation in Node.js:

     app.get('/api/users', (req, res) => {
       const version = req.query.version;
       if (version === '1') {
         // Handle v1 logic
       } else if (version === '2') {
         // Handle v2 logic
       }
     });
    
  • OpenAPI:
    You can define the query parameter in your OpenAPI spec:

     paths:
       /users:
         get:
           parameters:
             - in: query
               name: version
               schema:
                 type: string
                 enum: ['1', '2']
               required: true
           ...
    

3. Header Versioning

  • Description: The version number is specified in a custom header, such as Accept or API-Version.
  • Example:
    • GET /api/users with Accept: application/vnd.myapi.v1+json
    • GET /api/users with API-Version: 1
  • Pros:
    • Clean URLs, no need to alter the endpoint structure.
    • Versioning logic can be completely abstracted from clients.
  • Cons:

    • Versioning is less visible, making debugging and testing harder.
    • Complexities may arise in managing headers.
  • Implementation in Node.js:

     app.get('/api/users', (req, res) => {
       const version = req.headers['api-version'];
       if (version === '1') {
         // Handle v1 logic
       } else if (version === '2') {
         // Handle v2 logic
       }
     });
    
  • OpenAPI:
    You can define a custom header in the OpenAPI spec:

     paths:
       /users:
         get:
           parameters:
             - in: header
               name: API-Version
               schema:
                 type: string
                 enum: ['1', '2']
               required: true
           ...
    

4. Content Negotiation (Media Type Versioning)

  • Description: The version is encoded in the Accept header’s media type.
  • Example:
    • Accept: application/vnd.myapi.v1+json
    • Accept: application/vnd.myapi.v2+json
  • Pros:
    • Clean URLs.
    • Follows REST principles closely.
  • Cons:

    • Requires clients to understand and correctly implement content negotiation.
    • More complex to implement and manage.
  • Implementation in Node.js:

     app.get('/api/users', (req, res) => {
       const acceptHeader = req.headers['accept'];
       if (acceptHeader.includes('vnd.myapi.v1')) {
         // Handle v1 logic
       } else if (acceptHeader.includes('vnd.myapi.v2')) {
         // Handle v2 logic
       }
     });
    
  • OpenAPI:
    Specify different media types in the OpenAPI spec:

     paths:
       /users:
         get:
           responses:
             '200':
               description: OK
               content:
                 application/vnd.myapi.v1+json:
                   schema:
                     type: object
                     properties:
                       ...
                 application/vnd.myapi.v2+json:
                   schema:
                     type: object
                     properties:
                       ...
    

5. Versioning through Separate Subdomains or Hosts

  • Description: Versioning through separate subdomains or even different domains.
  • Example:
    • https://v1.api.example.com/users
    • https://v2.api.example.com/users
  • Pros:
    • Completely isolates versions, minimizing conflicts.
    • Allows different versions to scale independently.
  • Cons:

    • Requires additional infrastructure management (DNS, SSL, etc.).
    • Increases complexity in managing multiple deployments.
  • Implementation in Node.js:

    This is more about deployment configuration rather than in-code changes, so your Node.js app would be deployed under different domains or subdomains.

  • OpenAPI:

    Each version would typically have its own OpenAPI documentation, hosted on different URLs.

Best Practices

  • Deprecation Policy: Always define a clear deprecation policy, informing clients about how long previous versions will be supported before they are deprecated.
  • Documentation: Ensure that your OpenAPI specs are well-documented and versioned alongside the API codebase.
  • Testing: Use automated testing to cover all API versions.
  • Client Communication: Regularly communicate changes and deprecations to API consumers.

Conclusion

The choice of versioning strategy depends on your specific use case, the complexity of the API, and your clients' needs. URI versioning is the simplest to implement and the most explicit, while header or media type versioning adheres more closely to REST principles but requires more sophistication.

💖 💪 🙅 🚩
backplane
Lewis Sheridan

Posted on August 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related