💻 API Response Comparison Script
Automatically detect differences between API versions to catch breaking changes before they break your app.
import json
from deepdiff import DeepDiff
import requests
# Compare API responses between two environments
def compare_api_responses(prod_url, staging_url, headers=None):
"""
Fetch and compare JSON responses from two API endpoints.
Returns detailed differences in structure, types, and values.
"""
# Fetch responses from both environments
prod_response = requests.get(prod_url, headers=headers).json()
staging_response = requests.get(staging_url, headers=headers).json()
# Compare with DeepDiff for detailed analysis
diff = DeepDiff(prod_response, staging_response,
ignore_order=True,
verbose_level=2)
# Format and display differences
if not diff:
print("✅ No differences found! APIs are consistent.")
return None
print("🚨 API Differences Detected:")
print("=" * 50)
# Check for type changes
if 'type_changes' in diff:
print("\n📊 TYPE CHANGES:")
for path, details in diff['type_changes'].items():
print(f" {path}: {details['old_type']} → {details['new_type']}")
# Check for added/removed fields
if 'dictionary_item_added' in diff:
print("\n➕ ADDED FIELDS:")
for field in diff['dictionary_item_added']:
print(f" {field}")
if 'dictionary_item_removed' in diff:
print("\n➖ REMOVED FIELDS:")
for field in diff['dictionary_item_removed']:
print(f" {field}")
# Check for value changes
if 'values_changed' in diff:
print("\n🔢 VALUE CHANGES:")
for path, details in diff['values_changed'].items():
print(f" {path}: {details['old_value']} → {details['new_value']}")
return diff
# Example usage
if __name__ == "__main__":
# Your API endpoints
PROD_API = "https://api.example.com/v1/users/123"
STAGING_API = "https://staging-api.example.com/v1/users/123"
# Add auth headers if needed
headers = {
"Authorization": "Bearer your_token_here",
"Content-Type": "application/json"
}
# Run comparison
differences = compare_api_responses(PROD_API, STAGING_API, headers)
The Problem: When Your API Decides to Evolve Without You
Let's be honest: APIs are the digital equivalent of those friends who keep changing their personality. One day they're returning JSON with snake_case, the next they've converted to camelCase because they read a Medium article. Or worse, they've decided that the 'email' field should now be called 'electronic_mail_address' because someone thought it would be 'more descriptive.'
The real tragedy isn't that APIs change—that's inevitable. The tragedy is that they change sneakily. Like ninjas in the night, they slip from staging to production with subtle modifications: a field type changes from string to integer, a required field becomes optional (or vice versa), or someone decides that 'null' and 'undefined' are interchangeable concepts. These aren't just bugs; they're betrayal. Your API promised to be consistent, and now it's out there living its best life, shape-shifting without so much as a changelog entry.
Consider this all-too-familiar scenario: You deploy to production feeling like a hero. The tests pass! The staging environment loves it! Then, at 2 AM, your phone buzzes. Production is down. Why? Because the authentication endpoint in production returns a slightly different error format than the one in staging. Instead of {'error': 'Invalid token'}, it's now {'message': 'Token validation failed', 'code': 401}. Your frontend is looking for 'error' but finding 'message,' so it's showing users 'Something went wrong!' which is technically true but spectacularly unhelpful.
The Solution: Stop Guessing, Start Comparing
Enter API Chameleon, the tool that treats your API like the unpredictable creature it is and actually documents its transformations. Instead of playing 'spot the difference' with Postman requests across three different environments, API Chameleon automatically captures API responses and compares them side-by-side.
Here's how it works in practice: You configure API Chameleon to monitor specific endpoints across your environments (dev, staging, prod). It makes identical requests to each environment and captures the responses. Then, like a forensic analyst examining crime scene photos, it compares the responses and generates a detailed diff report. Did a field disappear? Type change? New field appear? API Chameleon spots it and tells you exactly what changed and—here's the crucial part—how severe that change is.
The beauty of this approach is that it's proactive rather than reactive. Instead of discovering breaking changes when users complain (or when your monitoring alerts wake you up at 2 AM), you catch them during development or deployment. It's the difference between noticing your car has a flat tire before you leave for work versus noticing it when you're already on the highway.
How to Use It: Less Detective Work, More Actual Work
Getting started with API Chameleon is refreshingly straightforward. First, install it via npm:
npm install -g api-chameleonThen create a configuration file that defines your environments and endpoints:
// chameleon.config.json
{
"environments": {
"dev": "https://api.dev.example.com",
"staging": "https://api.staging.example.com",
"prod": "https://api.example.com"
},
"endpoints": [
"/api/v1/users",
"/api/v1/orders",
"/api/v1/auth/login"
],
"output": "./api-diffs"
}Run the comparison:
api-chameleon compare --config chameleon.config.jsonCheck out the full source code on GitHub for more advanced configuration options, including authentication headers, request payloads, and custom comparison rules.
Key Features That Actually Help
- Automatically captures and compares API responses across environments: No more manual requests in Postman or cURL. API Chameleon handles the tedious part so you can focus on the important part (fixing things before they break).
- Highlights structural differences: Added fields, removed fields, type changes (string → number, null → object)—it catches the subtle stuff that breaks your application in subtle (and not-so-subtle) ways.
- Generates a diff report with severity ratings: Not all changes are created equal. API Chameleon categorizes changes as breaking (will probably break your app) or non-breaking (might be fine, but you should know about it).
Conclusion: Stop Debugging, Start Building
API Chameleon won't stop your APIs from changing—that would require convincing every developer on your team (and every third-party service you integrate with) to never modify anything ever again. But it will give you visibility into those changes before they become production issues. It turns the 'it works on my machine' problem into 'I know exactly why it doesn't work in production, and I fixed it already.'
Try it out: https://github.com/BoopyCode/api-chameleon
Your future self—the one who doesn't get woken up at 2 AM because of a subtly changed API response—will thank you. Or at least, that version of you will be better rested and slightly less likely to rage-quit and become a goat farmer.
Quick Summary
- What: API Chameleon automatically captures and compares API responses across different environments to catch breaking changes before they break everything.
💬 Discussion
Add a Comment