# Handling Requests
The JsonApi class is a PSR-15 request handler (opens new window).
Instantiate it with your API's base path, then pass in a PSR-7 request and you'll get back a PSR-7 response. You should catch any exceptions and pass them back into the error method to generate a JSON:API error document.
use Tobyz\JsonApiServer\JsonApi;
$api = new JsonApi('/api');
/** @var Psr\Http\Message\ServerRequestInterface $request */
/** @var Psr\Http\Message\ResponseInterface $response */
try {
$response = $api->handle($request);
} catch (Exception $e) {
$response = $api->error($e);
}
TIP
In Laravel, you'll need to convert the Laravel request into a PSR-7 request (opens new window) before you can pass it into JsonApi. You can then return the response directly from the route or controller – the framework will automatically convert it back into a Laravel response and display it.
# Authentication
You (or your framework) are responsible for performing authentication.
Often you will need to access information about the authenticated user inside of your schema – for example, when scoping which resources a visible within the API. An effective way to pass on this information is by setting an attribute on your Request object before passing it into the request handler.
$request = $request->withAttribute('user', $user);
← Installation Adapters →