# Generate OpenApi at build time?

**URL:** <https://softwaremill.community/t/generate-openapi-at-build-time/344>\
**Category:** tapir\
**Created:** [December 31, 2023, 7:21am UTC](https://softwaremill.community/t/generate-openapi-at-build-time/344 "2023-12-31T07:21:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arashi01](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/arashi01/32/225_2.png) [@arashi01](https://softwaremill.community/u/arashi01)\
**Post date:** [December 31, 2023, 7:21am UTC](https://softwaremill.community/t/generate-openapi-at-build-time/344/1 "2023-12-31T07:21:39Z")

</div>

Hi all. Is it possible to generate OpenApi Yaml/JSON as part of an sbt build process? Anyone have any examples of this?

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [December 31, 2023, 9:21am UTC](https://softwaremill.community/t/generate-openapi-at-build-time/344/2 "2023-12-31T09:21:35Z")

</div>

I don’t have an example at hand, but you should invoke sbt’s `run` or `runMain` (or some other task that runs your code) with a dedicated “main” class which generates the openapi file and writes it to the provided path. You could probably do this after compilation or before packaging.

---

<div class="post-metadata">

**Author:** ![frozenwizard](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@frozenwizard](https://softwaremill.community/u/frozenwizard)\
**Post date:** [January 2, 2024, 8:07pm UTC](https://softwaremill.community/t/generate-openapi-at-build-time/344/3 "2024-01-02T20:07:52Z")

</div>

I have an sbt task defined as

```auto
generateOpenApiDoc :=
  (Runtime / runMain)
    .toTask("path.to.OpenApiGenerator")
    .value

```

that gets invoked with `sbt generateOpenApiDoc`

OpenApiGenerator mainly consists of

```auto
val openApiDocs: OpenAPI = OpenAPIDocsInterpreter().serverEndpointsToOpenAPI().openapi("3.0.3")

Files.write(Paths.get(targetOutputPath), openApiDocs.toYaml3_0_3.getBytes(StandardCharsets.UTF_8))

```

---

<div class="post-metadata">

**Author:** ![hochgi](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/hochgi/32/32_2.png) [@hochgi](https://softwaremill.community/u/hochgi)\
**Post date:** [January 6, 2024, 4:40pm UTC](https://softwaremill.community/t/generate-openapi-at-build-time/344/4 "2024-01-06T16:40:24Z")

</div>

I have a complete example repo you can take a look at:

> **[GitHub - hochgi/tapir-zervice: template project + experiments](https://github.com/hochgi/tapir-zervice/)**
>
> template project + experiments. Contribute to hochgi/tapir-zervice development by creating an account on GitHub.

the `matapi` (materialize API) module is the one you want to look at.  
It’s all explained in the README, but LMK if you have any questions 🙂

---

<div class="post-metadata">

**Author:** ![andreazube-eu](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/andreazube-eu/32/233_2.png) [@andreazube-eu](https://softwaremill.community/u/andreazube-eu)\
**Post date:** [January 17, 2024, 2:05pm UTC](https://softwaremill.community/t/generate-openapi-at-build-time/344/5 "2024-01-17T14:05:05Z")

</div>

As an alternative, what I’ve seen doing and working quite well is having a “test” that generates the openapi and writes it down.

That way the spec is updated on every pipeline run, but not at every compile.  
If you want, you can even check if the output is the same, and fail the test if it’s not (so a sort of `overWrite` parameter).
