DEV Community

Cover image for Automatically Generate Bruno API Collections from Your Symfony Routes
Tim Nelles
Tim Nelles

Posted on

Automatically Generate Bruno API Collections from Your Symfony Routes

Photo by Lucas Santos on Unsplash

Introduction

Bruno is a great and open-source API client designed for efficient testing and collaboration. However, setting up API collections manually can be tedious, especially when dealing with large Symfony applications. Wouldn’t it be great if you could generate Bruno collections automatically from your Symfony routes? This is exactly what opctim/symfony-bruno-generator does.

In this article, we’ll explore how this Symfony bundle can streamline API collection generation, ensuring consistency across projects while saving valuable development time.

Features

opctim/symfony-bruno-generator is a Symfony bundle built using opctim/bruno-lang, a framework-agnostic bru-lang implementation for handling Bruno collections programmatically. This bundle automatically scans your controllers, extracts route information, and generates a Bruno collection in a standardized format.

  • Automatic Route Extraction: Detects and processes all defined Symfony routes.

  • Minimal User Input: Guides you through generating API collections interactively.

  • Customizable Base URL: Allows flexible API testing environments.

Installation

To start using symfony-bruno-generator, install the package via Composer:

composer require --dev opctim/symfony-bruno-generator
Enter fullscreen mode Exit fullscreen mode

Then, register the bundle in config/bundles.php (if you’re not using symfony/flex):

return [
    Opctim\BrunoGeneratorBundle\OpctimBrunoGeneratorBundle::class => ['dev' => true],
];
Enter fullscreen mode Exit fullscreen mode

Usage

Generating a Bruno collection is as simple as running the following command:

php bin/console make:bruno
Enter fullscreen mode Exit fullscreen mode

This command:

  1. Scans all controllers in your Symfony project (below the App\ namespace)

  2. Extracts available routes and methods (GET, POST, etc.)

  3. Writes request files into a structured Bruno collection (retains the controller folder structure in bruno)

Interactive CLI Workflow

During execution, the command will:

  • Check for an existing Bruno collection

  • Offer to create a new collection if none exists

  • Ask for a collection name and base URL

  • List detected controllers

  • Prompt you to confirm which requests should be generated

Here’s an example of the output:

Found bruno collection "my_collection" at /bruno

Do you want to generate 5 requests for the UserController? (yes/no) [yes]:
✔ Generated

  • GET {{baseUrl}}/api/users -> bruno/user/get_api_users.bru
  • POST {{baseUrl}}/api/users -> bruno/user/post_api_users.bru
Enter fullscreen mode Exit fullscreen mode

Future Improvements

While the current version efficiently extracts routes and generates basic requests, future enhancements include:

  • Automatic detection of request body structures.

  • Extraction of query parameters for GET requests.

  • Support for API Platform metadata.

  • Custom header configurations for authentication.

Conclusion

With opctim/symfony-bruno-generator, you no longer need to manually maintain your API collection. This package allows Symfony developers to generate Bruno requests effortlessly, ensuring consistency and reducing setup time.

If you’re interested in making your API development workflow smoother, give my package a try. You can also explore opctim/bruno-lang if you need deeper control over Bruno collections.

https://github.com/opctim/symfony-bruno-generator

https://github.com/opctim/bruno-lang

🚀 Happy generating!

Top comments (0)