---
title: "Ruby on Rails"
description: "Generate dynamic OG images and on-demand graphics in Ruby on Rails with the Imejis.io API. A controller proxies the render so your API key stays server-side."
url: "https://www.imejis.io/apis/rails"
published: "2026-07-27"
---

# Ruby on Rails

## Ruby on Rails

Generate a unique image per page or record from one Imejis template. The controller below calls Imejis server-side with `Net::HTTP` and sends the image straight back, keeping your API key private.

Set two environment variables (for example with `dotenv-rails` or your host's config).

```bash title=".env"
IMEJIS_API_KEY=your_api_key
IMEJIS_DESIGN_ID=your_design_id
```

### 1. A controller that renders the image

```ruby showLineNumbers title="app/controllers/og_controller.rb"
require "net/http"
require "json"

class OgController < ApplicationController
  def show
    uri = URI("https://render.imejis.io/v1/#{ENV['IMEJIS_DESIGN_ID']}")
    request = Net::HTTP::Post.new(
      uri,
      "dma-api-key" => ENV["IMEJIS_API_KEY"],
      "Content-Type" => "application/json"
    )
    request.body = { "headline.text" => params.fetch(:title, "Untitled") }.to_json

    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    expires_in 1.year, public: true
    send_data response.body, type: "image/jpeg", disposition: "inline"
  end
end
```

```ruby showLineNumbers title="config/routes.rb"
get "/og", to: "og#show"
```

### 2. Reference it in your layout

```html showLineNumbers title="app/views/layouts/application.html.erb"
<meta property="og:image" content="<%=
"#{request.base_url}/og?title=#{CGI.escape(@post.title)}" %>">
<meta name="twitter:card" content="summary_large_image" />
```

**Override any field.** Each dynamic component exposes keys like `headline.text`, `title.color`, or `image.image`. Send only the ones you want to change. A design's overridable fields are listed in its Share / API panel.
