Skip to main content

REFERENCE

SDK Demos

How to use @asgard-js/core and @asgard-js/react, with demos you can try on the page.

Private

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

  1. 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");
    }}
    />
    );
    }
  2. Auth Flow​

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

    See also the Auth demo for all 7 AuthState values.

  3. Further Reading​

    For auth design on the platform side, see the Asgard developer docs:

Ready to get started?

Want to see how Asgard fits your team? Talk to us.

Contact us