Keep the SDK and API key in trusted server code. Do not put an API key in browser or mobile code.
Install the package
npm i @affinity-health/sdkyarn add @affinity-health/sdkpnpm add @affinity-health/sdkbun add @affinity-health/sdkThese examples use SDK 1.15.0 and its Affinity client.
The SDK supports Bun, Node.js 20 or later, AWS Lambda, and standard worker runtimes.
Create the client
Read the API key from the process environment. Create one client and reuse it.
import { Affinity } from "@affinity-health/sdk";
const apiKey = process.env.AFFINITY_API_KEY;
if (!apiKey) throw new Error("Set AFFINITY_API_KEY");
export const affinity = new Affinity(apiKey);Use an sk_test_... key during development. Test mode accepts only synthetic clinical data.
Confirm access
Read the API key access before you make other requests.
const access = await affinity.apiKeys.getAccess();
if (access.livemode) {
throw new Error("Use a Test API key for this procedure");
}The response identifies the current mode and permitted operations.