Menu AuthGuardHelp centre
Portuguêsauthguard.pt

Developers · SDKs

TypeScript / Node.js SDK

  • 1 min read
  • SaaS · Self-hosted

The @authguard/sdk package targets Node.js and uses jose for JWTs.

Install

It is not on npm yet. From the repository's sdks/typescript folder:

npm ci && npm pack          # produces authguard-sdk-0.1.0.tgz
npm install ./authguard-sdk-0.1.0.tgz

Create a request and wait

import { IntentsClient, ApiError } from "@authguard/sdk";

const intents = new IntentsClient({
  baseUrl: "https://identity.example.com",
  clientId: process.env.AUTHGUARD_CLIENT_ID!,
  clientSecret: process.env.AUTHGUARD_CLIENT_SECRET!,
});

const { intent } = await intents.createIntent(
  { operationType: "invoice.pay", subjectEmail: "ana@example.com",
    externalReference: "INV-42", expiresInSeconds: 900,
    fields: { amount: { amount: "1250.00", currency: "EUR" } } },
  { idempotencyKey: "erp-INV-42-attempt-1" },
);
console.log(intent.comparisonCode); // show it next to the operation

const decided = await intents.waitForDecision(intent.intentId, { timeoutMs: 15 * 60_000 });

cancelIntent throws an ApiError with code === "conflict" when the request is already over.

Verify a webhook

import { verifyWebhook, WebhookVerificationError } from "@authguard/sdk";

const event = verifyWebhook(process.env.AUTHGUARD_WEBHOOK_SECRET!, req.headers, req.body); // raw body

Use the raw body (for example express.raw) and deduplicate on the webhook-id header.

Verify the receipt

verifyIntentDigest(intent.intentJson, intent.intentDigest) and checkReceipt(intent.receipt, { intentId, intentDigest, state, clientId }).

Error codes in this article

Article referencedev-sdk-typescriptquote it to support to identify this article. Checked against the product at commit 5f6607a8.