Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Laravel REST API Interview Questions and Answers

In a Laravel REST API interview, you need to explain how you design HTTP endpoints and how you use Laravel’s tools to build them. A REST API lets clients work with resources, such as users or orders, through URLs and standard HTTP methods. In a stateless API, the client includes everything the server needs inside each request, and the server handles it without relying on data stored from an earlier request.

The examples here use Laravel 13 as a baseline. Laravel 13 was released in March 2026 and requires PHP 8.3 or later. Laravel 12 remains in its security-fix window through February 24, 2027, though its bug-fix window has ended, so check the documentation for your version before relying on a version-specific detail. The questions below walk through API design choices and connect them to Laravel’s routing, validation, authentication, response, and testing features.

API design and routing

1. What makes an API RESTful, and what does stateless mean?

A REST-style API exposes resources at URLs and uses HTTP methods to show what a client wants to do. A client can request a list of posts with GET /posts or create a new one with POST /posts. The server reads the URL, headers, and body of that request to handle it. The post itself still has state in your database; statelessness describes how the client and server communicate.

In everyday work, developers often say “REST API” to mean a JSON API that uses resource-based URLs and follows HTTP conventions. Strict REST adds further constraints, including hypermedia controls, which are links or instructions in a response that tell clients which actions they can take next. In an interview, state which meaning you have in mind. Returning JSON alone does not make an API RESTful. OWASP’s REST guidance covers resources, HTTP methods, status codes, and hypermedia in more detail.

2. How do you enable API routes in Laravel 13?

Run php artisan install:api to install Sanctum and create routes/api.php. Laravel loads stateless API routes from that file. Putting routes there gives your API a clear entry point, but you still have to define the rest of the contract: request validation, access rules, response formats, and status codes. Laravel covers the command and setup in its routing documentation.

A good interview answer separates route setup from API design. Laravel registers the endpoints for you, while you decide which records a user can access and which fields each endpoint returns.

3. How would you structure CRUD endpoints for a resource?

Put the resource name in the URL and use HTTP methods to show the action. For a posts resource, clients send GET /posts to list posts, POST /posts to create one, GET /posts/{post} to fetch one, PUT or PATCH /posts/{post} to update it, and DELETE /posts/{post} to remove it.

You can register those standard API routes in Laravel with Route::apiResource('posts', PostController::class);. That helper leaves out the browser-oriented create and edit routes. You still need to decide what each action accepts and returns, and check whether the user is allowed to access that specific post. Laravel’s router supports the standard HTTP methods and resource routes.

4. What is the difference between PUT and PATCH?

A PUT request replaces the target resource’s representation. HTTP defines PUT as idempotent, so sending the same request multiple times has the same intended effect as sending it once. A PATCH request usually updates only part of a resource. Your API needs to document what omitted fields mean and whether repeating a particular patch produces the same result.

For example, you might require clients to send every editable field on PUT, while allowing them to send only the changed fields on PATCH. You define that rule in your API contract because Laravel’s route helpers do not enforce it. RFC 9110 defines the HTTP semantics for PUT and PATCH.

Validation and responses

5. Why use Laravel API Resources instead of returning Eloquent models directly?

An Eloquent model represents your database data and can serialize to JSON, but returning it directly ties your API output to your database schema. An API Resource lets you choose the exact attributes and relationships to include in the response. That keeps your public response format stable when the underlying model changes, and it prevents accidental data exposure because you explicitly list each field.

For example, a controller can return PostResource::make($post) instead of returning the model directly. Laravel 13 also includes JsonApiResource if your API follows the JSON:API specification, though standard REST-style APIs do not require that format. You can find the details in Laravel’s API Resource documentation.

6. How does request validation work, and what response should a client expect?

Laravel checks incoming data before your controller makes any changes. When the data fails validation and the client expects JSON, Laravel returns 422 Unprocessable Entity along with the error details. Clients should send Accept: application/json so Laravel knows to return JSON instead of redirecting. Document your exact error format so clients know what to expect.

