API versioning validation helps make sure your app talks to the right version of an API. This avoids errors when the API changes.
0
0
API versioning validation in Postman
Introduction
When your app needs to work with different API versions at the same time.
When you want to check if the API version in the response matches what you requested.
When you want to prevent your app from breaking after an API update.
When you test backward compatibility of your app with older API versions.
When you want to confirm that deprecated API versions are no longer accessible.
Syntax
Postman
pm.test("API version is correct", function () { const responseJson = pm.response.json(); pm.expect(responseJson.version).to.eql("v1.0"); });
Use pm.response.json() to parse the API response body as JSON.
Use pm.expect() with .to.eql() to check exact equality.
Examples
This checks the API version from the response header instead of the body.
Postman
pm.test("API version is v2", function () { const version = pm.response.headers.get('API-Version'); pm.expect(version).to.eql('v2'); });
This test checks if the version string starts with 'v1' using a regular expression.
Postman
pm.test("API version starts with v1", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.version).to.match(/^v1/);
});Sample Program
This Postman test script checks that the API response JSON has a version field exactly equal to 'v1.0'.
Postman
pm.test("Validate API version in response body", function () { const responseJson = pm.response.json(); pm.expect(responseJson.version).to.eql("v1.0"); });
OutputSuccess
Important Notes
Always confirm where the API version is returned: in headers or response body.
Use exact matches to avoid confusion between similar versions like 'v1' and 'v10'.
Keep your tests updated when the API versions change or new versions are added.
Summary
API versioning validation ensures your app uses the correct API version.
Check version from response body or headers depending on API design.
Use Postman tests with pm.expect() to verify version values.