---
title: "sdk/ruby"
description: "Ruby server SDK for networkless JWT verification, Rack/Rails request authentication, and webhook signature validation."
locale: "en"
---

> Documentation Index
> Fetch the locale documentation index at: https://xid.dev/en/llms.txt
> Use this file to discover all available pages before exploring further.

# sdk/ruby

## Status

Implemented and verified locally. Real IdP round-trip verification (JWKS fetch, token sign/verify against a live XID instance) has not been performed yet and must be completed before production use.

## Install

```ruby
# Gemfile
gem "xid"

bundle install
```

## Quick start

```ruby
require "xid"

Xid.configure do |c|
  c.issuer         = "https://xid.dev"
  c.audience       = "your_client_id"
  c.webhook_secret = "whsec_AbCdEf..."
end

# Verify a token
begin
  claims = Xid.verify_token(raw_token)
  puts claims.sub    # => "usr_abc123"
  puts claims.scope  # => "openid profile email"
rescue Xid::TokenVerificationError => e
  puts "Token invalid: #{e.message}"
end
```

## Authenticate a Rack/Rails request

```ruby
# Sinatra before-filter
before do
  auth = Xid.authenticate_request(request)
  halt 401, "Unauthorized" unless auth.signed_in?
  @current_user_id = auth.claims.sub
end

# Rack middleware (env-based)
auth = Xid.authenticate_request(env)
unless auth.signed_in?
  return [401, { "Content-Type" => "application/json" },
      [JSON.generate({ error: auth.reason })]]
end
```

## Verify webhook

```ruby
# Rails controller action
def receive
  raw_body = request.raw_post
  payload = Xid.verify_webhook(request.headers.to_h, raw_body)
  handle_event(payload["type"], payload["data"])
  head :ok
rescue Xid::WebhookVerificationError
  head :bad_request
end
```

## Multi-issuer setup

```ruby
config_a = Xid::Configuration.new
config_a.issuer   = "https://tenant-a.xid.dev"
config_a.audience = "client_a"
client_a = Xid::Client.new(config_a)
claims = client_a.verify_token(token)
```

## Configuration options

| Key | Default | Description |
| --- | --- | --- |
| `issuer` | `https://xid.dev` | OIDC issuer URL |
| `audience` | `nil` | Expected aud claim; nil skips validation |
| `jwks_ttl` | `3600` | JWKS local cache TTL in seconds |
| `leeway` | `60` | JWT clock skew tolerance in seconds |
| `webhook_secret` | `nil` | Webhook signing secret with `whsec_` prefix |
| `webhook_tolerance` | `300` | Webhook replay window in seconds |
| `cookie_name` | `__xid_token` | Cookie key for access token extraction |

## Platform notes

- Depends on the `jwt` gem (ES256/RS256 support). Ruby 3.1+ required.
- `Xid.authenticate_request` accepts both a Rack env hash and a Rack `Request` object.
- Exception hierarchy: `Xid::Error` -&gt; `ConfigurationError`, `JwksError`, `TokenVerificationError`, `WebhookVerificationError`.

Source: https://xid.dev/sdks/ruby/index.mdx
