Laravel Idea for PhpStorm - Full-featured IDE for productive artisans!

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.

Curotec logo

Curotec

Hire the top Latin American Laravel talent. Flexible engagements, budget optimized, and quality engineering.

Curotec
Bacancy logo

Bacancy

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

Bacancy
Laravel Forge logo

Laravel Forge

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

Laravel Forge
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
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
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
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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
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 →
Connecting Laravel Socialite with Google Client PHP Library image

Connecting Laravel Socialite with Google Client PHP Library

Read article
Create Self-Contained PHP Executables with PHPacker image

Create Self-Contained PHP Executables with PHPacker

Read article
String Manipulation with Laravel's remove Method image

String Manipulation with Laravel's remove Method

Read article
Stargazing in Your Laravel App with Intervention Zodiac image

Stargazing in Your Laravel App with Intervention Zodiac

Read article
Simple Cookie Consent Packge for Laravel image

Simple Cookie Consent Packge for Laravel

Read article
Leveraging Laravel's Conditional Ping Methods for Task Monitoring image

Leveraging Laravel's Conditional Ping Methods for Task Monitoring

Read article