Fully automated API documentation generation for Laravel
August 8, 2024
Learn how to get fully automated API documentation (without PHPDoc annotations required) for your Laravel app with Scramble.
Recently I was listening to Laravel podcast, where Matt and Taylor discussed API documentation generation. Matt said that there are 2 ways of generating API documentation: you either write PHPDoc annotations in your code which makes it cluttered, or you write and maintain API spec file manually yourself which is tedious and may result in outdated documentation.
I’m writing this post to show that there is another way to have API documentation: using static code analysis. This allows having fully automated API documentation (without PHPDoc annotations required), always up-to-date with your codebase.
Meet Scramble
Let me introduce you Scramble: the library for API documentation generation for Laravel which doesn’t require you to write any PHPDoc annotations.
Scramble works by using static code analysis. It infers the types across the codebase and based on that it can tell how requests or responses look like.
The API we’ll make a documentation for
To keep this post short, this API is a very simple – this is a small booking management system where user can manage their bookings.
So here is the API we’ll document:
So before describing how to document the API and introducing you to Scramble, I’d like to describe the API implementation first, so we’re on the same page about what it does.
Places API
Places API allows to get the list of places available for bookings. We want to allow users to search the places using a bunch of filters, and give them ability to sort them.
We will have Place
model and invokable PlaceController
:
Our place resource will be a simple one for now, showing basic attributes of the place.
Bookings API
For bookings, we’ll have a Booking
model and BookingController
. For now it’ll be very simple controller, allowing users to list their bookings, create a booking, update a booking, or delete it.
When user creates or updates a booking, we’d want to know it the given time is available for a booking and if not – the API will reply with clear message about the issue of time is being not available.
So here is the controller:
BookingResource
is going to be as simple as possible for now as well:
Registering routes
Now, we’ll enable API routing using this command. This will save us few keystrokes and create API routes file with the setup we need. For auth we’ll use Sanctum, which also will be added when installing the API.
Now, we’ll define our endpoints in routes/api.php
Let’s now check the list of resulting API routes by running route:list
command.
Okay, so at this point we have few endpoints that implement our basic logic for booking API. Let’s document it so it can be consumed.
Documenting the API
First of all, install Scramble:
Now visit /docs/api
page.
Well, this is it! Our API documentation is 90% ready without any manual PHPDoc annotations!
Let’s push it to 100% by making sure everything is correct and adding some manual text descriptions where needed.
But before that, I’d like to reveal this magic so we’re on the same page why it worked.
How it works, in a nutshell
First of all, Scramble collects routes that are considered as API routes. By default these are routes which URI start with api/
prefix.
Then, once routes are collected, Scramble documents each route by documenting request body/query params, path params.
After the request part documented, Scramble’s type inference system kicks in: for every route’s controller method, Scramble analyzes what type is returned from it using static code analysis. Then, the returned types become documented as responses.
Ensuring response are correct
While Scramble has inferred most of types, there is a paginated response (GET api/places
) which need a manual annotation (relevant for 0.11.*
, but most likely will be improved starting from 0.12.*
).
So here is GET api/places
actual successful response:
To show it correctly in the documentation, we need to add manual annotation for endpoint’s controller method:
Now Scramble generates the documentation for this endpoint’s response taking into account this endpoint returns a paginated set of PlaceResource
:
Adding titles and descriptions to our endpoints
To make it simpler for users to consume the documentation we may also add titles and descriptions to our endpoints.
To do so, we simply add the text to the PHPDoc comment of the method.
And the resulting documentation:
Enhancing the documentation with extra features
Now, our documentation is almost ready. Let’s improve it further before calling it a day.
Adding authentication details
To add authentication details, we need to describe how our authentication works in terms of OpenAPI standard. To do so, we need to add a security scheme.
Sanctum auth’s API token is provided via Authentication
header.
To add this information to the documentation, we’ll define the default security scheme:
Now our documentation clearly communicates to consumers about how to authenticate:
You can learn more about the security documentation in the documentation: https://scramble.dedoc.co/usage/security
Grouping and organizing endpoints
Our endpoints are already grouped in a way that makes sense because to the controller base name used as a group for the endpoints.
In case we want to override that, we can use @tags
annotation on controller’s level to name a group of endpoints in a sensible way.
So to group all our endpoints under the same category called “Bookings management”, we simply add the annotation on the controller level:
And now we have all these endpoints in the same group:
While this definitely makes sense in some scenarios when grouping by controller’s name does not work, for our API the defaults perfectly makes sense, so we’ll stick with that.
Adding logo, title, API overview page
Finally, we’d want to finish our documentation by adding title, logo, and API overview page. To do so, publish the config file:
To specify the title and logo, we’ll define the scramble.ui.title
, and scramble.ui.logo
config keys:
For logo, I’ve created an SVG file in public/images
folder with an icon from Heroicons pack.
Now, to add an API overview page, we’ll define scramble.info.description
key. You can use string with markdown syntax here, but I like to get the content of some file, to keep config file short and sweet and edit the content in a markdown’s file.
And, here is the result:
Enabling access to documentation in production
By default, Scramble documentation will work in non-production environment only. To allow accessing documentation in production environment, you need to define viewApiDocs
gate – it describes the authorization behavior for production environment.
In our case we want to make our documentation fully public, so let’s do that. In a service provider we’ll define this gate:
Now, our documentation will be available on docs/api
in production for everyone to browse.
Conclusion
So as you can see, there is a third way of having API documentation: relying on static code analysis. This approach not only saves time but also ensures that your documentation is always up-to-date with the latest code changes.
This post touches only basics. You can learn more about Scramble here: https://scramble.dedoc.co/
Happy coding!