Question: I'd like to set up an API versioning, similar to how Stripe does it but I'm not quite sure how to make express do what I need. <https://stripe.com/docs/api#versioning>

What I'm looking for is, the proper route would be something like:

/api/v1/call

The kicker is, I'd like them to pass in a version revision like stripe allows, so if they sent a header like "API-Version: 2015-08-15", it would map to that specific version of the major version. So, v1 but the version updated on 2015-08-15.

Essentially, if there is an update to the API call that is not backwards compatible, I'd roll a new version for that particular call. Express would be smart enough to know that if a version isn't passed, use the latest. If a version is passed, use the latest version for each call up until the version date.

I'd assume the directory structure would be something like:

* /router/
* /router/v1
* /router/v1/call
* /router/v1/anotherCall

And maybe in the call directories, there is a index that checks for the header version and uses the proper file.

So maybe for instance

* /router/v1/call/index.js
* /router/v1/call/20150810.js -- First version
* /router/v1/call/20150815.js -- Updated version that isn't backwards compat.

Thoughts? Ideas?

Answer: 
This how I'm handling versioning. Basically you create a new `router` object and use `app.use` so that only `/api/v1` routes are sent to it. I then use a "fall through" route which catches anything which didn't match and returns a unknown command message. I also renamed the `res.json` function so that I can add `APIversion = 1` to each object that went out (That's in the `router.use` function call). 

Whenever I have a v2 api I'll do this exact same thing but create a new file and use a different `app.use` path. See below:

app.js

```
....
app.use('/api/v1', require('./api1.js'));
....

```

api1.js

```
var express = require('express');
var router = express.Router();

router.use(function (req, res, next) {
    res._json = res.json;
    res.json = function json(obj) {
        obj.APIversion = 1;
        res._json(obj);
    };
    next();
});

/* ADD ALL YOUR ROUTES HERE */

//Done - catch all - return command failed
router.get('*', function (req, res) {
    res.status = 404;
    res.json({
        success: false,
        message: 'Unknown command'
    });
});

module.exports = router;

```
<https://medium.com/@vgjohn/node-js-api-versioning-with-totoro-node-c2ea1ef3dfba>

There's a small package called totoro-node that helps deal with route management for api versioning. It might help to solve some of the problems you're facing. You just write a simple api definition like this and you can control which endpoints or api versions to deprecate or inherit into subsequent api versions. <https://www.npmjs.com/package/totoro-node>

```
var app = express()

app.use('/api', totoro.rain({
    v1: {
        "/oAuth": {
            method: "GET",
            deprecated: true,
            endpointImplementation: routes.authRoutes.oAuth
        },
        "/ssoToken": {
            method: "GET",
            endpointImplementation: routes.authRoutes.sso
        }
    },
    v2: {
        "/ssoToken": {
            method: "GET",
            endpointImplementation: routes.authRoutes.sso
        }
    }
}))

```