Ainative React Sdk

Other

Use @ainative/react-sdk to add AI chat and credits to React apps. Use when (1) Installing @ainative/react-sdk, (2) Using the useChat hook for chat completions, (3) Displaying credit balance with useCredits, (4) Setting up AINativeProvider, (5) Handling loading/error states in chat UI. Published npm package v1.0.1.

Install

openclaw skills install ainative-react-sdk

@ainative/react-sdk

React hooks and components for AINative — chat completions, credit tracking, and managed sessions.

Install

npm install @ainative/react-sdk

Setup: AINativeProvider

Wrap your app (or a subtree) with the provider:

import { AINativeProvider } from '@ainative/react-sdk';

function App() {
  return (
    <AINativeProvider config={{ apiKey: 'ak_your_key' }}>
      <YourApp />
    </AINativeProvider>
  );
}

useChat Hook

import { useChat } from '@ainative/react-sdk';
import type { Message } from '@ainative/react-sdk';

function ChatUI() {
  const { messages, isLoading, error, sendMessage } = useChat({
    model: 'claude-3-5-sonnet-20241022',
    temperature: 0.7,
    max_tokens: 1024,
  });

  const handleSend = async (input: string) => {
    await sendMessage([
      ...messages,
      { role: 'user', content: input }
    ]);
  };

  return (
    <div>
      {messages.map((msg, i) => (
        <div key={i} className={`message ${msg.role}`}>
          <strong>{msg.role}:</strong> {msg.content}
        </div>
      ))}
      {isLoading && <div>Thinking...</div>}
      {error && <div className="error">Error: {error.message}</div>}
      <input
        onKeyDown={(e) => e.key === 'Enter' && handleSend(e.currentTarget.value)}
        placeholder="Type a message..."
      />
    </div>
  );
}

useChat Options

OptionTypeDefaultDescription
modelstringModel ID (e.g. claude-3-5-sonnet-20241022)
temperaturenumber0.7Randomness (0–1)
max_tokensnumber1024Max response tokens

useChat Return

FieldTypeDescription
messagesMessage[]Full conversation history
isLoadingbooleanTrue while request in flight
errorAINativeError | nullLast error, if any
sendMessage(msgs: Message[]) => Promise<...>Send next message

useCredits Hook

import { useCredits } from '@ainative/react-sdk';

function CreditsBar() {
  const { balance, isLoading, error, refetch } = useCredits();

  if (isLoading) return <div>Loading credits...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return (
    <div>
      <span>{balance?.remaining_credits} credits remaining</span>
      <span> | Plan: {balance?.plan}</span>
      <button onClick={refetch}>Refresh</button>
    </div>
  );
}

useCredits Return

FieldTypeDescription
balanceCreditBalance | nullBalance data
isLoadingbooleanFetching state
errorAINativeError | nullError state
refetch() => voidManually refresh

CreditBalance shape: { remaining_credits: number, plan: string, ... }

Exports

import {
  AINativeProvider,
  useChat,
  useCredits,
  useAINativeContext,  // access raw config/baseUrl
  type Message,
  type ChatCompletionResponse,
  type CreditBalance,
  type AINativeError,
  type UseChatOptions,
} from '@ainative/react-sdk';

Environment Variable (CRA / Vite)

REACT_APP_AINATIVE_API_KEY=ak_your_key   # CRA
VITE_AINATIVE_API_KEY=ak_your_key        # Vite
<AINativeProvider config={{ apiKey: process.env.REACT_APP_AINATIVE_API_KEY! }}>

References

  • packages/sdks/react/src/hooks/useChat.ts — Hook implementation
  • packages/sdks/react/src/hooks/useCredits.ts — Credits hook
  • packages/sdks/react/src/AINativeProvider.tsx — Provider context
  • packages/sdks/react/src/index.ts — Package exports