Customers & growth · Developer marketing

Code generation strategy

Best for Startup to Growth

1SPEC2GENERATE3SHIP

Use it when

You need client libraries in several languages for an API that changes often, and want them built from one API description instead of written by hand.

Skip it when

  • You need polished libraries in a few key languages, where idiomatic design matters more than coverage. Use SDK strategy framework instead.
  • Developers need a working starter project more than a client library. Use Starter templates.
  • Your API has no complete, accurate machine-readable description yet. Settle the design with API design principles first.

What to apply first and what to use instead are listed below.

Approaches

Fully generated libraries

A generator builds each language's library from the API description, and every customization lives in generator config and templates.

  • Pro New endpoints reach every language as soon as the description changes.
  • Con The code can feel generic in some languages until the templates are tuned.

Generated core with a hand-written layer

The generator builds the low-level client, and a small hand-written layer adds convenience methods in files the generator never overwrites.

  • Pro Developers get idiomatic helpers while most of the code still regenerates.
  • Con The hand-written layer must be updated and tested each time the generated core changes.

Description published for users to generate

You publish the API description, and developers generate their own clients with open tools such as OpenAPI Generator or Kiota.

  • Pro It reaches any language without you maintaining libraries.
  • Con Each developer runs and updates their own generator, and quality varies by tool.

Inputs

  • API description
    The complete OpenAPI or other machine-readable description of the API, kept in version control.
  • Target languages
    The languages to support, ranked by the share of your developers who use each, from signup data or a developer survey.
  • Generator
    The tool and version for each language, such as OpenAPI Generator or Kiota, with its config file.
  • Language conventions
    Naming, error handling and packaging rules for each language, from that language's style guides.
  • Hand-written codeOptional
    Helpers and examples that must survive regeneration, listed by file.

Steps

  1. Check that the API description covers every endpoint, field and error, and fix the gaps in the description before generating.
  2. Choose the approach and a generator for each target language, and pin each generator's version.
  3. Set package names and naming options in each generator's config file, and change templates only where config falls short.
  4. List every hand-written file in the generator's ignore file or move it into a separate layer, so regeneration never overwrites it.
  5. Regenerate every library whenever the description changes, run each library's tests against a mock or live API, and publish to each language's package registry.

Output

A code generation plan and one generated library per target language, all built from the same description version.

  • Target language and package name
  • Generator and pinned version
  • Config file and changed templates
  • Hand-written files protected from regeneration
  • Description version each library was built from
  • Test result against the API
  • Registry and release version

Done when

  • Every target language has a pinned generator version and a config file in version control.
  • Every library in the latest release was built from the same description version.
  • No generated file carries a hand edit, and every hand-written file is listed as protected.
  • Each library passed its tests against the API before publishing.

Measure

Track the time from an API change to its release in every library, the share of endpoints each library covers, and issues filed against each library. Review per release for two quarters. It worked when new endpoints reach every language within your target lag and library issues fall against the period before generation began. Watch issues per language to spot templates that need tuning.

Hypothetical example

Ravelin Pay, a payments API, kept five hand-written libraries, and new endpoints took months to reach all of them. The team chose a generated core with a hand-written layer. They fixed 30 gaps in their OpenAPI description, pinned one generator version per language and set package names in config files. Paging helpers stayed hand-written and were listed in the ignore file. The build regenerated all five libraries on each description change, ran tests against a mock server and published them. The next new endpoint reached all five package registries in two days.

Common mistakes

  • Editing generated files by hand. Put changes in config, templates or a protected hand-written layer, or the next run erases them.
  • Generating from an incomplete description. Fix missing fields and errors in the description first, since every library copies its gaps.
  • Leaving the generator version unpinned. Pin it, since a new generator release can change the output of every library.
  • Giving every language the same style. Set each language's naming and packaging to its own conventions.

Sources

Show all 5 sources

By company stage

  1. Pre-IdeaNot applicableThere is no API or developer audience yet, so nothing exists to generate libraries from.
  2. IdeaNot applicableThe API is still changing and the few customers rarely need many languages, so hand-written examples suffice.
  3. Early StartupUsefulA stable API description helps early developers, though coverage across many languages is rarely the main constraint yet.
  4. StartupCriticalHundreds of customers use varied languages and the API changes often, so generated libraries scale developer reach cheaply.
  5. GrowthCriticalMany customers across regions need broad language coverage, and generation keeps libraries in step with frequent API releases.
  6. ScaleUsefulGeneration still covers long tail languages, though key languages often move to polished hand-crafted SDKs.
  7. EnterpriseUsefulLarge API portfolios benefit from generated libraries, but established teams usually run mature SDK programs already.

Apply first

Nothing. You can start here.

Use instead

  • SDK strategy framework

    use code generation when you must support many languages and the API changes often; use hand-built SDKs when idiomatic developer experience in a few key languages matters more.

  • Starter templates

    Developers need a working starter project more than a client library. Use Starter templates.

  • API design principles

    Your API has no complete, accurate machine-readable description yet. Settle the design with API design principles first.

Recommended resources

No sponsored resources on this framework yet. Sponsored listings would be labelled here, never in search.

Progress is not saved until you have an account. Create a free account