Public Runtime API v1 Live (1.0.0-dev-pilot) Custom Transactions V2: Planned (Not Deployed) @trustlith/sdk-js: Local Prototype Only

Integration Code Examples (V1 Backend)

Use these snippets exclusively on your backend server. Replace TRUSTLITH_API_BASE_URL (for DEV: https://provisioning-dev.trustlith.com, local: http://localhost:8787), TRUSTLITH_API_KEY, and APP_PUBLIC_ORIGIN with your server environment variables.

1. Node.js / TypeScript (Built-in fetch)

Login Step-Up + Polling + Cancel

ts
const baseUrl = process.env.TRUSTLITH_API_BASE_URL!.replace(/\/+$/, "");
const apiKey = process.env.TRUSTLITH_API_KEY!;
const appOrigin = process.env.APP_PUBLIC_ORIGIN!;

export async function createLoginChallenge(externalUserId: string, deviceLabel: string) {
  const res = await fetch(`${baseUrl}/v1/challenges`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify({
      externalUserId,
      action: "login",
      context: { origin: appOrigin, device: deviceLabel },
    }),
  });
  if (!res.ok) throw new Error(`Challenge create failed: ${res.status} ${await res.text()}`);
  return (await res.json()) as { challengeId: string; status: "PENDING"; expiresAt: string };
}

export async function getChallengeStatus(challengeId: string) {
  const res = await fetch(`${baseUrl}/v1/challenges/${encodeURIComponent(challengeId)}/status`, {
    headers: { Authorization: `Bearer ${apiKey}` },
  });
  if (!res.ok) throw new Error(`Status check failed: ${res.status} ${await res.text()}`);
  return (await res.json()) as {
    challengeId: string;
    status: "PENDING" | "APPROVED" | "REJECTED" | "CANCELLED" | "EXPIRED";
    expiresAt: string;
    action: "login" | "fund_transfer";
  };
}

export async function cancelChallenge(challengeId: string) {
  const res = await fetch(`${baseUrl}/v1/challenges/${encodeURIComponent(challengeId)}/cancel`, {
    method: "POST",
    headers: { Authorization: `Bearer ${apiKey}` },
  });
  if (!res.ok) throw new Error(`Cancel failed: ${res.status} ${await res.text()}`);
  return (await res.json()) as { challengeId: string; status: "CANCELLED" };
}

Fund Transfer Authorization (type: "fund_transfer" Mandatory)

ts
export async function createFundTransferChallenge(input: {
  externalUserId: string;
  amount: string;               // e.g. "250.00"
  currency: string;             // e.g. "USD"
  recipientId: string;          // e.g. "acct_vendor_01"
  recipientDisplay: string;     // e.g. "Acme Cloud Services"
  transactionReference: string; // e.g. "INV-2026-041"
}) {
  const res = await fetch(`${baseUrl}/v1/challenges`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify({
      externalUserId: input.externalUserId,
      action: "fund_transfer",
      context: { origin: appOrigin, device: "Checkout Session" },
      transactionContext: {
        type: "fund_transfer",
        amount: input.amount,
        currency: input.currency,
        recipientId: input.recipientId,
        recipientDisplay: input.recipientDisplay,
        transactionReference: input.transactionReference,
      },
    }),
  });
  if (!res.ok) throw new Error(`Fund transfer create failed: ${res.status} ${await res.text()}`);
  return (await res.json()) as { challengeId: string; status: "PENDING"; expiresAt: string };
}

2. Python (httpx or standard library urllib)

python
import os
import json
import urllib.request

BASE_URL = os.environ["TRUSTLITH_API_BASE_URL"].rstrip("/")
API_KEY = os.environ["TRUSTLITH_API_KEY"]
APP_ORIGIN = os.environ["APP_PUBLIC_ORIGIN"]


def create_fund_transfer_challenge(external_user_id: str, amount: str, currency: str, recipient_id: str, recipient_display: str, reference: str) -> dict:
    payload = {
        "externalUserId": external_user_id,
        "action": "fund_transfer",
        "context": {"origin": APP_ORIGIN, "device": "Python Backend"},
        "transactionContext": {
            "type": "fund_transfer",
            "amount": amount,
            "currency": currency,
            "recipientId": recipient_id,
            "recipientDisplay": recipient_display,
            "transactionReference": reference,
        },
    }
    req = urllib.request.Request(
        f"{BASE_URL}/v1/challenges",
        data=json.dumps(payload).encode("utf-8"),
        headers={
            "Content-Type": "application/json",
            "Authorization": f"Bearer {API_KEY}",
        },
        method="POST",
    )
    with urllib.request.urlopen(req, timeout=10) as resp:
        return json.loads(resp.read().decode("utf-8"))

3. cURL

bash
# 1. Trigger a login challenge
curl -sS -X POST "$TRUSTLITH_API_BASE_URL/v1/challenges" \
  -H "Authorization: Bearer $TRUSTLITH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "externalUserId": "user_12345",
    "action": "login",
    "context": {
      "origin": "https://app.example.com",
      "device": "CLI Verification"
    }
  }'

# 2. Poll challenge status
curl -sS "$TRUSTLITH_API_BASE_URL/v1/challenges/<challengeId>/status" \
  -H "Authorization: Bearer $TRUSTLITH_API_KEY"

# 3. Cancel an active pending challenge
curl -sS -X POST "$TRUSTLITH_API_BASE_URL/v1/challenges/<challengeId>/cancel" \
  -H "Authorization: Bearer $TRUSTLITH_API_KEY"