Ruby

Installation and Setup

  1. Install the Ruby gem:
$gem install schematichq

Or add it to your Gemfile:

1gem "schematichq"

Then run:

$bundle install
  1. Issue an API key for the appropriate environment using the Schematic app. Be sure to capture the secret key when you issue the API key; you’ll only see this key once, and this is what you’ll use with schematic-ruby.

  2. Using this secret key, initialize a client in your application:

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(api_key: api_key)
5
6# interactions with the client
7
8client.close

By default, the client will do some local caching for flag checks. If you would like to change this behavior, you can do so using an initialization option to specify the max size of the cache (in terms of number of records) and the max age of the cache (in seconds):

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4cache_size = 100
5cache_ttl = 1.0 # in seconds
6client = Schematic::SchematicClient.new(
7 api_key: api_key,
8 cache_providers: [
9 Schematic::LocalCache.new(max_size: cache_size, ttl: cache_ttl)
10 ]
11)
12
13# interactions with the client
14
15client.close

You can also disable local caching entirely with an initialization option; bear in mind that, in this case, every flag check will result in a network request:

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(
5 api_key: api_key,
6 cache_providers: []
7)
8
9# interactions with the client
10
11client.close

You may want to specify default flag values for your application, which will be used if there is a service interruption or if the client is running in offline mode (see below). You can do this using an initialization option:

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(
5 api_key: api_key,
6 flag_defaults: {
7 "some-flag-key" => true
8 }
9)
10
11# interactions with the client
12
13client.close

Custom Logging

You can provide your own logger implementation to control how the Schematic client logs messages. The logger must implement the Schematic::Logger module with error, warn, info, and debug methods:

1require "schematichq"
2
3class CustomLogger
4 include Schematic::Logger
5
6 def error(message, *args)
7 # Your custom error logging logic
8 end
9
10 def warn(message, *args)
11 # Your custom warning logging logic
12 end
13
14 def info(message, *args)
15 # Your custom info logging logic
16 end
17
18 def debug(message, *args)
19 # Your custom debug logging logic
20 end
21end
22
23api_key = ENV["SCHEMATIC_API_KEY"]
24client = Schematic::SchematicClient.new(
25 api_key: api_key,
26 logger: CustomLogger.new
27)
28
29# interactions with the client
30
31client.close

You can also adjust the log level of the built-in console logger:

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(
5 api_key: api_key,
6 logger: Schematic::ConsoleLogger.new(level: :debug)
7)

If no logger is provided, the client will use a default console logger at the :info level that outputs to stderr.

Usage Examples

A number of these examples use keys to identify companies and users. Learn more about keys here.

Sending identify events

Create or update users and companies using identify events.

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(api_key: api_key)
5
6client.identify({
7 company: {
8 keys: { "id" => "your-company-id" },
9 name: "Acme, Inc.",
10 traits: { "city" => "Atlanta" }
11 },
12 keys: {
13 "email" => "wcoyote@acme.net",
14 "user_id" => "your-user-id"
15 },
16 name: "Wile E. Coyote",
17 traits: {
18 "enemy" => "Bugs Bunny",
19 "login_count" => 24,
20 "is_staff" => false
21 }
22})
23
24client.close

This call is non-blocking and there is no response to check.

Sending track events

Track activity in your application using track events; these events can later be used to produce metrics for targeting.

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(api_key: api_key)
5
6client.track({
7 event: "some-action",
8 company: { "id" => "your-company-id" },
9 user: {
10 "email" => "wcoyote@acme.net",
11 "user_id" => "your-user-id"
12 }
13})
14
15client.close

This call is non-blocking and there is no response to check.

If you want to record large numbers of the same event at once, or perhaps measure usage in terms of a unit like tokens or memory, you can optionally specify a quantity for your event:

1client.track({
2 event: "query-tokens",
3 company: { "id" => "your-company-id" },
4 user: {
5 "email" => "wcoyote@acme.net",
6 "user_id" => "your-user-id"
7 },
8 quantity: 1500
9})

Creating and updating companies

Although it is faster to create companies and users via identify events, if you need to handle a response, you can use the companies API to upsert companies. Because you use your own identifiers to identify companies, rather than a Schematic company ID, creating and updating companies are both done via the same upsert operation:

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(api_key: api_key)
5
6response = client.companies.upsert_company(
7 keys: { "id" => "your-company-id" },
8 name: "Acme Widgets, Inc.",
9 traits: {
10 "city" => "Atlanta",
11 "high_score" => 25,
12 "is_active" => true
13 }
14)
15puts response.data
16
17client.close

You can define any number of company keys; these are used to address the company in the future, for example by updating the company’s traits or checking a flag for the company.

You can also define any number of company traits; these can then be used as targeting parameters.

Creating and updating users

