TypeScript SDK (Beta)

The TypeScript SDK is in beta. It exposes the Collate Prior Auth API as typed methods for Node.js. Use it in backend services, workers, route handlers, and server actions. Do not import it into browser code, because that would expose your API key.

The REST API remains the canonical integration surface while the SDK is in beta. Use the TypeScript SDK in backend services, workers, route handlers, and server actions. Do not import it into browser code.

Install

$npm install @collate/sdk

If your organization does not have SDK beta access, use the REST API guides and API reference instead.

Configure the Client

Set your API key and create a client.

$export COLLATE_API_KEY="your-sandbox-api-key"
$export COLLATE_BASE_URL="https://api.sandbox.trycollate.ai"
1import { CollateClient } from "@collate/sdk";
2
3const collate = new CollateClient({
4 apiKey: process.env.COLLATE_API_KEY,
5 environment: process.env.COLLATE_BASE_URL ?? "https://api.sandbox.trycollate.ai",
6});

Create an Authorization

Create exactly one authorization for one payer submission. Use a fresh idempotency key for each new create request.

1import { randomUUID } from "node:crypto";
2
3const authorization = await collate.authorizations.create({
4 "Idempotency-Key": randomUUID(),
5 providerNpi: "1234567893",
6 request: {
7 payerId: "payer_aetna",
8 coverageState: "CA",
9 requestType: "drug",
10 serviceCode: { system: "hcpcs", code: "J0791" },
11 },
12 policy: { finalSubmission: "requires_approval" },
13 metadata: { externalCaseId: "case_789" },
14});
15
16console.log(authorization.id);
17console.log(authorization.status);
18console.log(authorization.nextAction?.type);

Resolve Requirements

When the authorization asks for requirements, patch answers or attach files. Send the current version as If-Match.

1let auth = authorization;
2
3auth = await collate.authorizations.answers.patch(auth.id, {
4 "If-Match": `"${auth.version}"`,
5 answers: {
6 item: [
7 {
8 linkId: "coverage.member_id",
9 answer: [{ valueString: "W123456789" }],
10 },
11 {
12 linkId: "request.diagnosis_code",
13 answer: [{ valueString: "D57.1" }],
14 },
15 ],
16 },
17});

The response is the updated authorization. If requirements cleared, the next action usually changes to continue_authorization or approve_submission.

Attach a File

The API returns a single-use upload target. Upload the bytes to that URL, then complete and attach the file.

1import { readFile } from "node:fs/promises";
2
3const created = await collate.files.create({
4 fileName: "clinical-note.pdf",
5 contentType: "application/pdf",
6 purpose: "authorization_attachment",
7});
8
9const bytes = await readFile("clinical-note.pdf");
10const upload = await fetch(created.upload.url, {
11 method: created.upload.method,
12 headers: created.upload.headers,
13 body: bytes,
14});
15
16if (!upload.ok) {
17 throw new Error(`Upload failed: ${upload.status}`);
18}
19
20const file = await collate.files.complete(created.file.id);
21
22auth = await collate.authorizations.attachments.create(auth.id, {
23 "If-Match": `"${auth.version}"`,
24 fileId: file.id,
25 documentTypes: ["clinical_notes"],
26});

Confirm the Current Action

Confirm only when nextAction.type is continue_authorization or approve_submission.

1const confirmBody =
2 auth.nextAction?.type === "approve_submission"
3 ? { acceptedReviewSnapshotId: auth.submission.reviewSnapshot?.id }
4 : null;
5
6auth = await collate.authorizations.confirm(auth.id, {
7 "If-Match": `"${auth.version}"`,
8 body: confirmBody,
9});

After confirmation, poll the authorization until it needs another action, waits on the payer, completes, or cancels.

1const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
2
3while (auth.status === "processing" || auth.status === "waiting_on_payer") {
4 await sleep(auth.status === "processing" ? 5_000 : 30_000);
5 auth = await collate.authorizations.retrieve(auth.id);
6}

Error Handling

The SDK throws CollateError for API errors.

1import { CollateClient, CollateError } from "@collate/sdk";
2
3try {
4 const auth = await collate.authorizations.retrieve("auth_123");
5 console.log(auth.status);
6} catch (error) {
7 if (error instanceof CollateError) {
8 console.error(error.statusCode);
9 console.error(error.body);
10 }
11 throw error;
12}

Use error.body.error.code for recovery logic when the body is the standard Collate error envelope. Common recoverable codes are version_conflict, review_stale, requirements_not_satisfied, and live_session_not_available.

Framework Usage

Use the SDK only on the server.

1// app/api/authorizations/route.ts
2import { CollateClient } from "@collate/sdk";
3import { NextResponse } from "next/server";
4
5const collate = new CollateClient({
6 apiKey: process.env.COLLATE_API_KEY,
7 environment: process.env.COLLATE_BASE_URL ?? "https://api.sandbox.trycollate.ai",
8});
9
10export async function POST(request: Request) {
11 const body = await request.json();
12
13 const authorization = await collate.authorizations.create({
14 "Idempotency-Key": body.externalCaseId,
15 providerNpi: body.providerNpi,
16 request: body.request,
17 policy: { finalSubmission: "requires_approval" },
18 metadata: { externalCaseId: body.externalCaseId },
19 });
20
21 return NextResponse.json({ id: authorization.id, status: authorization.status });
22}

Method Map

TypeScript method groupCommon methods
authorizationscreate, retrieve, list, confirm, cancel
authorizations.answerspatch
authorizations.attachmentscreate, list, delete
authorizationRouteslist, resolve
filescreate, complete, retrieve, download
payers, providerslist, retrieve
authorizations.liveSessionretrieve
authorizations.manualHandoffscreate, retrieve, complete, unableToComplete, revoke

See the API reference for exact request and response fields.