Refport
SDKs

Node.js (Server)

The refport server SDK provides track.lead(), track.sale(), and embedTokens.create() for server-side conversion reporting.

The refport package is the server-side SDK for Node.js. Use it to report lead and sale conversion events, generate embed tokens for the referral portal, and read click IDs from incoming HTTP requests.

Installation

npm install refport
# or
pnpm add refport
# or
yarn add refport

Initialising the client

Create a single Refport instance and reuse it across your application. Store your API key in an environment variable — never commit it to source control.

import { Refport } from 'refport';

export const refport = new Refport({
  apiKey: process.env.REFPORT_API_KEY!,
});

Constructor options

OptionTypeRequiredDescription
apiKeystringYesYour Refport API key
baseUrlstringNoOverride the API base URL (default: https://app.refport.co)

track.lead(params)

Report a lead event when a user signs up or creates an account.

await refport.track.lead({
  clickId: 'abc123',
  eventName: 'Sign Up',
  customerExternalId: user.id,
  customerEmail: user.email,
  customerName: user.name,
});

Parameters

ParameterTypeRequiredDescription
clickIdstringYesThe refp_id click ID from the user's cookie
eventNamestringYesDescriptive name for the lead event (e.g. "Sign Up")
customerExternalIdstringYesYour internal user ID
customerEmailstringNoUser's email address
customerNamestringNoUser's display name
customerAvatarstringNoURL to the user's avatar image
metadataRecord<string, unknown>NoArbitrary metadata attached to the event

Response

{
  eventName: string;
  click: { id: string };
  link: {
    id: string;
    domain: string;
    key: string;
    url: string;
    programId: string | null;
  };
  customer: {
    externalId: string;
    email: string;
    name: string | null;
  };
}

track.sale(params)

Report a sale event when a customer makes a purchase.

await refport.track.sale({
  clickId: 'abc123',
  customerExternalId: user.id,
  amount: 4900,        // in smallest currency unit (cents for USD)
  currency: 'usd',
  eventName: 'Purchase',
  invoiceId: 'inv_001',
  paymentProcessor: 'stripe',
});

Parameters

ParameterTypeRequiredDescription
clickIdstringYesThe refp_id click ID from the user's cookie
customerExternalIdstringYesYour internal user ID
amountnumberYesSale amount in the smallest currency unit (e.g. cents)
currencystringNoISO 4217 currency code (default: "usd")
eventNamestringNoDescriptive name for the event (default: "Purchase")
invoiceIdstringNoYour invoice or order ID for deduplication
paymentProcessor"stripe" | "shopify" | "polar" | "paddle" | "revenuecat" | "custom"NoThe payment processor used
customerEmailstringNoCustomer's email
customerNamestringNoCustomer's display name
customerAvatarstringNoURL to the customer's avatar
metadataRecord<string, unknown>NoArbitrary metadata

Response

{
  eventName: string;
  customer: {
    externalId: string;
    email: string;
    name: string | null;
  };
  sale: {
    id: string;
    amount: number;
    currency: string;
    commissionAmount: number;
    status: string;
    invoiceId: string | null;
    paymentProcessor: string;
  };
}

embedTokens.create(params)

Generate a short-lived public token for rendering the embedded referral portal. See Embedded Portal for full usage.

const token = await refport.embedTokens.create({
  tenantId: user.id,
  partner: {
    email: user.email,
    name: user.name,
  },
});
// token.publicToken — pass to <RefportEmbed>
// token.expires — Date object

Parameters

At least one of tenantId, enrollmentId, or partner must be provided.

ParameterTypeRequiredDescription
tenantIdstringNoYour internal tenant/customer ID
enrollmentIdstringNoExisting enrollment ID to bind to
partner.emailstringYes (in partner)Partner's email address
partner.namestringNoPartner's display name

Two helpers are exported for reading the refp_id click ID from server-side requests, without depending on any specific framework.

getClickIdFromRequest(req, cookieName?)

Works with standard Request objects (Fetch API), Next.js / Express / Nitro request objects, and anything with a cookies map or headers object.

import { getClickIdFromRequest } from 'refport';

const clickId = getClickIdFromRequest(req);

getClickIdFromCookie(cookieHeader, cookieName?)

Parses a raw Cookie header string directly.

import { getClickIdFromCookie } from 'refport';

const clickId = getClickIdFromCookie(req.headers.get('cookie'));

Error handling

All SDK methods throw typed errors. Catch them to handle specific failure modes gracefully.

import {
  RefportAuthError,
  RefportNotFoundError,
  RefportRateLimitError,
  RefportValidationError,
  RefportError,
} from 'refport';

try {
  await refport.track.lead({ ... });
} catch (err) {
  if (err instanceof RefportAuthError) {
    // Invalid or missing API key
  } else if (err instanceof RefportValidationError) {
    // Bad request parameters
  } else if (err instanceof RefportRateLimitError) {
    // Too many requests — back off and retry
  } else if (err instanceof RefportError) {
    // Generic error: err.status, err.code, err.message
  }
}
Error classHTTP statusWhen thrown
RefportAuthError401Invalid or missing API key
RefportValidationError400 / 422Invalid request parameters
RefportNotFoundError404Resource not found
RefportRateLimitError429Rate limit exceeded
RefportErroranyBase class for all errors

On this page