Similarly, you can upsert users using the Schematic API, as an alternative to using identify events. Because you use your own identifiers to identify users, rather than a Schematic user ID, creating and updating users are both done via the same upsert operation:

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(api_key: api_key)
5
6response = client.companies.upsert_user(
7 keys: {
8 "email" => "wcoyote@acme.net",
9 "user_id" => "your-user-id"
10 },
11 company: { "id" => "your-company-id" },
12 name: "Wile E. Coyote",
13 traits: {
14 "city" => "Atlanta",
15 "login_count" => 24,
16 "is_staff" => false
17 }
18)
19puts response.data
20
21client.close

You can define any number of user keys; these are used to address the user in the future, for example by updating the user’s traits or checking a flag for the user.

You can also define any number of user traits; these can then be used as targeting parameters.

Checking flags

When checking a flag, you’ll provide keys for a company and/or keys for a user. You can also provide no keys at all, in which case you’ll get the default value for the flag.

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(api_key: api_key)
5
6is_flag_on = client.check_flag(
7 "some-flag-key",
8 company: { "id" => "your-company-id" },
9 user: {
10 "email" => "wcoyote@acme.net",
11 "user_id" => "your-user-id"
12 }
13)
14
15if is_flag_on
16 # Flag is on
17else
18 # Flag is off
19end
20
21client.close

Checking flags with entitlement details

If you need more detail about how a flag check was resolved, including any entitlement associated with the check, use check_flag_with_entitlement. This returns a response object with the flag value, the reason for the evaluation result, and entitlement details such as usage, allocation, and credit balances when applicable.

1require "schematichq"
2
3api_key = ENV["SCHEMATIC_API_KEY"]
4client = Schematic::SchematicClient.new(api_key: api_key)
5
6resp = client.check_flag_with_entitlement(
7 "some-flag-key",
8 company: { "id" => "your-company-id" },
9 user: {
10 "email" => "wcoyote@acme.net",
11 "user_id" => "your-user-id"
12 }
13)
14
15puts "Flag: #{resp.flag_key}, Value: #{resp.value}, Reason: #{resp.reason}"
16
17if resp.entitlement
18 puts "Feature allocation: #{resp.feature_allocation}"
19 puts "Feature usage: #{resp.feature_usage}"
20end
21
22client.close

Other API operations

The Schematic API supports many operations beyond these, accessible via the API modules on the client: accounts, billing, companies, credits, entitlements, events, features, and plans.

Webhook Verification

Schematic can send webhooks to notify your application of events. To ensure the security of these webhooks, Schematic signs each request using HMAC-SHA256. The SDK provides utility functions to verify these signatures.

Verifying Webhook Signatures

When your application receives a webhook request from Schematic, you should verify its signature to ensure it’s authentic:

1require "schematichq"
2
3webhook_secret = "your-webhook-secret" # Get this from the Schematic app
4
5# Verify a signature directly
6Schematic::Webhooks.verify_signature(payload, signature, timestamp, webhook_secret)
7# Raises Schematic::Webhooks::InvalidSignatureError if the signature is invalid
8# Raises Schematic::Webhooks::MissingSignatureError if the signature is missing
9# Raises Schematic::Webhooks::MissingTimestampError if the timestamp is missing

Computing Signatures

You can compute a hex-encoded signature for a given payload:

1hex_sig = Schematic::Webhooks.compute_hex_signature(body, timestamp, secret)

Rack / Rails Integration

The SDK can verify webhook signatures from Rack-compatible request objects (including Rails controllers):

1# In a Rails controller
2class WebhooksController < ApplicationController
3 skip_before_action :verify_authenticity_token
4
5 def create
6 webhook_secret = ENV["SCHEMATIC_WEBHOOK_SECRET"]
7
8 begin
9 Schematic::Webhooks.verify_webhook_request(request, webhook_secret)
10
11 # Signature is valid, process the webhook
12 payload = JSON.parse(request.raw_post)
13 # ... handle the event ...
14
15 head :ok
16 rescue Schematic::Webhooks::WebhookSignatureError => e
17 render json: { error: e.message }, status: :bad_request
18 end
19 end
20end

For a Sinatra application:

1require "sinatra"
2require "schematichq"
3
4post "/webhooks/schematic" do
5 webhook_secret = ENV["SCHEMATIC_WEBHOOK_SECRET"]
6
7 begin
8 Schematic::Webhooks.verify_webhook_request(request, webhook_secret)
9
10 payload = JSON.parse(request.body.read)
11 request.body.rewind
12 # ... handle the event ...
13
14 status 200
15 json status: "ok"
16 rescue Schematic::Webhooks::WebhookSignatureError => e
17 status 400
18 json error: e.message
19 end
20end

Webhook Test Server

The SDK includes a standalone webhook test server for local development:

$ruby scripts/webhook_test_server.rb <webhook_secret>
$# or
$SCHEMATIC_WEBHOOK_SECRET=your-secret ruby scripts/webhook_test_server.rb

