Tips on how to Use an LLM-Powered Boilerplate for Constructing Your Personal Node.js API


For a very long time, one of many widespread methods to start out new Node.js initiatives was utilizing boilerplate templates. These templates assist builders reuse acquainted code constructions and implement customary options, similar to entry to cloud file storage. With the most recent developments in LLM, mission boilerplates look like extra helpful than ever.

Constructing on this progress, I’ve prolonged my present Node.js API boilerplate with a brand new software LLM Codegen. This standalone function allows the boilerplate to mechanically generate module code for any objective primarily based on textual content descriptions. The generated module comes full with E2E assessments, database migrations, seed information, and mandatory enterprise logic.

Historical past

I initially created a GitHub repository for a Node.js API boilerplate to consolidate the most effective practices I’ve developed through the years. A lot of the implementation is predicated on code from an actual Node.js API working in manufacturing on AWS.

I’m enthusiastic about vertical slicing structure and Clear Code ideas to maintain the codebase maintainable and clear. With latest developments in LLM, significantly its assist for big contexts and its capability to generate high-quality code, I made a decision to experiment with producing clear TypeScript code primarily based on my boilerplate. This boilerplate follows particular constructions and patterns that I imagine are of top quality. The important thing query was whether or not the generated code would comply with the identical patterns and construction. Primarily based on my findings, it does.

To recap, right here’s a fast spotlight of the Node.js API boilerplate’s key options:

  • Vertical slicing structure primarily based on DDD & MVC ideas
  • Providers enter validation utilizing ZOD
  • Decoupling utility elements with dependency injection (InversifyJS)
  • Integration and E2E testing with Supertest
  • Multi-service setup utilizing Dockercompose

Over the previous month, I’ve spent my weekends formalizing the answer and implementing the mandatory code-generation logic. Under, I’ll share the main points.

Implementation Overview

Let’s discover the specifics of the implementation. All Code Era logic is organized on the mission root degree, contained in the llm-codegen folder, making certain simple navigation. The Node.js boilerplate code has no dependency on llm-codegen, so it may be used as a daily template with out modification.

LLM-Codegen folder construction

It covers the next use instances:

  • Producing clear, well-structured code for brand spanking new module primarily based on enter description. The generated module turns into a part of the Node.js REST API utility.
  • Creating database migrations and increasing seed scripts with primary information for the brand new module.
  • Producing and fixing E2E assessments for the brand new code and making certain all assessments move.

The generated code after the primary stage is clear and adheres to vertical slicing structure ideas. It consists of solely the mandatory enterprise logic for CRUD operations. In comparison with different code technology approaches, it produces clear, maintainable, and compilable code with legitimate E2E assessments.

The second use case entails producing DB migration with the suitable schema and updating the seed script with the mandatory information. This activity is especially well-suited for LLM, which handles it exceptionally nicely.

The ultimate use case is producing E2E assessments, which assist verify that the generated code works accurately. In the course of the working of E2E assessments, an SQLite3 database is used for migrations and seeds.

Primarily supported LLM purchasers are OpenAI and Claude.

Tips on how to Use It

To get began, navigate to the basis folder llm-codegen and set up all dependencies by working:

npm i

llm-codegen doesn’t depend on Docker or some other heavy third-party dependencies, making setup and execution simple and easy. Earlier than working the software, be sure that you set no less than one *_API_KEY atmosphere variable within the .env file with the suitable API key to your chosen LLM supplier. All supported atmosphere variables are listed within the .env.pattern file (OPENAI_API_KEY, CLAUDE_API_KEY and so on.) You should use OpenAIAnthropic Claude, or OpenRouter LLaMA. As of mid-December, OpenRouter LLaMA is surprisingly free to make use of. It’s attainable to register right here and acquire a token without cost utilization. Nevertheless, the output high quality of this free LLaMA mannequin could possibly be improved, as a lot of the generated code fails to move the compilation stage.

