# @auth57labs/sdk

TypeScript SDK and embeddable Web Components for **Auth57** — verified prior-authorization rule data for all 51 US states.

Full OpenAPI spec: <https://auth57labs.com/openapi.json>
Interactive API reference: <https://auth57labs.com/api-reference>

---

## Install

```bash
npm install @auth57labs/sdk
# or pnpm add @auth57labs/sdk
# or yarn add @auth57labs/sdk
```

Works in Node 18+, Deno, Bun, Cloudflare Workers, Vercel Edge, and every modern browser. Zero runtime dependencies for the core client.

## Quick start — TypeScript client

```ts
import { Auth57Client } from '@auth57labs/sdk';

const client = new Auth57Client({
  apiKey: process.env.AUTH57_API_KEY!, // grab at https://auth57labs.com/subscribe-api
});

const result = await client.paLookup({
  state: 'CA',
  program: 'medicaid_ffs',
  service: 'advanced_imaging',
});

console.log(result.result.pa_required);       // true
console.log(result.result.source_url);         // https://www.dhcs.ca.gov/...
console.log(result.next_steps);                // ordered submission guide
```

### Cross-state comparison

```ts
const compared = await client.compare({
  states: ['CA', 'TX', 'NY', 'FL', 'WA'],
  program: 'medicaid_mco',
  drug: 'glp_1_agonists',
});

console.log(compared.summary.divergence_summary);
// "3 of 5 states require PA. NY, FL are outliers."
```

### Error handling

```ts
import { Auth57Error } from '@auth57labs/sdk';

try {
  await client.paLookup({ state: 'CA', program: 'medicaid_ffs', drug: 'unknown' });
} catch (err) {
  if (err instanceof Auth57Error) {
    console.log(err.status);       // 404, 429, 401, etc.
    console.log(err.body.error);   // "Rule not found"
    if (err.status === 429) console.log(err.body.retry_after_seconds);
  }
}
```

---

## Quick start — Web Component

The component is pure Lit-based custom elements. Works anywhere — plain HTML, React, Vue, Svelte, and Salesforce Lightning (via a thin LWC wrapper).

```html
<script type="module" src="https://cdn.auth57labs.com/sdk/pa-lookup.js"></script>

<auth57-pa-lookup
  api-key="fca35353-4528-4fa8-b45f-f47f541ac091"
  state="CA"
  program="medicaid_ffs"
  service="advanced_imaging"
  auto-query>
</auth57-pa-lookup>
```

### Listening for results

```js
document.querySelector('auth57-pa-lookup').addEventListener('auth57-result', (ev) => {
  console.log(ev.detail.result.pa_required);
});
```

### Imperative API

```js
const widget = document.querySelector('auth57-pa-lookup');
widget.state = 'TX';
widget.drug = 'biologics';
await widget.query();
```

### Styling

The component exposes CSS custom properties that penetrate the Shadow DOM:

```css
auth57-pa-lookup {
  --auth57-accent: #00d4c8;
  --auth57-accent-cta: #f64d82;
  --auth57-bg: #ffffff;
  --auth57-text: #0a0e1a;
  --auth57-muted: #6b7280;
  --auth57-border: #e8e4de;
  --auth57-font: 'Your Font', system-ui, sans-serif;
}
```

---

## Salesforce Lightning

Wrap `<auth57-pa-lookup>` in a 30-line LWC to use it on any Salesforce record page or Flow screen. Full guide at <https://auth57labs.com/docs/salesforce>.

Preferred setup for no-code users: import the OpenAPI spec directly into Salesforce **External Services** — every endpoint becomes a Flow action with zero custom code. Also covered in the Salesforce guide.

---

## Types exported

```ts
import type {
  StateCode,           // 'AL' | 'AK' | ... | 'WY'
  Program,             // 'medicaid_ffs' | 'medicare_advantage' | ...
  PaType,              // 'none' | 'partial' | 'conditional' | 'full'
  PaLookupInput,
  PaLookupResponse,
  CompareInput,
  CompareResponse,
  CompareStateResult,
  PaRule,
  NextStep,
  Auth57ApiError,
} from '@auth57labs/sdk';
```

All request/response types mirror the OpenAPI spec exactly.

---

## Runtime compatibility

| Runtime         | Client | Web Component |
|-----------------|:------:|:-------------:|
| Browser (modern) | ✓      | ✓             |
| Node 18+        | ✓      | —             |
| Deno            | ✓      | —             |
| Bun             | ✓      | —             |
| Cloudflare Workers | ✓   | —             |
| Vercel Edge     | ✓      | —             |
| Salesforce LWC  | via wrapper | via wrapper |

---

## Building locally

```bash
cd sdk
npm install
npm run typecheck
npm run build
```

## License

MIT © Auth57 Labs
