Docs

Hooks Overview

React hooks for integrating with the ChatGPT Apps SDK

ChatGPT Apps SDK Hooks

The ChatGPT Apps SDK provides a set of React hooks for building widgets that run inside ChatGPT. These hooks enable your widget to communicate with the host ChatGPT environment, manage display modes, send messages, and more.

Installation

npm install @chatgpt-apps/sdk

Available Hooks

HookDescription
useWidgetPropsAccess props passed to your widget from the host
useDisplayModeGet the current display mode (pip, inline, fullscreen)
useMaxHeightGet the maximum height available for your widget
useCallToolCall tools/functions defined by the host
useSendMessageSend messages back to the ChatGPT conversation
useOpenExternalOpen external URLs in a new browser tab
useRequestDisplayModeRequest a change to the widget's display mode
useOpenAIGlobalAccess global OpenAI context and utilities

Usage Example

import {
  useWidgetProps,
  useDisplayMode,
  useSendMessage,
} from '@chatgpt-apps/sdk';

function MyWidget() {
  const props = useWidgetProps();
  const displayMode = useDisplayMode();
  const sendMessage = useSendMessage();

  const handleClick = () => {
    sendMessage({
      type: 'text',
      content: 'Button clicked in widget!',
    });
  };

  return (
    <div>
      <p>Display mode: {displayMode}</p>
      <button onClick={handleClick}>Send Message</button>
    </div>
  );
}

TypeScript Support

All hooks are fully typed with TypeScript. Import types from the SDK:

import type {
  WidgetProps,
  DisplayMode,
  MessagePayload,
} from '@chatgpt-apps/sdk';

Error Handling

Hooks may throw errors if used outside of a valid ChatGPT Apps SDK context. Wrap your widget with the SDK provider:

import { ChatGPTAppsProvider } from '@chatgpt-apps/sdk';

function App() {
  return (
    <ChatGPTAppsProvider>
      <MyWidget />
    </ChatGPTAppsProvider>
  );
}

On this page