Ruby on Rails icon

Ruby on Rails

Ruby on railsRuby 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).

.env
IMEJIS_API_KEY=your_api_key
IMEJIS_DESIGN_ID=your_design_id

1 a controller that renders the image1. A controller that renders the image

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
config/routes.rb
get "/og", to: "og#show"

2 reference it in your layout2. Reference it in your layout

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.