The FastComments Nim SDK. You can use this to build secure and scalable backend applications that interact with FastComments, or build reactive client applications.
- Nim >= 1.6.0
- nimcrypto >= 0.5.4
nimble install fastcommentsnimble buildThis library contains the generated API client and the SSO utilities to make working with the API easier.
For the API client, there are three API modules, api_default, api_public, and api_moderation. The api_default contains methods that require your API key, and api_public contains api calls
that can be made directly from a browser/mobile device/etc without authentication. The api_moderation module contains methods for the moderator dashboard.
The api_moderation module provides an extensive suite of live and fast moderation APIs. Every api_moderation method accepts an sso parameter and can authenticate via SSO or a FastComments.com session cookie.
Important: Authenticated endpoints require your API key to be set as the x-api-key header.
import httpclient
import fastcomments
import fastcomments/apis/api_default
import fastcomments/models/model_comment_data
let client = newHttpClient()
client.headers["x-api-key"] = "your-api-key"
# Make authenticated API calls.
# Required parameters (and the request body) are positional; optional
# parameters are passed via the operation's options object.
let (response, httpResponse) = getComments(
httpClient = client,
tenantId = "your-tenant-id",
options = GetCommentsOptions(
urlId: "your-url-id",
direction: SortDirections.DESC
)
)
if response.isSome:
let resp = response.get()
if resp.comments.isSome:
echo "Found ", resp.comments.get().len, " comments"Public endpoints don't require authentication:
import httpclient
import fastcomments
import fastcomments/apis/api_public
let client = newHttpClient()
# Make public API calls.
# tenantId and urlId are required (positional); everything else is optional.
let (response, httpResponse) = getCommentsPublic(
httpClient = client,
tenantId = "your-tenant-id",
urlId = "your-url-id",
options = GetCommentsPublicOptions(
direction: SortDirections.DESC
)
)
if response.isSome:
let resp = response.get()
if resp.comments.isSome:
echo "Found ", resp.comments.get().len, " comments"Moderation endpoints power the moderator dashboard and are authenticated with an SSO token for the acting moderator:
import httpclient
import fastcomments
import fastcomments/apis/api_moderation
let client = newHttpClient()
# List comments in the moderation dashboard.
# This operation has no required parameters, so everything is optional.
let (response, httpResponse) = getApiComments(
httpClient = client,
options = GetApiCommentsOptions(
count: 30,
tenantId: "your-tenant-id",
sso: "your-sso-token"
)
)
if response.isSome:
let resp = response.get()
echo "Found ", resp.comments.len, " comments"- 401 authentication error: Make sure you set the
x-api-keyheader on your HttpClient before making DefaultAPI requests:client.headers["x-api-key"] = "your-api-key" - Wrong API class: Use
api_defaultfor server-side authenticated requests,api_publicfor client-side/public requests, andapi_moderationfor moderator dashboard requests.
All API methods in this SDK return tuples of (Option[ResponseType], Response). The first element contains the parsed response if successful, and the second element is the raw HTTP response.
Required parameters and the request body are passed positionally. The remaining optional parameters are collected into a single Api<Operation>Options object, which is the last argument. Operations with no optional parameters take no options object.
import httpclient
import options
import fastcomments
import fastcomments/apis/api_default
let client = newHttpClient()
client.headers["x-api-key"] = "your-api-key"
let (response, httpResponse) = getComments(
httpClient = client,
tenantId = "your-tenant-id",
options = GetCommentsOptions(
urlId: "your-url-id",
direction: SortDirections.DESC
)
)
if httpResponse.code == Http200:
if response.isSome:
let resp = response.get()
if resp.comments.isSome:
echo "Found ", resp.comments.get().len, " comments"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.
import fastcomments/sso
let user = newSimpleSSOUserData(
userId = "user-123",
email = "user@example.com",
avatar = "https://example.com/avatar.jpg"
)
let sso = newSimple(simpleUserData = user)
let token = sso.createToken()
echo "SSO Token: ", tokenimport fastcomments/sso
let user = newSecureSSOUserData(
userId = "user-123",
email = "user@example.com",
username = "johndoe",
avatar = "https://example.com/avatar.jpg"
)
let apiKey = "your-api-key"
let sso = newSecure(apiKey = apiKey, secureUserData = user)
let token = sso.createToken()
echo "Secure SSO Token: ", tokenSet the required environment variables:
export FASTCOMMENTS_API_KEY="your-api-key"
export FASTCOMMENTS_TENANT_ID="your-tenant-id"Run the tests:
nimble testOr run specific tests:
nim c -r tests/test_sso.nim
nim c -r tests/test_sso_integration.nimTo update the generated client from the OpenAPI spec:
./update.shMIT License - see LICENSE file for details
For support, please visit https://fastcomments.com/auth/my-account/help or email support@fastcomments.com