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

# Installation

> Install the Gatelit JavaScript SDK and set up your first AI request.

## Install the package

<CodeGroup>
  ```bash npm theme={null}
  npm install @gatelit/sdk
  ```

  ```bash pnpm theme={null}
  pnpm add @gatelit/sdk
  ```

  ```bash yarn theme={null}
  yarn add @gatelit/sdk
  ```
</CodeGroup>

## Import paths

The SDK exposes two separate entry points.

**`@gatelit/sdk`** — the main client for making AI requests from your frontend or backend:

```typescript theme={null}
import { GatelitClient } from "@gatelit/sdk"
```

**`@gatelit/sdk/sign`** — backend-only token signing helper:

```typescript theme={null}
import { signToken } from "@gatelit/sdk/sign"
```

<Note>
  `@gatelit/sdk/sign` is a separate import so that the Web Crypto token-signing
  code is never bundled into browser builds. Only import it from server-side
  code (Node.js, edge functions, API routes).
</Note>

## TypeScript support

The package ships its own TypeScript declarations — no `@types` package needed.
All public types (`GatelitClientConfig`, `ChatMessage`, `ChatResponse`,
`StreamChunk`, etc.) are exported from `@gatelit/sdk`.

## Minimal end-to-end setup

The typical flow is:

1. Your backend signs a short-lived token with `signToken`.
2. Your frontend fetches that token and passes it to `GatelitClient`.
3. The client attaches the token to every request and the gateway enforces your policy.

**Backend — issue a signed token** (e.g. a Next.js API route):

```typescript theme={null}
// app/api/ai-token/route.ts
import { signToken } from "@gatelit/sdk/sign"

export async function GET(req: Request) {
  // authenticate your user first
  const token = await signToken(
    { sub: "user_123", model: "openai/gpt-4o-mini", ttl: 60 },
    process.env.GATELIT_SIGNING_SECRET!
  )
  return Response.json({ token })
}
```

**Frontend — create a client and send a message:**

```typescript theme={null}
import { GatelitClient } from "@gatelit/sdk"

const client = new GatelitClient({
  gatewayUrl: "https://gateway.yourapp.com",
  getToken: async () => {
    const { token } = await fetch("/api/ai-token").then(r => r.json())
    return { token, scheme: "GatelitSigned" }
  },
})

const response = await client.chat({
  model: "openai/gpt-4o-mini",
  messages: [{ role: "user", content: "Hello!" }],
})

console.log(response.content)
```

<Tip>
  `getToken` is called before every request. Cache the token in your own
  variable and only re-fetch when it is close to expiry to avoid unnecessary
  round-trips.
</Tip>
