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…
You can generate Laravel API documentation directly from your routes, validation rules, controller return types, and API Resources instead of writing PHPDoc or OpenAPI annotations on every endpoint. Annotations are extra comments you embed in code to describe an endpoint, and an OpenAPI specification is a machine-readable file that lists an API’s routes, inputs, outputs, and authentication rules.
Scramble is a practical place to start. It reads your Laravel code to describe most of the API, serves an interactive documentation page, and exports an OpenAPI file. You avoid repeating yourself in comments, though Scramble cannot explain business intent or guarantee that every inferred detail matches how your API behaves at runtime. You still need to review the output and add context where the code leaves questions unanswered.
When your Laravel code states its structure clearly, a documentation generator can read those details directly. Routes supply the paths and HTTP methods. Form Requests, which are Laravel classes that validate incoming requests, show what input an endpoint accepts through their validation rules. Controller return types and API Resources, which map models to response fields, describe what the endpoint sends back. Middleware, the code that runs as Laravel handles a request, signals which routes require authentication.
Scramble relies on static code analysis, meaning it reads your source files without sending HTTP requests to your endpoints. It documents route parameters, request validation rules, and many response shapes straight from your controllers and resources, as outlined in its overview of how inference works.
The more clearly your code shows what an endpoint accepts and returns, the less manual documentation you have to write. A Form Request, for instance, can spell out the fields required to create a post:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePostRequest extends FormRequest
{
public function rules(): array
{
return [
'title' => ['required', 'string', 'max:200'],
'body' => ['required', 'string'],
];
}
}
An API Resource makes the response fields explicit:
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class PostResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
];
}
}
When a controller validates input with that Form Request and returns the Resource, Scramble can trace both sides of the endpoint:
namespace App\Http\Controllers;
use App\Http\Requests\StorePostRequest;
use App\Http\Resources\PostResource;
use App\Models\Post;
class PostController
{
public function store(StorePostRequest $request): PostResource
{
$post = Post::create($request->validated());
return new PostResource($post);
}
}
A route like Route::post('/posts', [PostController::class, 'store']); ties the URL to the controller. Scramble then infers the route, the validated input, and the response fields. Its request documentation lists the route, request, and validation signals it recognizes.
If your main goal is an OpenAPI specification generated from Laravel code with minimal per-endpoint comments, start with Scramble. Its installation guide requires PHP 8.1 or higher and Laravel 10 or higher. Install it with Composer:
composer require dedoc/scramble
Once installed, Scramble serves an interactive documentation page at /docs/api and a JSON specification at /docs/api.json. By default, Scramble makes those routes available only in the local environment. Before you expose the UI in staging or production, decide who should see it and define the viewApiDocs gate to grant them access. The installation and setup instructions show how to configure that gate.
To save the specification as a file for a release or an external documentation host, run:
php artisan scramble:export --path=public/api.json
Scramble writes an OpenAPI 3.1.0 document. You can pass a custom output path, and if your project registers multiple APIs, you can pick one with the --api option. Your CI pipeline can run this command during builds to generate the file and surface schema changes alongside code changes. See the export documentation for the full list of command options.
Check which routes Scramble includes before you publish the file. Scramble filters routes by a configured API path, which defaults to routes starting with api. Look through the generated endpoint list to make sure internal, administrative, or health-check routes are left out when you do not want public readers to see them.
Verify authentication settings as well. Scramble’s authentication detection is opt-in. When you turn it on, its default middleware strategy recognizes auth and auth:*, so you will need to configure custom middleware or custom authentication schemes yourself. Open the generated OpenAPI file and check that protected routes list the credentials clients actually need. The authentication setup covers how to configure this.
When Scramble finishes exporting, you have a specification file, though that alone does not prove every endpoint is accurate. Compare representative routes against what clients receive in practice. Check required and optional inputs, response fields, pagination, error status codes, file uploads, and authentication.
Some details live only in product decisions or domain rules. Your code might show that a field is a string without explaining what values are valid. Add descriptions selectively in those places instead of annotating every endpoint. Keep Scramble’s limits in mind as well: it cannot translate certain complex PHP regular expressions into the JSON Schema format OpenAPI uses, and it struggles to infer dynamic response shapes. Inspect those rules and responses in the exported file.
You can run the export command in CI whenever related code changes, then have a reviewer inspect the diff. That catches route and schema changes early and keeps the documentation in sync with the codebase. Keep your automated tests in place as well, since tests verify runtime behavior while the OpenAPI file describes the contract clients rely on.
If request and response examples are central to your documentation, consider Scribe. Scribe can build a static site, a view served by Laravel, an OpenAPI specification, and a Postman collection. You can run it without annotations, because it can infer request details from Form Requests and validation rules, and you can still add annotations later when you want to refine the output. See the Scribe introduction for an overview.
Scribe can also simulate requests to capture real response examples. By default, it makes those calls only for GET endpoints and tries to wrap them in a database transaction that it rolls back afterward. Because Scribe executes your application code during these calls, any endpoint that talks to external services or triggers side effects needs a safe environment. Point Scribe at dummy integrations and non-production data instead of live services. Scribe’s response documentation explains how this works and what safeguards are available.
Use Scramble when you want an inferred OpenAPI file and your Laravel code already expresses routes, validation, and response shapes clearly. Use Scribe when you need a reader-facing documentation site with generated examples and you can safely run its simulated requests. With either tool, you still need to document any behavior that your code does not make obvious.
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