Rack Integration

Better Auth Ruby is Rack-first. BetterAuth.auth(...) returns an object that can be called directly by Rack.

Gemfile

Gemfile
gem "better_auth"

Auth Instance

config/auth.rb
require "better_auth"

AUTH = BetterAuth.auth(
  secret: ENV.fetch("BETTER_AUTH_SECRET"),
  base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
  email_and_password: {
    enabled: true
  },
  database: BetterAuth::Adapters::Postgres.new(
    url: ENV.fetch("DATABASE_URL")
  )
)

Mount In Rack

config.ru
require_relative "config/auth"

map "/api/auth" do
  run AUTH
end

Verify the mount:

Terminal
curl http://localhost:3000/api/auth/ok
{ "ok": true }

Sinatra

Sinatra apps should use the better_auth-sinatra adapter for helpers and Better Auth-specific Rake tasks. See the Sinatra integration guide for the full setup.

Server-Side API Calls

You can also call endpoints from Ruby code:

result = AUTH.api.sign_in_email(
  body: {
    email: "ada@example.com",
    password: "password123"
  }
)

When you need to forward cookies, request a Rack response:

status, headers, body = AUTH.api.sign_in_email(
  {
    body: {
      email: "ada@example.com",
      password: "password123"
    },
    as_response: true
  }
)

On this page