The FastComments Ruby SDK. You can use this to build secure and scalable backend applications that interact with FastComments, or build reactive client applications.
Add this line to your application's Gemfile:
gem 'fastcomments'And then execute:
bundle installOr install it yourself as:
gem install fastcommentsThis library contains the generated API client and the SSO utilities to make working with the API easier.
For the API client, there are two classes, DefaultApi and PublicApi. The DefaultApi contains methods that require your API key, and PublicApi contains api calls
that can be made directly from a browser/mobile device/etc without authentication.
Important: You must set your API key on the ApiClient before making authenticated requests. If you don't, requests will fail with a 401 error.
require 'fastcomments'
# Create and configure the API client
config = FastCommentsClient::Configuration.new
api_client = FastCommentsClient::ApiClient.new(config)
# REQUIRED: Set your API key (get this from your FastComments dashboard)
config.api_key['x-api-key'] = 'YOUR_API_KEY_HERE'
# Create the API instance with the configured client
api = FastCommentsClient::DefaultApi.new(api_client)
# Now you can make authenticated API calls
begin
# Example: Add an SSO user
user_data = {
id: 'user-123',
email: '[email protected]',
displayName: 'John Doe'
}
response = api.add_sso_user('YOUR_TENANT_ID', user_data)
puts "User created: #{response}"
rescue FastCommentsClient::ApiError => e
puts "Error: #{e.response_body}"
# Common errors:
# - 401: API key is missing or invalid
# - 400: Request validation failed
endPublic endpoints don't require authentication:
require 'fastcomments'
public_api = FastCommentsClient::PublicApi.new
begin
response = public_api.get_comments_public(
tenant_id: 'YOUR_TENANT_ID',
url_id: 'page-url-id'
)
puts response
rescue FastCommentsClient::ApiError => e
puts e.message
end- 401 "missing-api-key" error: Make sure you set
config.api_key['x-api-key'] = 'YOUR_KEY'before creating the DefaultApi instance. - Wrong API class: Use
DefaultApifor server-side authenticated requests,PublicApifor client-side/public requests. - Null API key: The SDK will silently skip authentication if the API key is null, leading to 401 errors.
You'll see you're supposed to pass a broadcastId in some API calls. When you receive events, you'll get this ID back, so you know to ignore the event if you plan to optimistically apply changes on the client
(which you'll probably want to do since it offers the best experience). Pass a UUID here. The ID should be unique enough to not occur twice in a browser session.
For SSO examples, see below.
require 'fastcomments'
# Create Simple SSO token
user = FastComments::SSO::SimpleSSOUserData.new(
user_id: 'user-123',
email: '[email protected]',
avatar: 'https://example.com/avatar.jpg'
)
sso = FastComments::SSO::FastCommentsSSO.new_simple(user)
token = sso.create_token
puts "SSO Token: #{token}"
# Use the SSO token to make an authenticated API call
config = FastCommentsClient::Configuration.new
api_client = FastCommentsClient::ApiClient.new(config)
public_api = FastCommentsClient::PublicApi.new(api_client)
response = public_api.get_comments_public(
tenant_id: 'your-tenant-id',
url_id: 'your-page-url-id',
sso: token
)
puts "Status: #{response}"require 'fastcomments'
# Create Secure SSO token
user = FastComments::SSO::SecureSSOUserData.new(
user_id: 'user-123',
email: '[email protected]',
username: 'johndoe',
avatar: 'https://example.com/avatar.jpg'
)
api_key = 'your-api-key'
sso = FastComments::SSO::FastCommentsSSO.new_secure(api_key, user)
token = sso.create_token
puts "Secure SSO Token: #{token}"
# Use the SSO token to make an authenticated API call
config = FastCommentsClient::Configuration.new
api_client = FastCommentsClient::ApiClient.new(config)
public_api = FastCommentsClient::PublicApi.new(api_client)
response = public_api.get_comments_public(
tenant_id: 'your-tenant-id',
url_id: 'your-page-url-id',
sso: token
)
puts "Status: #{response}"Set the required environment variables:
export FASTCOMMENTS_API_KEY="your-api-key"
export FASTCOMMENTS_TENANT_ID="your-tenant-id"Run the tests:
bundle exec rspecTo update the generated client from the OpenAPI spec:
./update.shThis will download the latest OpenAPI spec from a running FastComments server (or use a local copy) and regenerate the client code.
MIT License - see LICENSE file for details
For support, please visit https://fastcomments.com/auth/my-account/help or email [email protected]