Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

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
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
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
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

The latest

View all →
Encrypt Files in Laravel with AES-256-GCM and Memory-Efficient Streaming image

Encrypt Files in Laravel with AES-256-GCM and Memory-Efficient Streaming

Read article
Statamic 6 Is Officially Released image

Statamic 6 Is Officially Released

Read article
Livewire 4 and Blade Improvements in Laravel VS Code Extension v1.5.0 image

Livewire 4 and Blade Improvements in Laravel VS Code Extension v1.5.0

Read article
Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension image

Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension

Read article
NativePHP for Mobile Is Now Free image

NativePHP for Mobile Is Now Free

Read article
Fuse for Laravel: A Circuit Breaker Package for Queue Jobs image

Fuse for Laravel: A Circuit Breaker Package for Queue Jobs

Read article