To begin llm-codegen, run the next command:

npm run begin

Subsequent, you’ll be requested to enter the module description and identify. Within the module description, you possibly can specify all mandatory necessities, similar to entity attributes and required operations. The core remaining work is carried out by micro-agents: DeveloperTroubleshooter, and TestsFixer.

Right here is an instance of a profitable code technology:

Profitable code technology

Under is one other instance demonstrating how a compilation error was mounted:

The next is an instance of a generated orders module code:

A key element is which you could generate code step-by-step, beginning with one module and including others till all required APIs are full. This method permits you to generate code for all required modules in just some command runs.

How It Works

As talked about earlier, all work is carried out by these micro-agents: DeveloperTroubleshooter and TestsFixer, managed by the Orchestrator. They run within the listed order, with the Developer producing a lot of the codebase. After every code technology step, a test is carried out for lacking information primarily based on their roles (e.g., routes, controllers, companies). If any information are lacking, a brand new code technology try is made, together with directions within the immediate in regards to the lacking information and examples for every function. As soon as the Developer completes its work, TypeScript compilation begins. If any errors are discovered, the Troubleshooter takes over, passing the errors to the immediate and ready for the corrected code. Lastly, when the compilation succeeds, E2E assessments are run. At any time when a check fails, the TestsFixer steps in with particular immediate directions, making certain all assessments move and the code stays clear.

All micro-agents are derived from the BaseAgent class and actively reuse its base methodology implementations. Right here is the Developer implementation for reference:

Every agent makes use of its particular immediate. Take a look at this GitHub hyperlink for the immediate utilized by the Developer.

After dedicating vital effort to analysis and testing, I refined the prompts for all micro-agents, leading to clear, well-structured code with only a few points.

In the course of the growth and testing, it was used with numerous module descriptions, starting from easy to extremely detailed. Listed below are just a few examples:

- The module chargeable for library e-book administration should deal with endpoints for CRUD operations on books.
- The module chargeable for the orders administration. It should present CRUD operations for dealing with buyer orders. Customers can create new orders, learn order particulars, replace order statuses or info, and delete orders which are canceled or accomplished. Order will need to have subsequent attributes: identify, standing, positioned supply, description, picture url
- Asset Administration System with an "Belongings" module providing CRUD operations for firm belongings. Customers can add new belongings to the stock, learn asset particulars, replace info similar to upkeep schedules or asset areas, and delete information of disposed or offered belongings.

Testing with gpt-4o-mini and claude-3-5-sonnet-20241022 confirmed comparable output code high quality, though Sonnet is dearer. Claude Haiku (claude-3–5-haiku-20241022), whereas cheaper and related in worth to gpt-4o-mini, usually produces non-compilable code. Total, with gpt-4o-mini, a single code technology session consumes a median of round 11k enter tokens and 15k output tokens. This quantities to a price of roughly 2 cents per session, primarily based on token pricing of 15 cents per 1M enter tokens and 60 cents per 1M output tokens (as of December 2024).

Under are Anthropic utilization logs exhibiting token consumption:

Primarily based on my experimentation over the previous few weeks, I conclude that whereas there should be some points with passing generated assessments, 95% of the time generated code is compilable and runnable.

I hope you discovered some inspiration right here and that it serves as a place to begin to your subsequent Node.js API or an improve to your present mission. Ought to you’ve gotten options for enhancements, be happy to contribute by submitting PR for code or immediate updates.

When you loved this text, be happy to clap or share your ideas within the feedback, whether or not concepts or questions. Thanks for studying, and blissful experimenting!

UPDATE [February 9, 2025]: The LLM-Codegen GitHub repository was up to date with DeepSeek API assist. It’s cheaper than gpt-4o-mini and affords practically the identical output high quality, however it has an extended response time and typically struggles with API request errors.

Until in any other case famous, all photographs are by the writer