Sunday, June 22, 2025
HomeAPIWhat should be the API response structure?

What should be the API response structure?

The API response structure should be consistent and easy to understand. It should include a status code, a message, and the actual data being returned. The message should provide a brief explanation of the status code, and the data should be in a well-defined format, such as JSON or XML.

class BaseController extends Controller
{
    public function sendResponse($result, $message, $code=200): JsonResponse
    {
        $response = [
            'success' => true,
            'data' => $result,
            'message' => $message,
        ];

        return response()->json($response, 200);
    }

    public function sendError($error, $errorMessages = [], $code = 404): JsonResponse
    {
        $response = [
            'success' => false,
            'message' => $error,
        ];

        if(!empty($errorMessages)){
            $response['data'] = $errorMessages;
        }

        return response()->json($response, $code);
    }
}

Please take a look at the example above. I have created two functions: one for sending a success response and another for sending an error response. This will assist frontend developers (or anyone using the functions) in understanding whether the response is an error or success. Based on that, they can show the appropriate response to the user. Otherwise, they would need to check each message or code to display the actual message to the user.

Then, you can inherit the class from the BaseController. Just an example:

class UserController extends BaseController
{
    public function index()
    {
        $users = User::all();
        return $this->sendResponse($users, 'Users retrieved successfully.');
    }

    public function show($id)
    {
        $user = User::find($id);
        if (is_null($user)) {
            return $this->sendError('User not found.');
        }
        return $this->sendResponse($user, 'User retrieved successfully.');
    }
}

The UserController class inherits from the BaseController. It has two functions for showing all users and showing a specific user. These functions use the sendResponse and sendError functions from the BaseController to send the appropriate response to the frontend.

Atiq
Atiqhttps://elearn.softscholar.com
Software Engineer with extensive experience developing modern web applications using various programming languages and frameworks. Strong background in converting legacy systems to modern architectures and contributing to SaaS platforms. Proficient in working with microservices and cloud technologies. Adept at managing teams, implementing CI/CD pipelines, and deploying SaaS platforms to optimize enterprise operations. Proven ability to lead cross-functional teams and mentor junior developers, ensuring project success and team growth.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments