<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Roger Rodrigues</title>
    <description>The latest articles on DEV Community by Roger Rodrigues (@satulg).</description>
    <link>https://dev.to/satulg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2905311%2F1063917d-1970-45f7-b429-27aa39d409df.jpg</url>
      <title>DEV Community: Roger Rodrigues</title>
      <link>https://dev.to/satulg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/satulg"/>
    <language>en</language>
    <item>
      <title>How to customize Scramble – Laravel OpenAPI Docs</title>
      <dc:creator>Roger Rodrigues</dc:creator>
      <pubDate>Fri, 04 Apr 2025 18:06:33 +0000</pubDate>
      <link>https://dev.to/satulg/how-to-customize-scramble-laravel-openapi-docs-5bg5</link>
      <guid>https://dev.to/satulg/how-to-customize-scramble-laravel-openapi-docs-5bg5</guid>
      <description>&lt;p&gt;I tried to customize the documentation of my API and was so much difficult! &lt;/p&gt;

&lt;p&gt;I'm writing this article to help devs that would to customize / add new endpoints without the route in api.php. For example: my API have a webhook to the client. So, i would like to add the webhook documentation to the documentation and I achieved.&lt;/p&gt;

&lt;p&gt;First, lets create a Scramble DocumentTransformer:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;/app/Transformers/AddWebhookDocumentationTransformer.php&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Transformers;

use Dedoc\Scramble\Contracts\DocumentTransformer;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\Operation;
use Dedoc\Scramble\Support\Generator\Path;
use Dedoc\Scramble\Support\Generator\RequestBodyObject;
use Dedoc\Scramble\Support\Generator\Response;
use Dedoc\Scramble\Support\Generator\Schema;
use Dedoc\Scramble\Support\Generator\Types\ObjectType;
use Dedoc\Scramble\Support\Generator\Types\StringType;
use Dedoc\Scramble\Support\Generator\Types\IntegerType;
use Dedoc\Scramble\Support\Generator\Types\BooleanType;

class AddWebhookDocumentationTransformer implements DocumentTransformer
{
        public function handle(OpenApi $document, $context): void
    {
        $operation = (new Operation('post'));
        $operation-&amp;gt;operationId = 'webhookNotification';
        $operation-&amp;gt;security = [];
{% embed  %}
        $operation-&amp;gt;summary('Webhook Notification')
            -&amp;gt;description(
                'This is an example of the webhook sent by us after a transaction is completed. ' .
                    'The payload below represents the data sent to the URL configured by the client.'
            );

        // Webhook schema definition
        $schema = (new ObjectType())
            -&amp;gt;addProperty('payer_id', new IntegerType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Payer ID.'))
            -&amp;gt;addProperty('payer_name', new StringType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Payer name.'))
            -&amp;gt;addProperty('payer_document', new StringType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Payer document (CPF/CNPJ).'))
            -&amp;gt;addProperty('payer_email', new StringType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Payer email.'))
            -&amp;gt;addProperty('product_id', new StringType(), fn ($p) =&amp;gt; $p-&amp;gt;description('External product ID.'))
            -&amp;gt;addProperty('order_id', new StringType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Order ID.'))
            -&amp;gt;addProperty('status', new StringType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Transaction status (e.g., approved, declined).'))
            -&amp;gt;addProperty('total', new IntegerType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Total transaction amount in cents.'))
            -&amp;gt;addProperty('paid', new BooleanType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Indicates if the transaction was paid (true/false).'))
            -&amp;gt;addProperty('paid_at', new StringType(), fn ($p) =&amp;gt; $p-&amp;gt;description('Payment date and time (ISO 8601).'));

        // Adding the request body
        $operation-&amp;gt;addRequestBodyObject(
            RequestBodyObject::make()
                -&amp;gt;setContent('application/json', Schema::fromType($schema))
                -&amp;gt;description('Structure of the payload sent by the webhook')
        );

        // Adding the expected response
        $operation-&amp;gt;addResponse(
            (new Response(200))
                -&amp;gt;description('Webhook received successfully.')
        );

        // Creating the /webhook path
        $pathItem = Path::make('/webhook')
            -&amp;gt;addOperation($operation);

        // Adding the path to the document
        $document-&amp;gt;addPath($pathItem);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, we will register this transformer to the Scramble:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;/app/Providers/AppServiceProvider.php&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scramble::configure()
            -&amp;gt;routes(function (Route $route) {
                return Str::startsWith($route-&amp;gt;uri, 'api/');
            })
            -&amp;gt;withDocumentTransformers(AddWebhookDocumentationTransformer::class);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
