Skip to main content

What you’ll build

A reusable widget that embeds the assistant directly in your application. The widget provides:
  • A floating button that opens a chat panel when clicked
  • Real-time streaming responses based on information from your documentation
  • Message rendering with Markdown support
Users can use the widget to get help with your product without leaving your application.
Demo of the assistant widget being opened and the user typing in How do I get started? Then the assistant responds.

Prerequisites

  • The assistant enabled for your project
  • Your domain name, which appears at the end of your dashboard URL. For example, if your dashboard URL is https://app.mintlify.com/org-name/domain-name, your domain name is domain-name
  • An assistant API key
  • A Next.js app that uses the App Router
  • Basic React knowledge

Get your assistant API key

  1. Navigate to the API keys page in your dashboard.
  2. Click Create Assistant API Key.
  3. Copy the assistant API key (starts with mint_dsc_) and save it securely.
Your assistant API key controls access to your assistant quota. This tutorial keeps the key server-side using a backend proxy so the key never reaches the client bundle.

Build the widget

The widget uses the AI SDK useChat hook to call the assistant message endpoint through a route in your app.
1

Install dependencies

2

Store your API key

Store your assistant API key as a server-side environment variable. Don’t use the NEXT_PUBLIC_ prefix, which bundles the value into your client code.
.env.local
Replace mint_dsc_your_token_here with your assistant API key.
3

Add a proxy route

Create a route that adds your API key to each request and forwards it to the Mintlify API. The route streams the response back unchanged and passes through the thread headers.
app/api/assistant/route.js
Replace your-domain with your domain name.
4

Create the widget component

components/AssistantWidget.jsx
The fp field identifies the user. Use anonymous or your own user ID.
5

Add the widget to your layout

Render the widget in your root layout so it appears on every page.
app/layout.jsx
6

Start the development server

Open your application in a browser and click the Ask button to open the assistant widget.

Customization ideas

Source citations

The assistant searches your documentation before it answers. Each search appears in the message as a tool-search part, and its output.results array lists the pages it found. Extract them to show sources under each answer:
Replace https://docs.example.com with the URL of your documentation site.

Track conversation threads

Store thread IDs and thread keys to maintain conversation history across sessions. When a user creates a new conversation thread, the server returns two values in the response headers:
  • X-Thread-Id: The thread identifier
  • X-Thread-Key: A secret key for the thread (only returned once, when you create the thread)
You must capture and persist both values on the first response. On every subsequent message, include both threadId and threadKey in the request body. If you send a threadId without the corresponding threadKey, the server returns a 404 error.
You must store the X-Thread-Key header immediately. The server only returns it when you create a new thread. You cannot retrieve it later.
The transport reads its body function before each request, so the next request includes the saved values:

Add a keyboard shortcut

Allow users to open and close the widget with a keyboard shortcut: