Skip to content

Comments

Solve : Standardize API Response Format #33

Open
linisha15 wants to merge 1 commit intoOSeMOSYS:masterfrom
linisha15:standardize-flask-api-responses
Open

Solve : Standardize API Response Format #33
linisha15 wants to merge 1 commit intoOSeMOSYS:masterfrom
linisha15:standardize-flask-api-responses

Conversation

@linisha15
Copy link

@linisha15 linisha15 commented Feb 20, 2026

closes #24
Description

This PR introduces a standardized JSON response schema across all Flask API endpoints to ensure consistent response structures.

Currently, some endpoints return raw data (e.g., return jsonify(data), 200), while others return structured objects such as:

{
"message": "...",
"status_code": "success"
}

This inconsistency makes frontend integration and future maintenance more difficult, as consumers of the API cannot rely on a predictable response format.

🚀 Proposed Solution

A helper utility function api_response() has been introduced to enforce a consistent response schema across endpoints.

Standard Success Response
{
"success": true,
"message": "Optional human-readable message",
"data": { ... },
"error": null
}

Standard Error Response
{
"success": false,
"message": "Error description",
"data": null,
"error": "Detailed error info (optional)"
}

🛠 Implementation Details

Added a reusable helper function: api_response(success, message=None, data=None, error=None, status_code=200)

Refactored selected routes to use the new standardized response format

Preserved existing HTTP status codes (200, 400, 404, 500, etc.)

No endpoint URLs were changed

Example helper implementation:

from flask import jsonify

def api_response(success, message=None, data=None, error=None, status_code=200):
response = {
"success": success,
"message": message,
"data": data,
"error": error
}
return jsonify(response), status_code

@arnchlmcodes
Copy link

I noticed that PR #32 (global error handlers for #28) is being worked on in parallel and currently builds the error response structure manually. Since both changes appear to aim for the same standardized schema, it might be helpful to consider aligning that PR to use this helper as well, so the standardization is consistent end-to-end. Just suggesting this in case it helps keep everything unified

@linisha15
Copy link
Author

@arnchlmcodes Thanks for pointing that out! I agree that aligning both PRs would help keep the schema consistent. I'm happy to coordinate or help refactor PR #32 to use this helper if that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Standardize Flask API responses to a consistent JSON schema

2 participants