> For the complete documentation index, see [llms.txt](https://tonystrawberry.gitbook.io/rails-openapi-generator/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tonystrawberry.gitbook.io/rails-openapi-generator/guides/programmatic-use.md).

# Programmatic Use

The generator exposes a Ruby API for use outside of rake or CLI — useful for testing, tooling integration, or dynamic spec serving.

## Generate and write to disk

```ruby
require "rails_openapi_generator"

config = RailsOpenapiGenerator::Configuration.new
config.output_path = "doc/openapi.json"
config.title       = "My API"
config.api_version = "1.0.0"

report = RailsOpenapiGenerator::Generator.new(config).generate
```

`generate` writes the file to `config.output_path` and returns a `GenerationReport`.

## Get the document in memory

```ruby
document = RailsOpenapiGenerator::Generator.new(config).document
# => Hash — the full OpenAPI 3.1 document, not serialized
```

Use this to post-process the document before writing:

```ruby
doc = RailsOpenapiGenerator::Generator.new(config).document

# Add a security scheme
doc["components"] ||= {}
doc["components"]["securitySchemes"] = {
  "bearerAuth" => {
    "type" => "http",
    "scheme" => "bearer",
    "bearerFormat" => "JWT"
  }
}

# Apply it globally
doc["security"] = [{ "bearerAuth" => [] }]

File.write("doc/openapi.json", JSON.pretty_generate(doc))
```

## Reading the generation report

```ruby
report = RailsOpenapiGenerator::Generator.new(config).generate

puts "Processed: #{report.processed_count}"
puts "Skipped:   #{report.skipped_count}"

report.skipped.each do |item|
  puts "  - #{item.verb} #{item.path}: #{item.reason}"
end

report.warnings.each do |warning|
  puts "Warning: #{warning}"
end
```

## Using the default configuration

If you have a `config/initializers/rails_openapi_generator.rb` already, you can use the configured instance directly:

```ruby
generator = RailsOpenapiGenerator::Generator.new(RailsOpenapiGenerator.configuration)
generator.generate
```

## Serving the spec from a controller

```ruby
class Api::DocsController < ApplicationController
  def show
    config = RailsOpenapiGenerator.configuration
    document = RailsOpenapiGenerator::Generator.new(config).document
    render json: document
  end
end
```

Note: generating the spec on every request is slow for large apps. Cache the result:

```ruby
def show
  spec = Rails.cache.fetch("openapi_spec", expires_in: 1.hour) do
    RailsOpenapiGenerator::Generator.new(RailsOpenapiGenerator.configuration).document
  end
  render json: spec
end
```

## RSpec integration

Test that your spec is still valid after changes:

```ruby
# spec/openapi_spec.rb
require "rails_helper"
require "json_schemer"

RSpec.describe "OpenAPI spec" do
  let(:document) do
    config = RailsOpenapiGenerator::Configuration.new
    config.output_path = Rails.root.join("doc/openapi.json").to_s
    RailsOpenapiGenerator::Generator.new(config).document
  end

  it "is valid OpenAPI 3.1" do
    meta_schema = JSONSchemer.openapi31
    errors = meta_schema.validate(document).to_a
    expect(errors).to be_empty, errors.map { |e| e["error"] }.join("\n")
  end

  it "has no generation warnings" do
    config = RailsOpenapiGenerator::Configuration.new
    report = RailsOpenapiGenerator::Generator.new(config).generate
    expect(report.warnings).to be_empty
  end
end
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://tonystrawberry.gitbook.io/rails-openapi-generator/guides/programmatic-use.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
