# SDKs on npm

> The eight JavaScript packages of Hosting by Infaris, each with its install line and a minimal example.

Source: https://docs.infaris.com/en/pulseguard/hosting-sdks

All SDKs are on npm under `@infaris/*`, version 0.1.0. Attach the service to your site first; Hosting then sets the
variables the examples use.

| Package | For |
| --- | --- |
| `@infaris/auth-js` | Sign-in with an email code, React hooks and token checks on the server |
| `@infaris/data` | The Data API: REST on your database with Row Level Security |
| `@infaris/realtime-js` | Database changes, broadcast and presence over WebSockets |
| `@infaris/storage-js` | Files in buckets, signed URLs and uploads |
| `@infaris/kv` | KV (Valkey) over HTTP, with rate limiting and caching |
| `@infaris/vector` | Vector search with pgvector |
| `@infaris/flags` | Feature flags and Edge Config |
| `@infaris/insights` | Speed Insights and Web Analytics without cookies |

Secret keys (`ifr_sk_...`) belong on the server only.

## `@infaris/auth-js`

```bash
npm install @infaris/auth-js
```

```ts
import { createClient } from '@infaris/auth-js';

const auth = createClient(process.env.NEXT_PUBLIC_INFARIS_AUTH_URL!, process.env.NEXT_PUBLIC_INFARIS_AUTH_KEY!);
await auth.signInWithOtp({ email });
const { data, error } = await auth.verifyOtp({ email, token: code });
```

More in [Auth](hosting-auth).

## `@infaris/data`

```bash
npm install @infaris/data
```

```ts
import { createClient } from '@infaris/data';

const db = createClient(process.env.NEXT_PUBLIC_INFARIS_DATA_URL!, process.env.NEXT_PUBLIC_INFARIS_AUTH_KEY!, {
  accessToken: () => auth.getAccessToken(), // auth from @infaris/auth-js
});
const { data, error } = await db.from('todos').select('id, title').eq('done', false);
```

A table is only reachable after `hosting db expose`. More in [Data API](hosting-data-api).

## `@infaris/realtime-js`

```bash
npm install @infaris/realtime-js
```

```ts
import { createRealtimeClient } from '@infaris/realtime-js';

const rt = createRealtimeClient({
  url: process.env.NEXT_PUBLIC_INFARIS_REALTIME_URL!,
  apiKey: process.env.NEXT_PUBLIC_INFARIS_AUTH_KEY!,
  accessToken: () => auth.getAccessToken(),
});
rt.channel('todos')
  .on('postgres_changes', { event: '*', schema: 'public', table: 'todos' }, (p) => console.log(p.new))
  .subscribe();
```

Turn the table on for Realtime first. More in [Realtime](hosting-realtime).

## `@infaris/storage-js`

```bash
npm install @infaris/storage-js
```

```ts
import { createStorageClientFromEnv } from '@infaris/storage-js';

const storage = createStorageClientFromEnv(); // server, reads INFARIS_STORAGE_*
await storage.bucket.upload('invoices/0042.pdf', pdf, { contentType: 'application/pdf' });
const { data } = await storage.bucket.createSignedUrl('invoices/0042.pdf', 300);
```

More in [Storage](hosting-storage).

## `@infaris/kv`

```bash
npm install @infaris/kv
```

```ts
import { Kv } from '@infaris/kv';

const kv = Kv.fromEnv(); // KV_REST_API_URL and KV_REST_API_TOKEN
await kv.set('greeting', 'hello', { ex: 60 });
const greeting = await kv.get<string>('greeting');
```

Attach: `infaris-guard hosting kv link <kvId> --site <site>`. More in [KV and vector](hosting-kv-vector).

## `@infaris/vector`

```bash
npm install @infaris/vector pg
```

```ts
import pg from 'pg';
import { createVectorStore, mistral } from '@infaris/vector';

const docs = createVectorStore({
  db: new pg.Pool({ connectionString: process.env.DATABASE_URL }),
  table: 'docs',
  dimensions: 1024,
  embed: mistral(), // MISTRAL_API_KEY
});
await docs.upsert([{ id: 'faq-1', content: 'You can cancel every month.' }]);
const hits = await docs.search('How do I cancel?', { limit: 5 });
```

Server only. Create the table with `infaris-guard hosting db vector create <databaseId> docs --dimensions 1024`.

## `@infaris/flags`

```bash
npm install @infaris/flags
```

```ts
import { createFlagsClient } from '@infaris/flags';

const flags = createFlagsClient(); // server, reads INFARIS_FLAGS_URL and INFARIS_FLAGS_SECRET_KEY
if (await flags.getFlag('new-checkout', { userId: user.id })) {
  // new checkout
}
```

Turn on: `infaris-guard hosting config enable --site <site>`. More in [Feature flags](hosting-flags).

## `@infaris/insights`

```bash
npm install @infaris/insights
```

```tsx
import { Insights } from '@infaris/insights/next';

<Insights apiKey="ins_..." />
```

Also for React (`@infaris/insights/react`) and Vue (`@infaris/insights/vue`). On Hosting the script is added for
you; the package is for sites elsewhere. More in [Speed Insights](hosting-speed-insights).
