If you want it in every JSON response, then I would consider a middleware which appends to the response content:
// AddTokenToResponse Middleware
public function handle($request, Closure $next)
{
return tap($next($request), function (JsonResponse $response) {
$modifiedData = array_merge($response->getData(true), [
'token' => uniqid('tok_')
]);
$response->setData($modifiedData);
});
}
This should give you an idea; it takes the response data, appends the token, and then sets the new data on the response. Your token will likely be coming from elsewhere, but this same general approach will apply. Make sure to add the new middleware either as a route middleware, or for the entire api middleware group as appropriate to your application.