Skip to content

HomeDocsDeveloperRuby

Developer

Ruby

Mint Reqio HS256 identity tokens on a Ruby server using the jwt gem. Compatible with Rails, Sinatra, and any Ruby 2.7+ framework.

The snippet below is exactly what the dashboard generates under Project > Settings > Identity when you choose Ruby. Copy it from the dashboard to get your real project ID and audience URL pre-filled.

Before you start

  • A Reqio project
  • A signing secret generated at Project > Settings > Identity
  1. Install the jwt gem

    Install

    gem install jwt

    Or add to your Gemfile:

    gem "jwt"
  2. Mint a token

    # gem install jwt
    require "jwt"
     
    now = Time.now.to_i
    payload = {
      sub:    current_user.id,
      email:  current_user.email,
      traits: {
        # amount in cents; interval is "month" | "year"
        plan: current_user.plan,
        amount: current_user.plan_amount,
        interval: current_user.billing_interval,
        # optional CRM enrichment: 0-100 health, CRM account id, ISO renewal
        # date -- keys stay camelCase on the wire
        healthScore: account.health_score,
        accountId: account.crm_id,
        renewalAt: account.renewal_at,
      },
      aud:    "https://reqio.app/p/YOUR_PROJECT_ID",
      iat:    now,
      exp:    now + 300,
    }
    token = JWT.encode(payload, ENV["REQIO_SECRET"], "HS256")

    Replace YOUR_PROJECT_ID with your project ID, or copy the full snippet from the dashboard where the audience URL is pre-filled.

    Traits keys must stay camelCase

    Reqio's verifier reads camelCase keys (healthScore, accountId, renewalAt). Use camelCase symbols in the traits hash, JWT.encode serializes symbol keys by their literal name, so a snake_case symbol produces a snake_case JSON key the verifier will not recognize.

    Framework-specific examples:

    # app/controllers/application_controller.rb
    require "jwt"
     
    class ApplicationController < ActionController::Base
      before_action :set_reqio_token, if: :user_signed_in?
     
      private
     
      def set_reqio_token
        now = Time.now.to_i
        @reqio_token = JWT.encode(
          {
            sub:    current_user.id.to_s,
            email:  current_user.email,
            traits: { plan: current_user.plan },
            aud:    ENV["REQIO_AUDIENCE"],
            iat:    now,
            exp:    now + 300,
          },
          ENV["REQIO_SECRET"],
          "HS256"
        )
      end
    end
    <%# app/views/layouts/application.html.erb %>
    <% if @reqio_token %>
      <script
        src="https://reqio.app/widget.js"
        data-project-id="<%= ENV['REQIO_PROJECT_ID'] %>"
        data-identity-token="<%= @reqio_token %>"
        async
      ></script>
    <% end %>

    Store the audience URL in REQIO_AUDIENCE so you do not hardcode it.

  3. Set the environment variable

    Set REQIO_SECRET to the signing secret from Project > Settings > Identity > Reveal secret. Store it via Rails credentials, dotenv, or your hosting platform's secret management; never render it into responses.

    Environment variable

    REQIO_SECRET=your-signing-secret
  4. Pass the token to the widget

    For server-rendered templates, pass the token via data-identity-token:

    <script
      src="https://reqio.app/widget.js"
      data-project-id="<%= ENV['REQIO_PROJECT_ID'] %>"
      data-identity-token="<%= @reqio_token %>"
      async
    ></script>

    For SPAs where the user authenticates after the initial page load, call window.Reqio.identify() from JavaScript after login:

    window.Reqio.identify(token);

Traits reference

planString
Plan name on your billing system, e.g. "pro".
amountInteger
Recurring charge as billed, in cents. For annual plans send the full annual total; Reqio divides by 12 to derive the monthly value.
interval"month" | "year"
Billing cadence for amount.

CRM enrichment

healthScoreInteger (0-100)
CRM account-health score. Reqio clamps out-of-range values.
accountIdString
Your CRM's external account id. Not displayed, a matching key for a future CRM sync (email first, accountId fallback).
renewalAtString (ISO 8601 date)
Next renewal date. Unparseable values are dropped by Reqio.

Security

Store REQIO_SECRET in your environment and never render it into responses. The JWT is short-lived and audience-bound to one Reqio project. The signing secret must remain server-side.