See rates for the top Laravel developers in Latin America

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
Laravel Code Review

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

Visit Laravel Code Review
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
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
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
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
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
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
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 →
Laravel 12.45.1, 12.45.2, and 12.46.0 Released image

Laravel 12.45.1, 12.45.2, and 12.46.0 Released

Read article
"Don't Remember" Form Helper Added in Inertia.js 2.3.7 image

"Don't Remember" Form Helper Added in Inertia.js 2.3.7

Read article
Fast Laravel Course Launch image

Fast Laravel Course Launch

Read article
Laravel News Partners With Laracon India image

Laravel News Partners With Laracon India

Read article
A new beta of Laravel Wayfinder just dropped image

A new beta of Laravel Wayfinder just dropped

Read article
Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026 image

Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026

Read article