You can validate input with a Form Request class or with validation logic inside the controller. Keep in mind that validation only checks whether the input meets the endpoint’s rules. It does not check whether the caller has permission to modify the target record. Laravel’s validation documentation covers this behavior and its JSON responses.

7. How should an API handle errors and HTTP status codes?

Return a status code that matches what happened, paired with a consistent response body that software clients can parse. Send a client-error status when a request is invalid or unauthorized, and a server-error status when the server fails unexpectedly. Never send stack traces or internal implementation details to clients.

Laravel’s error renderer checks the request’s Accept header to decide whether to return JSON or HTML, and you can customize how it renders exceptions. Test and document your API’s error format instead of assuming every Laravel project returns the same JSON structure. Laravel explains this in its error-handling documentation. OWASP also recommends generic client-facing messages for internal failures and points to 429 Too Many Requests for throttled requests.

Authentication and security

8. What is the difference between authentication and authorization?

Authentication identifies who is making the request. Authorization checks whether that user is allowed to perform a specific action on a specific resource. Confirming that a user is signed in does not mean they can update a particular post.

In an interview, explain that your API must check permissions on every protected request, including access to individual records. A route can require users to sign in and still expose another user’s record if your code does not check ownership or apply a policy. OWASP recommends access control for every non-public endpoint.

9. When would you choose Sanctum instead of Passport?

Choose Sanctum for first-party SPAs, mobile clients, and straightforward API-token setups. Choose Passport when your API needs full OAuth 2.0 server features. OAuth 2.0 is an authorization framework for granting clients delegated access; if your API only needs simple token authentication, adding Passport brings extra complexity without solving a real requirement. Laravel outlines these use cases in its authentication documentation.

Sanctum supports two authentication modes. A first-party single-page application (SPA) uses Laravel’s cookie-based session authentication and CSRF protection. Other clients send a bearer token in the Authorization header. You can use token abilities to restrict what a token is allowed to do, while your application policies still decide whether the authenticated user can act on a particular resource. Laravel covers these modes in its Sanctum documentation.

10. How would you apply rate limiting?

Rate limiting caps how many requests a client can make within a defined window. In Laravel, you attach rate-limiting middleware and choose what identifies the client, such as a user ID or API key. You can also set a separate limit by IP address. Which option you pick depends on how clients sign in and how they use the endpoint.

Set limits based on the work each endpoint does. Lock down login and account-recovery endpoints to block repeated guessing. Give resource-heavy search endpoints and endpoints that write many records their own limits. Return 429 Too Many Requests when a client exceeds its allowance. Keep in mind that counting requests alone does not cap the work a single expensive request can trigger. Laravel’s rate-limiting tools handle the throttling mechanics, while OWASP covers the related API security concerns.

Pagination and testing

11. How should a Laravel API paginate results?

Paginate collection endpoints so the server does not try to return an unlimited number of records in one response. Define how clients request another page, how many records each page holds, and what navigation metadata the response includes.

Laravel supports both page-based and cursor-based pagination. Use page-based pagination when clients need page numbers. Use cursor pagination for large or frequently changing collections, since it tracks a position in an ordered set of results. Pick the method that fits how clients use the endpoint, and include the pagination links and details in your response format. Laravel’s API Resource documentation covers paginated resource responses.

12. How do you test a Laravel API endpoint?

Write feature tests that send HTTP requests through Laravel to check the response status, JSON fields, and access rules. Test a successful request alongside failure cases: invalid input, a request from a user who is not signed in, and a signed-in user trying to reach another user’s record. That way you verify the full endpoint contract instead of only checking that the controller returns data.

Laravel provides JSON request helpers such as getJson, postJson, putJson, and patchJson. Your tests can use these helpers to inspect the response and confirm the result of a write. For example, a test that creates a record should check the returned status and response fields, then confirm that Laravel stored the expected record in the database. See Laravel’s HTTP testing documentation for the full set of assertions.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *