Connect: Ruby on Rails
Connect a Rails site: one service, one controller, four routes and two views.
This is the simple way: we send finished HTML with the SEO done, and your site shows it in its own design. Using a coding agent? Copy the prompt from your dashboard's Integration page instead: it has your values filled in and the same steps for this framework.
How it works
- The key comes from the environment.
BLOGSFORSEO_API_KEYis read on the server and sent asAuthorization: Bearer <key>. It's never in the code, the templates or the browser. - Every visit asks our API. The pages are rendered on each request, never saved at build time, so new posts, edits and removals show up on their own. The one rule for caching: nothing may keep our API's answer for more than 5 minutes. If you add a cache, keep it at 300 seconds or less, and check your CDN rules on
/blogand/blog-sitemap.xmltoo. - A 404 from us is a 404 from you. An unknown or removed post must answer a real 404, not an empty page. Any other error fails the request, so your framework shows its normal error page.
Match the routes to your post URL pattern in the dashboard: these use /blog and /blog/{slug}. In this guide, {API} means https://blogsforseo.com/api/v1.
Set it up
Uses Ruby's built-in Net::HTTP, so there's nothing to install.
# app/services/blogs_for_seo.rb
require "net/http"
require "json"
module BlogsForSeo
API_URL = ENV.fetch("BLOGSFORSEO_API_URL", "https://blogsforseo.com").chomp("/") + "/api/v1"
# Asks the Content API on every request. A 404 from the API is this page's 404.
def self.get(path, query = {})
uri = URI(API_URL + path)
uri.query = URI.encode_www_form(query) unless query.empty?
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Bearer " + ENV.fetch("BLOGSFORSEO_API_KEY")
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 5, read_timeout: 10) do |http|
http.request(request)
end
raise ActionController::RoutingError, "Not found" if response.is_a?(Net::HTTPNotFound)
response.value # Raises unless the API answered 2xx.
response.body
end
end
# app/controllers/blog_controller.rb
class BlogController < ApplicationController
def index
page = [params[:page].to_i, 1].max
@blog = JSON.parse(BlogsForSeo.get("/html/posts", page: page))
end
def show
@blog = JSON.parse(BlogsForSeo.get("/html/posts/" + ERB::Util.url_encode(params[:slug])))
end
def sitemap
render plain: BlogsForSeo.get("/sitemap", format: "xml"), content_type: "application/xml"
end
def llms
render plain: BlogsForSeo.get("/llms.txt")
end
end
# config/routes.rb, inside Rails.application.routes.draw
get "blog", to: "blog#index"
get "blog/:slug", to: "blog#show", constraints: { slug: /[a-z0-9-]+/ }
get "blog-sitemap.xml", to: "blog#sitemap", format: false
get "llms.txt", to: "blog#llms", format: false
Views, in your own layout. head and body are already escaped by us, so print them raw:
<%# app/views/blog/show.html.erb %>
<% content_for :head, raw(@blog["head"]) %>
<%= raw @blog["body"] %>
<%# app/views/blog/index.html.erb %>
<%= raw @blog["body"] %>
Your layout needs <%= yield :head %> inside <head>; leave out your own <title> on post pages.
- IndexNow key file: save
public/<your IndexNow key>.txtcontaining just the key. - robots.txt: add the sitemap line from your dashboard (
Sitemap: https://<your domain>/blog-sitemap.xml) topublic/robots.txt. - Caching: Rails sends
Cache-Control: privateby default, which is right. If you addRails.cache.fetch, useexpires_in: 5.minutesor less, and don't addexpires_inor page caching to these actions for longer.
The advanced way
Fetch /posts and /posts/{slug} instead of the /html endpoints, and build the page from the fields in the Content API reference, following the advanced guide. bodyHtml is already sanitised: print it raw. When you put jsonLd in a <script type="application/ld+json">, escape <:
JSON.generate(post["jsonLd"]).gsub("<", '\u003c')
Check it
Run these against your site (local or live). -A GPTBot asks the way an AI crawler does, without JavaScript:
curl -A GPTBot https://<your domain>/blog # lists posts, or "No posts yet"
curl -A GPTBot https://<your domain>/blog/<a post slug> # <h1>, article text, canonical link, application/ld+json
curl -i https://<your domain>/blog/not-a-real-post # HTTP 404
curl https://<your domain>/blog-sitemap.xml # the same posts as GET {API}/sitemap
curl https://<your domain>/llms.txt # plain text
curl https://<your domain>/<your IndexNow key>.txt # just the key
curl https://<your domain>/robots.txt # has the Sitemap: line
Then check it stays fresh: the number of posts on /blog matches total from the API. Finish with the simple guide's dashboard steps: check the post address, run the health check and connect Search Console.