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

> ## Agent Instructions
> Treat these docs and the Whop MCP server as the source of truth for Whop API behavior. Don't infer endpoint behavior from marketing pages.
> MCP server: connect https://mcp.whop.com/mcp (streamable HTTP, browser sign-in, no API key in the client config) to search these docs and read or change live Whop data. Setup: https://docs.whop.com/developer/guides/ai_and_mcp.md
> Whop CLI: install with `curl -fsSL https://whop.com/install.sh | sh` to scaffold, run, and deploy Whop apps and websites. It acts on production, not the sandbox. Overview: https://docs.whop.com/cli/overview.md Agent mode: https://docs.whop.com/cli/agent-mode.md
> Hosting: Whop serves a site or app from the build `whop apps deploy` uploads, with rollbacks, secrets, and server logs. Guide: https://docs.whop.com/developer/websites/hosting.md
> Plugins: in Claude Code, run `/plugin marketplace add whopio/plugins`, then `/plugin install whop@whop`. Guide: https://docs.whop.com/developer/guides/plugins/claude-code.md Every agent tool: https://docs.whop.com/developer/ai/overview.md
> Environments: build and test in the sandbox (API `https://sandbox-api.whop.com/api/v1`, keys at https://sandbox.whop.com/dashboard/developer, SDK environment `Sandbox`; no real money). Production (API `https://api.whop.com/api/v1`, keys at https://whop.com/dashboard/developer, SDK environment `Production`, the SDK default) moves real money, so use it only at launch. The two share no accounts, keys, products, or webhooks, and their keys look identical: never mix them. Sandbox guide: https://docs.whop.com/developer/guides/sandbox.md Comparison: https://docs.whop.com/developer/quickstart.md
> API: send `Authorization: Bearer <API_KEY>` from your server only, and pin a dated version with the `Api-Version-Date` header. OpenAPI spec: https://api.whop.com/api/v1/openapi.json
> Which API: build on the Current API, whose pages live under https://docs.whop.com/api-reference/ (start at https://docs.whop.com/api-reference/overview.md). Pages under /api-reference/legacy/ are the Legacy API. Use one only when https://docs.whop.com/api-reference/stability.md lists the resource as Legacy-only.

# 채팅 세션 만들기

> 레거시 ChatSession 레퍼런스입니다. 새로운 통합에는 @whop/elements의 Chat 그룹을 사용하세요.

## 개요

인증을 관리하고 채팅 요소를 생성합니다.

## 예제

### 기본 사용법

```typescript theme={null}
const session = whopElements.createChatSession({
  token: async () => {
    const response = await fetch("/api/token");
    const data = await response.json();
    return data.token;
  },
});

session.on("ready", () => {
  console.log("Session is ready");
});
```

### 요소 생성

```typescript theme={null}
const chatElement = session.createElement("chat-element", {
  channelId: "feed_XXXXXXXXXXXXXX",
});
chatElement.mount("#chat-container");
```

## 옵션

| 속성 | 타입 | 필수 | 기본값 | 설명 |
| - | - | - | - | - |
| `token` | `Token \| GetToken` | 예 | - | 세션에 사용할 토큰입니다. 함수가 제공되면 해당 함수를 호출하고 대기하여 토큰을 가져옵니다. 함수가 제공된 경우 토큰은 만료되기 전에 자동으로 갱신됩니다. 문자열이 제공되면 해당 문자열이 토큰으로 사용되며 자동으로 갱신되지 않습니다. 단, 새 토큰으로 'updateOptions'를 호출하여 런타임에 토큰을 업데이트할 수 있습니다. |

## 이벤트

ChatSession이 발생시키는 이벤트입니다.

`on()` 메서드를 사용하여 이러한 이벤트를 수신하세요.

### `optionsUpdated`

`updateOptions()`를 통해 세션 옵션이 업데이트되면 발생합니다.

**콜백 시그니처:** (options: `ExpandedChatSessionOptions`) => void

### `tokenRefreshed`

인증 토큰이 갱신되면 발생합니다.

**콜백 시그니처:** (token: `string`) => void

### `tokenRefreshError`

토큰 갱신이 실패하면 발생합니다.

**콜백 시그니처:** (error: `unknown`) => void

### `error`

세션 작동 중에 오류가 발생하면 발생합니다.

**콜백 시그니처:** (error: `unknown`) => void

### `ready`

세션이 준비되고 인증되면 발생합니다.

**콜백 시그니처:** (`void`) => void

## 메서드

### `createElement(type, options)`

새 요소 인스턴스를 생성합니다.

| 매개변수 | 타입 | 설명 |
| - | - | - |
| `type` | T \| \{ type: T; } | 요소 타입(예: "chat-element") |
| `options` | ChatSessionElements\[T]\[0] | 요소별 구성 옵션 |

**반환값:** `ChatSessionElements[T][1]`

```typescript theme={null}
const element = session.createElement("chat-element", {
  channelId: "feed_XXXXXXXXXXXXXX",
  onReady: () => console.log("Element ready"),
});
element.mount("#chat-container");
```

### `updateOptions(options)`

초기화 후 세션 옵션을 업데이트합니다.

변경 사항은 모든 활성 요소에 전파됩니다.

| 매개변수 | 타입 | 설명 |
| - | - | - |
| `options` | Partial\<ChatSessionOptions> | 업데이트할 값이 담긴 부분 옵션 객체 |

### `destroy()`

세션을 삭제하고 마운트된 모든 요소를 정리합니다.

더 이상 세션이 필요하지 않을 때 이 메서드를 호출하여 리소스를 해제하세요.

### 모달 메서드

#### `showSearchModal(options, force)`

모달 오버레이에 검색 요소를 표시합니다.

기본적으로 모달은 완료(`onResultClick`) 또는 닫기(`onClose`) 시 자동으로 닫힙니다.
자동 닫힘을 방지하려면 콜백에서 `ev.preventDefault()`를 호출하세요.

| 매개변수 | 타입 | 설명 |
| - | - | - |
| `options` | [`SearchElementOptions`](https://docs.whop.com/ko/sdk/elements/search-element#searchelementoptions) \| ((modal: ModalContainer) => [`SearchElementOptions`](https://docs.whop.com/ko/sdk/elements/search-element#searchelementoptions)) | 요소 옵션 또는 모달 컨테이너를 받는 콜백 |
| `force` | Force \| undefined | - |

**반환값:** `Force extends true ? ModalContainer : ModalContainer | undefined`


## Related topics

- [채팅 인증](https://docs.whop.com/ko/developer/guides/chat/authentication.md)
- [Element 세션(레거시)](https://docs.whop.com/ko/sdk/elements/overview.md)
- [iOS 페이월 만들기](https://docs.whop.com/ko/developer/guides/ios/build-a-paywall.md)
