OG Pilot
rails og image

Rails OG image: static file, Active Storage, or the API

Use this page when a Rails app needs an Open Graph image per record — a blog post, a product, a profile — and you do not want to design each card by hand. It compares three ways to do that, then gives a copy-paste job that calls the OG Pilot API and stores the file with Active Storage.

Three ways to ship the card

A static image in the layout is the right call when one artwork represents the whole site. Generating the file yourself and attaching it with Active Storage is the right call when each record needs its own pixels and you want to own the drawing code. Calling the hosted API and caching the result in Active Storage is the right call when each record needs its own card and you would rather not run an image library or a headless browser.

There is a fourth path, already written up on the Rails OG image guide. The gem returns a CDN URL on the request, and the layout prints that URL. You never store the bytes. Use that when a URL in the tag is enough. Use the job on this page when the file has to live on the record.

ApproachSetup effortPer-request cost / latencyCachingMaintenanceDynamic data
Static file in the layoutOne file in public/ and one meta tag in the layout.No render on the request. The server sends a file that already exists. This page does not publish a millisecond figure.The file stays until you replace it. Social platforms cache by URL, so a new filename is what makes them fetch again.You replace the artwork by hand. Every URL shares it unless you add more files.No. The same pixels for every post, product, or profile.
Your renderer + Active StorageA has_one_attached, a job, and an image library or a headless browser you operate. This page does not name a rendering gem.The HTML request can serve the stored file. The render runs in the job. This page does not publish that render's time or host cost.You replace the blob when the card fields change. Crawlers keep the previous preview until og:image points at a new URL and they fetch it.You own fonts, layout, emoji, and the process that draws the file.Yes, when the job reads the record and draws those fields.
OG Pilot API + Active StorageThe og_pilot_ruby gem, an API key, a verified domain, and a job that downloads the JSON url and attaches the bytes.The HTML request serves the stored file. The API call runs in the job. The plan catalog lists Starter at $5/month ($50/year, 10,000-image quota), Builder at $49/month ($99/year, 100,000-image quota), and Growth at $99/month ($199/year, unlimited image quota). A Stripe image_quota feature can replace the quota. The catalog has no per-image price.Replace the attachment when the card fields change. Pass updated_at as iat. The API docs say a present iat keeps the hosted cache for 24 hours from that timestamp. Omit iat and the hosted cache does not expire on its own.OG Pilot owns the template. You own the job, the attachment, and the public URL.Yes. Pass template, title, description, and path. Templates are listed on /docs, including blog_post and product.

API behavior is from /docs and from OgPilotRuby.create_blog_post_image in the og_pilot_ruby gem: json: true returns the parsed JSON, and a request error returns a Hash with image_url set to nil. The success body in this app includes status, url, width 1200, and height 630. A render that is still running returns HTTP 202 with status processing or failed, url null, and Retry-After: 30.

Meta tags in the layout

Put the tags in the layout head, filled from the record. content_for :head keeps the show template in charge of the values. og:url is the page, and og:image is the image. twitter:card is summary_large_image when you want the large card.

A static file can live in public/. root_url makes the image URL absolute, which is what a crawler can fetch. Width and height below are the API response values, 1200 and 630. Pass the real dimensions of a file you drew yourself.

rails_blob_url builds an absolute URL from the current request. In production the host has to be the public host. A disk service on localhost is not a URL a social crawler can fetch. Confirm the live URL in the Open Graph debugger.

Static file

<%# public/social-card.jpg — one file for the whole site %>
<%= tag.meta property: "og:image", content: URI.join(root_url, "/social-card.jpg").to_s %>
<%= tag.meta property: "og:title", content: "Your site" %>
<%= tag.meta property: "og:description", content: "One description for every URL" %>
<%= tag.meta property: "og:url", content: request.original_url %>
<%= tag.meta name: "twitter:card", content: "summary_large_image" %>

Helper, layout, and the record

# app/helpers/open_graph_helper.rb
module OpenGraphHelper
  def open_graph_tags(title:, description:, url:, image: nil, width: nil, height: nil)
    tags = []
    tags << tag.meta(property: "og:title", content: title)
    tags << tag.meta(property: "og:description", content: description)
    tags << tag.meta(property: "og:url", content: url)
    tags << tag.meta(property: "og:image", content: image) if image.present?
    tags << tag.meta(property: "og:image:width", content: width) if width.present?
    tags << tag.meta(property: "og:image:height", content: height) if height.present?
    tags << tag.meta(name: "twitter:card", content: "summary_large_image")
    safe_join(tags)
  end
end

# app/views/layouts/application.html.erb, inside <head>
<%= yield :head %>

# app/views/posts/show.html.erb
<% content_for :head do %>
  <%= open_graph_tags(
        title: @post.title,
        description: @post.excerpt,
        url: post_url(@post),
        image: (@post.og_image.attached? ? rails_blob_url(@post.og_image) : nil),
        width: 1200,
        height: 630
      ) %>
<% end %>

Call the API and attach the file

The job runs when the record is created and when the title or excerpt changes. It does not run on the HTML request, and attaching the file does not change those columns, so the callback does not enqueue itself again. Map author and published_at to your own schema. create_blog_post_image sets template to blog_post. Product, event, and the other helpers are on /docs.

image_type: "jpeg" is the documented claim for the delivered format. The JSON body does not include a content type, so the sample sets image/jpeg to match that claim. Attach only when status is completed and url is present. A 202 means the render is still going. Leave the current attachment and retry the job. The processing response sets Retry-After to 30.

iat is the record's updated_at. The docs say a present iat keeps the hosted cache for 24 hours from that timestamp. Omit it and the hosted cache does not expire on its own. Changing the title already changes the payload, so the next call is a different image either way. The API key stays in OG_PILOT_API_KEY. The browser never sees it.

To draw the card yourself, keep the same model, job, and attach call. Replace the gem call with the bytes from your library or browser. This page does not name that library, and it does not publish how long that render takes or what it costs to run.

Service, job, and invalidation

# Gemfile — version from /docs
gem "og_pilot_ruby", "~> 0.4.1"

# bundle install
# bin/rails g og_pilot_ruby:install
# Set OG_PILOT_API_KEY and OG_PILOT_DOMAIN on the server.
# The gem signs the JWT. Do not add a hand-rolled signer.

# app/models/post.rb
class Post < ApplicationRecord
  has_one_attached :og_image

  after_commit :refresh_og_image_later, if: :og_card_fields_changed?

  private

  def og_card_fields_changed?
    previously_new_record? || saved_change_to_title? || saved_change_to_excerpt?
  end

  def refresh_og_image_later
    RefreshPostOgImageJob.perform_later(self)
  end
end

# app/jobs/refresh_post_og_image_job.rb
class RefreshPostOgImageJob < ApplicationJob
  queue_as :default

  def perform(post)
    Posts::OgImage.attach(post)
  end
end

# app/services/posts/og_image.rb
require "net/http"
require "stringio"
require "uri"

module Posts
  class OgImage
    def self.attach(post)
      new(post).attach
    end

    def initialize(post)
      @post = post
    end

    def attach
      metadata = OgPilotRuby.create_blog_post_image(
        title: post.title,
        description: post.excerpt.to_s,
        author_name: post.author&.name,
        publish_date: post.published_at&.to_date&.iso8601,
        path: "/posts/#{post.to_param}",
        image_type: "jpeg",
        iat: post.updated_at.to_i,
        json: true
      )
      return unless metadata.is_a?(Hash)
      return unless metadata["status"] == "completed"
      return if metadata["url"].blank?

      bytes = download(metadata["url"])
      return if bytes.blank?

      post.og_image.attach(
        io: StringIO.new(bytes),
        filename: "og-#{post.id}.jpg",
        content_type: "image/jpeg"
      )
    end

    private

    attr_reader :post

    def download(url, redirects = 3)
      uri = URI.parse(url)
      return unless uri.is_a?(URI::HTTPS)

      response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
        http.get(uri.request_uri)
      end
      if response.is_a?(Net::HTTPRedirection) && redirects.positive? && response["location"].present?
        return download(URI.join(uri, response["location"]).to_s, redirects - 1)
      end

      response.body if response.is_a?(Net::HTTPSuccess)
    end
  end
end

The same card from MCP

An agent that already has the public page URL can call generate_og_image on the OG Pilot MCP server. The tool takes url and an optional style. It returns image_url. It does not take a Rails model, and it does not write Active Storage. Use the job above when the file has to be attached. Use MCP when the agent should produce a card for a URL that is already public.

Auth is OAuth 2.1 with PKCE. The config file has no API key. generate_og_image needs a signed-in account with an active plan and a credit. A trial can call the API after the domain is verified. A trial cannot call generate_og_image. Install steps and the tool table are on /docs/mcp.

MCP install

{
  "mcpServers": {
    "og-pilot": {
      "type": "streamable-http",
      "url": "https://ogpilot.com/mcp"
    }
  }
}

# Auth: OAuth 2.1 with PKCE. No API key in this file.
# Docs: https://ogpilot.com/docs/mcp

generate_og_image
{ "url": "https://example.com/posts/your-post" }

# Optional: "style". Omitted or unknown uses auto-detect.
# Returns status, image_url, page_url, title, style.
# Does not attach Active Storage.

Get an API key

Create an account, verify the domain, and call the API from the job. To let an agent generate a card for a public URL, install the MCP server on an active plan that has a credit.

Related guides

Sizes for the file you attach are on the og:image size cheat sheet. The OG image API comparison is the longer look at a hosted API, @vercel/og, and a DIY pipeline. After the tags are live, run the public URL through the Open Graph debugger.

How to add a Rails OG image

  1. Pick a static file, your own renderer stored in Active Storage, or the OG Pilot API with the file cached on the record.
  2. Print og:title, og:description, og:image, og:url, and twitter:card from the layout.
  3. When the card is generated, attach it in a job and replace it when the record's card fields change.
  4. Create an API key and call OgPilotRuby from that job, or install MCP if an agent should generate a card for the public URL.
  5. Check the public URL in the Open Graph debugger.

Questions

Which Rails approach should I use for og:image?

Use a static file when every URL can share one card. Use your own renderer plus Active Storage when the pixels have to live on the record and you want to own the layout. Use the OG Pilot API, with the file attached to the record, when you want a per-record card and you do not want to operate an image library or a headless browser. The Rails lander at /rails-og-image-generator is the shorter path: the gem returns a CDN URL and you put that URL in og:image, with no attachment.

Does og_pilot_ruby write Active Storage?

No. OgPilotRuby.create_image returns a URL, or a Hash when json: true. On success the Hash includes status, url, width, and height. Saving the bytes is your job: download url when status is completed and attach it. The docs say to use the gem rather than signing the JWT yourself. On a request error the gem returns a Hash whose image_url is nil and does not raise.

When should the Rails OG image job run again?

When a field that appears on the card changes, such as the title or the excerpt. Pass that record's updated_at as iat. The API docs say that if iat is present, the hosted cache is valid for 24 hours from that timestamp, and if iat is omitted the hosted cache does not expire on its own. Replacing the Active Storage attachment happens in the job, not on the HTML request.

Can MCP attach a Rails OG image?

No. generate_og_image takes a public page url and an optional style, and returns image_url. It does not take a Rails model and it does not write Active Storage. Install steps are at /docs/mcp. The tool needs an active plan. A trial is not enough.

Will this page publish latency or price benchmarks?

No. This page does not publish a render time or an API latency. The plan catalog lists Starter at $5/month ($50/year, 10,000-image quota), Builder at $49/month ($99/year, 100,000-image quota), and Growth at $99/month ($199/year, unlimited image quota). Those defaults are on /#pricing. A Stripe image_quota feature can replace the quota. The catalog has no per-image price.