Skip to main content

Private Bot

Connect to a private bot provider that requires an API key. The SDK handles the unauthenticated → needApiKeyauthenticated state transitions for you.

Private Bot Authentication

This demo connects to a private bot provider that requires an API key. The chatbot will automatically prompt for the key if the SDK detects an unauthenticated state.

Authentication Flow

  • API Key required for access
  • Auth state management
  • Error handling for unauthorized access
Loading chatbot...

Code Example

import { useState } from "react";
import { Chatbot } from "@asgard-js/react";
import type { AuthState } from "@asgard-js/core";

export function PrivateBot() {
const [authState, setAuthState] = useState<AuthState>("needApiKey");

return (
<Chatbot
title="Private Bot"
config={{
botProviderEndpoint: "https://...",
apiKey: authState === "authenticated" ? storedKey : undefined,
}}
customChannelId="private-channel"
authState={authState}
onApiKeySubmit={async (key) => {
// Validate the key, then switch state
setAuthState("authenticated");
}}
onAuthError={(error) => {
if (error.isAuthError) setAuthState("needApiKey");
}}
/>
);
}

Auth Flow

StepAuthStateSDK Behavior
1. InitialneedApiKeyShows API key input form
2. Submit keyTriggers onApiKeySubmit
3. Valid keyauthenticatedEstablishes SSE connection
4. Invalid keyinvalidApiKeyShows error, allows re-entry

See also the Auth demo for all 7 AuthState values.