> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flowsbuilt.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Installation

> Install the ChurnKit SDK in your Node.js, Next.js, or edge runtime project.

## Requirements

* Node.js 18+ (or any edge runtime with Web Crypto API)
* TypeScript 5.0+ (optional but recommended)

## Install

<CodeGroup>
  ```bash npm theme={null}
  npm install @vgpprasad91/churnkit-sdk
  ```

  ```bash yarn theme={null}
  yarn add @vgpprasad91/churnkit-sdk
  ```

  ```bash pnpm theme={null}
  pnpm add @vgpprasad91/churnkit-sdk
  ```

  ```bash bun theme={null}
  bun add @vgpprasad91/churnkit-sdk
  ```
</CodeGroup>

## Set your API key

Add your key to your environment:

```bash .env theme={null}
CHURNKIT_API_KEY=ck_live_your_key_here
```

<Tip>
  Use `ck_test_` keys in development and CI. Test keys are completely isolated from production data.
</Tip>

## Basic setup

```typescript theme={null}
import ChurnKit from '@vgpprasad91/churnkit-sdk'

const churn = new ChurnKit({
  apiKey: process.env.CHURNKIT_API_KEY!,
})
```

## Next.js setup

Create a shared client file so you don't instantiate on every request:

```typescript lib/churnkit.ts theme={null}
import ChurnKit from '@vgpprasad91/churnkit-sdk'

// Module-level singleton — instantiated once per server process
export const churn = new ChurnKit({
  apiKey: process.env.CHURNKIT_API_KEY!,
})
```

Then import `churn` in your Server Actions, Route Handlers, or API routes:

```typescript app/api/checkout/route.ts theme={null}
import { churn } from '@/lib/churnkit'

export async function POST(req: Request) {
  const { userId } = await req.json()
  await churn.event(userId, 'checkout_started')
  // ...
}
```

## Cloudflare Workers / Edge

The SDK uses only Web-standard APIs (`fetch`, `crypto.subtle`) and works on Cloudflare Workers, Vercel Edge Functions, and Deno Deploy with no modifications:

```typescript worker.ts theme={null}
import ChurnKit from '@vgpprasad91/churnkit-sdk'

export default {
  async fetch(request: Request, env: Env) {
    const churn = new ChurnKit({ apiKey: env.CHURNKIT_API_KEY })
    await churn.event('user_123', 'page_viewed')
    return new Response('ok')
  },
}
```

## Module formats

The package ships both ESM and CJS:

| Format | Entry point       |
| ------ | ----------------- |
| ESM    | `dist/index.mjs`  |
| CJS    | `dist/index.cjs`  |
| Types  | `dist/index.d.ts` |

`import` and `require` both work out of the box.
