The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

How to document multiple APIs in Laravel with Scramble

Published on by

How to document multiple APIs in Laravel with Scramble image

A single Laravel application can contain multiple APIs. This is useful for:

  • Different API versions
  • Public and internal APIs
  • APIs for frontend and backend consumption

With Scramble, you can create separate documentation for each API.

By default, Scramble registers an API called "default" and includes all endpoints where the URI starts with api/.

To include additional APIs, you simply register them, configure them, and expose their documentation.

Example: documenting multiple API versions

Consider an application with two API versions:

// routes/api.php
Route::prefix('v1')->group(function () {
// v1 routes
});
 
Route::prefix('v2')->group(function () {
// v2 routes
});

We’ll configure the default API documentation to document version 1 (v1) first by updating the config to use api/v1 as the path prefix:

// config/scramble.php
...
'api_path' => 'api/v1',
...

Version 2 (v2) must be registered explicitly. This is done by calling registerApi in a service provider's boot method and providing specific configuration overrides for v2. The first parameter is the API name (which can be any string), and the second parameter is the Scramble configuration overrides applied on top of the default configuration (config/scramble.php).

In this example, the registered API named v2 (this is just our choice; it could be internalpublic, etc.) will use the default configuration except for api_path, which is explicitly set for v2:

// app/Providers/AppServiceProvider.php
public function boot()
{
Scramble::registerApi('v2', ['api_path' => 'api/v2']);
}

We also need to register the documentation routes for v2. This is done by calling registerUiRoute and registerJsonSpecificationRoute, passing the desired documentation path as the first parameter and the API name (as used in registerApi) as the second parameter:

// routes/web.php
Scramble::registerUiRoute('docs/v2', api: 'v2');
Scramble::registerJsonSpecificationRoute('docs/v2/api.json', api: 'v2');

Now, we have documentation available for both APIs:

v1 documentation:

  • GET docs/api - UI for viewing v1 documentation
  • GET docs/api.json - OpenAPI 3.1.0 document for v1

v2 documentation:

  • GET docs/v2 - UI for viewing v2 documentation
  • GET docs/v2/api.json - OpenAPI 3.1.0 document for v2

Controlling documentation access

If some API versions should be public while others remain private, you can explicitly configure middleware for their documentation routes. This is useful when handling both public and internal APIs.

By default, documentation routes are only available in non-production environments. This logic is enforced by the RestrictedDocsAccess middleware. If you want v1 to be public and v2 restricted to non-production environments, remove RestrictedDocsAccess from v1 and add it to v2.

Making v1 public

Remove RestrictedDocsAccess from the default configuration:

// config/scramble.php
...
'middleware' => [
'web',
- RestrictedDocsAccess::class,
],
...

Restricting v2

Explicitly enable RestrictedDocsAccess for v2:

// app/Providers/AppServiceProvider.php
use Dedoc\Scramble\Http\Middleware\RestrictedDocsAccess;
 
public function boot()
{
Scramble::registerApi('v2', [
'api_path' => 'api/v2',
+ 'middleware' => [
+ 'web',
+ RestrictedDocsAccess::class,
+ ],
]);
}

Now, v1 documentation will be publicly accessible in production, while v2 will be restricted to non-production environments.

Customizing v1 documentation routes

You can also customize the default documentation routes (GET docs/apiGET docs/api.json). To do this, disable default route registration in the service provider's register method:

// app/Providers/AppServiceProvider.php
public function register()
{
Scramble::disableDefaultRoutes();
}

Then, manually register routes for the default API:

// routes/api.php
Scramble::registerUiRoute('docs/v1', api: 'default');
Scramble::registerJsonSpecificationRoute('docs/v1/api.json', api: 'default');

Conclusion

Using Scramble, you can document multiple APIs within a single Laravel application — whether for different versions, public vs. internal access, or distinct client needs. You have full control over routes, middleware, and other configurations for each API.

Give Scramble a try if you haven't already: https://scramble.dedoc.co

Roman Lytvynenko photo

Working on https://scramble.dedoc.co – Modern Laravel OpenAPI (Swagger) documentation generator.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Jump24 - UK Laravel Agency

Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Visit Jump24 - UK Laravel Agency
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
PestPHP Intellisense in Laravel VS Code Extension v1.7.0 image

PestPHP Intellisense in Laravel VS Code Extension v1.7.0

Read article
Drop in comments for Filament with Commentions image

Drop in comments for Filament with Commentions

Read article
Laravel Starter Kits Now Include Toast Notifications image

Laravel Starter Kits Now Include Toast Notifications

Read article
Ship AI with Laravel: Stop Your AI Agent from Guessing image

Ship AI with Laravel: Stop Your AI Agent from Guessing

Read article
Laravel Cloud Adds Path Blocking to Prevent Bots From Waking Hibernated Apps image

Laravel Cloud Adds Path Blocking to Prevent Bots From Waking Hibernated Apps

Read article
Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions image

Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions

Read article