This starts a WEBrick server on port 8080 (configurable via PORT env var) that listens for POST requests at /webhook, verifies signatures, and prints the payload.

DataStream

DataStream enables local flag evaluation by maintaining a WebSocket connection to Schematic and caching flag rules, company, and user data locally. Flag checks are evaluated using a local WASM rules engine, significantly reducing latency and network calls.

Setup

1require "schematichq"
2
3client = Schematic::SchematicClient.new(
4 api_key: ENV["SCHEMATIC_API_KEY"],
5 use_data_stream: true
6)
7
8# Flag checks are now evaluated locally
9is_flag_on = client.check_flag(
10 "some-flag-key",
11 company: { "id" => "your-company-id" }
12)
13
14client.close

Configuration Options

You can customize DataStream behavior via the datastream_options hash:

OptionTypeDefaultDescription
cache_ttlInteger86400 (24 hours)Cache TTL in seconds
replicator_modeBooleanfalseEnable replicator mode (see below)
replicator_health_urlStringhttp://localhost:8090/readyURL to poll for replicator health
replicator_health_intervalInteger30Health check interval in seconds
1require "schematichq"
2
3client = Schematic::SchematicClient.new(
4 api_key: ENV["SCHEMATIC_API_KEY"],
5 use_data_stream: true,
6 datastream_options: {
7 cache_ttl: 3600 # 1 hour
8 }
9)

Replicator Mode

When running the schematic-datastream-replicator service, you can configure the Schematic client to operate in Replicator Mode. In this mode, the client reads from a shared cache populated by the external replicator rather than establishing its own WebSocket connection.

Setup

1require "schematichq"
2
3client = Schematic::SchematicClient.new(
4 api_key: ENV["SCHEMATIC_API_KEY"],
5 use_data_stream: true,
6 datastream_options: {
7 replicator_mode: true,
8 replicator_health_url: "http://localhost:8090/ready",
9 replicator_health_interval: 30
10 }
11)
12
13# Flag checks use the shared cache
14is_flag_on = client.check_flag(
15 "some-flag-key",
16 company: { "id" => "your-company-id" }
17)
18
19client.close

When running in Replicator Mode, the client will:

  • Skip establishing WebSocket connections
  • Periodically check if the replicator service is ready
  • Use cached data populated by the external replicator service
  • Fall back to direct API calls if the replicator is not available

Testing

Offline Mode

In development or testing environments, you may want to avoid making network requests to the Schematic API. You can run Schematic in offline mode by specifying the offline option; in this case, it does not matter what API key you specify:

1require "schematichq"
2
3client = Schematic::SchematicClient.new(offline: true)
4
5client.close

You can also enable offline mode by not providing an API key:

1require "schematichq"
2
3client = Schematic::SchematicClient.new
4# Logs a warning and automatically enables offline mode
5
6client.close

Offline mode works well with flag defaults:

1require "schematichq"
2
3client = Schematic::SchematicClient.new(
4 offline: true,
5 flag_defaults: { "some-flag-key" => true }
6)
7
8# interactions with the client
9
10client.close

In an automated testing context, you may also want to use offline mode and specify single flag responses for test cases:

1require "schematichq"
2
3# In your test setup
4client = Schematic::SchematicClient.new(offline: true)
5client.set_flag_default("some-flag-key", true)
6
7# test code that expects the flag to be on
8assert client.check_flag("some-flag-key")
9
10client.close

Errors

Failed API calls will raise errors that can be rescued from granularly:

1require "schematichq"
2
3client = Schematic::SchematicClient.new(api_key: ENV["SCHEMATIC_API_KEY"])
4
5begin
6 result = client.accounts.create_api_key(name: "name")
7rescue Schematic::Errors::TimeoutError
8 puts "API didn't respond before our timeout elapsed"
9rescue Schematic::Errors::ServiceUnavailableError
10 puts "API returned status 503, is probably overloaded, try again later"
11rescue Schematic::Errors::ServerError
12 puts "API returned some other 5xx status, this is probably a bug"
13rescue Schematic::Errors::ResponseError => e
14 puts "API returned an unexpected status other than 5xx: #{e.code} #{e.message}"
15rescue Schematic::Errors::ApiError => e
16 puts "Some other error occurred when calling the API: #{e.message}"
17end

Note that check_flag and check_flag_with_entitlement never raise exceptions — errors are logged and default values are returned.

Advanced

Retries

The SDK is instrumented with automatic retries. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retryable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the max_retries option to configure this behavior:

1require "schematichq"
2
3client = Schematic::SchematicClient.new(
4 api_key: ENV["SCHEMATIC_API_KEY"],
5 max_retries: 3 # Configure max retries (default is 2)
6)

Timeouts

The SDK defaults to a 60 second timeout. Use the timeout option to configure this behavior:

1response = client.accounts.create_api_key(
2 name: "name",
3 timeout: 30 # 30 second timeout
4)