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

# init()

> Initialize the Analytiq SDK. Must be called once before any other function.

## Overview

`init()` sets up the SDK with your API key. It must be called **once**, at the very start of your app.

<Note>
  **React / Next.js users:** You don't need to call `init()` directly. The `useAnalytiq()` hook calls it automatically. See the [useAnalytiq reference](/api-reference/use-analytiq)
</Note>

Any events tracked before `init()` is called are automatically queued and sent as soon as `init()` finishes.

## Signature

```ts theme={null}
init(apiKey: string, options?: InitOptions): void
```

## Parameters

| Parameter                    | Type      | Required | Description                                                                                                        |
| ---------------------------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------------ |
| `apiKey`                     | `string`  | Yes      | Your project's API key from the Settings page. Starts with `pk_live_`                                              |
| `options.autoTrackPageViews` | `boolean` | Optional | If `true`, automatically fires a `page_view` event on every URL/route change via the History API. Default: `false` |
| `options.debug`              | `boolean` | Optional | If `true`, logs SDK actions to the browser console. Useful during development. Default: `false`                    |
| `options.host`               | `string`  | Optional | Override the Analytiq backend URL. Leave blank — defaults to the production server automatically.                  |

## Example (Vue / Vanilla JS)

```js theme={null}
import { init } from 'analytiq'

init(import.meta.env.VITE_ANALYTIQ_KEY, {
  autoTrackPageViews: true,  // automatically tracks every page navigation
  debug: true                // remove in production
})
```

## With environment variables

<Tabs>
  <Tab title="React / Vite">
    ```js theme={null}
    init(import.meta.env.VITE_ANALYTIQ_KEY, { autoTrackPageViews: true })
    ```
  </Tab>

  <Tab title="Next.js">
    ```js theme={null}
    init(process.env.NEXT_PUBLIC_ANALYTIQ_KEY, { autoTrackPageViews: true })
    ```
  </Tab>
</Tabs>

## What it does

1. Validates and stores your API key
2. Enables automatic page view tracking if `autoTrackPageViews: true`
3. Restores a previously identified `userId` from `localStorage` (persists across page refreshes)
4. Retries any events that were queued offline in a previous session
5. Logs `[analytiq] Initialized successfully.` to the browser console

## Important rules

<Warning>
  Call `init()` only **once** — at the root entry file of your app. Don't call it inside components or on every page.
</Warning>

* Call it **before** any `track()`, `identify()`, or `batchTrack()` calls
* If called a second time, it is safely ignored (guarded against double-init)
* **React / Next.js users:** Use `useAnalytiq()` instead — it handles `init()` for you

## TypeScript

```ts theme={null}
import { init } from 'analytiq'
import type { InitOptions } from 'analytiq'

const options: InitOptions = {
  autoTrackPageViews: true,
  debug: process.env.NODE_ENV === 'development',
  host: 'https://your-custom-backend.com' // only needed if self-hosting
}

init(import.meta.env.VITE_ANALYTIQ_KEY, options)
```
