DocumentationAPI ReferenceReact SDKRelease Notes
ScaleAI

Getting Started

OverviewInstallationQuick Start

Core

Provider & Setup

Hooks

AgentsOutbound CallsCall HistoryPhone NumbersIntegration CredentialsWebhooksKnowledge BasesFunction ToolsFunction Logs

Realtime

Realtime

UI

Components

Reference

TypesUtilities
Go to platform

Provider & Setup

Configure CallingAgentProvider, use its built-in query client, and handle SDK errors.

CallingAgentProvider is the root of every SDK integration. It makes your project credentials available to SDK requests and provides the SDK's built-in query client to hooks and components beneath it. Nothing in the SDK will work without it.

Import

import { CallingAgentProvider } from "@scalereal/sr-voice-engine-react-sdk";

Usage

Wrap the part of your component tree that uses SDK hooks or components with the provider. It creates and manages an SDK query client automatically, so no TanStack Query setup is required.

import { CallingAgentProvider } from "@scalereal/sr-voice-engine-react-sdk";
import { CallHistoryList } from "@scalereal/sr-voice-engine-react-sdk/ui";

export function CallHistoryFeature() {
  return (
    <CallingAgentProvider
      projectId={import.meta.env.VITE_CA_PROJECT_ID}
      secretKey={import.meta.env.VITE_CA_SECRET_KEY}
    >
      <CallHistoryList />
    </CallingAgentProvider>
  );
}

In Next.js, the provider must live in a Client Component. Pass values from NEXT_PUBLIC_ environment variables:

app/providers.tsx
"use client";

import { CallingAgentProvider } from "@scalereal/sr-voice-engine-react-sdk";
import type { ReactNode } from "react";

export function SdkProviders({ children }: { children: ReactNode }) {
  return (
    <CallingAgentProvider
      projectId={process.env.NEXT_PUBLIC_CA_PROJECT_ID!}
      secretKey={process.env.NEXT_PUBLIC_CA_SECRET_KEY!}
    >
      {children}
    </CallingAgentProvider>
  );
}

Security advisory: secretKey is sent from the browser as a Bearer token on every SDK API request. In a public-facing application where end-users could inspect network requests, consider routing SDK requests through a backend proxy that injects the key server-side. Contact your ScaleAI representative for guidance on proxy configuration.

Parameters

projectId

projectId: string

The project identifier used to scope API requests. The provider trims leading and trailing whitespace and throws a CallingAgentError with code missing-config when the resulting value is empty.


secretKey

secretKey: string

The service credential sent as a Bearer token on every SDK HTTP request. The provider trims whitespace and throws CallingAgentError with code missing-config when the resulting value is empty.


queryClient

queryClient?: QueryClient

An optional TanStack Query client. The provider uses its built-in client when this prop is omitted. Pass your application's client only if you want SDK queries to share its cache, devtools, and default options — see Use your own QueryClient below.


children

children: ReactNode

The React subtree that can access the provider's context and query client. All SDK hooks and components must be rendered inside this subtree.


useCallingAgent

useCallingAgent returns the active provider context — projectId, secretKey, and queryClient. Use it to build components that respond to the SDK's configuration or to access the query client directly.

import { useCallingAgent } from "@scalereal/sr-voice-engine-react-sdk";

useCallingAgent must be called inside a component that is rendered beneath CallingAgentProvider. Calling it outside the provider throws a CallingAgentError with code missing-config.

Realistic example — project-scoped analytics banner

The following component reads projectId from the provider and fetches project-level metadata to display a contextual banner:

import { useCallingAgent } from "@scalereal/sr-voice-engine-react-sdk";
import { useQuery } from "@tanstack/react-query";

interface ProjectMeta {
  name: string;
  plan: "starter" | "pro" | "enterprise";
}

async function fetchProjectMeta(projectId: string): Promise<ProjectMeta> {
  const res = await fetch(`/api/projects/${projectId}/meta`);
  if (!res.ok) throw new Error("Failed to load project metadata");
  return res.json();
}

export function ProjectBanner() {
  const { projectId, queryClient } = useCallingAgent();

  const { data, isLoading } = useQuery(
    {
      queryKey: ["project-meta", projectId],
      queryFn: () => fetchProjectMeta(projectId),
    },
    queryClient,
  );

  if (isLoading) return null;

  return (
    <div className="project-banner">
      <strong>{data?.name}</strong>
      <span className="plan-badge">{data?.plan}</span>
    </div>
  );
}

By passing queryClient from the hook to useQuery, the query participates in the same cache as SDK queries — useful for coordinating invalidations.


Use your own QueryClient

The SDK's built-in query client is ready to use without any extra setup. If your app already uses TanStack Query and you want SDK queries to share that cache, pass the same client to CallingAgentProvider and QueryClientProvider:

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { CallingAgentProvider } from "@scalereal/sr-voice-engine-react-sdk";
import type { ReactNode } from "react";

const queryClient = new QueryClient();

export function AppProviders({ children }: { children: ReactNode }) {
  return (
    <QueryClientProvider client={queryClient}>
      <CallingAgentProvider
        projectId={import.meta.env.VITE_CA_PROJECT_ID}
        secretKey={import.meta.env.VITE_CA_SECRET_KEY}
        queryClient={queryClient}
      >
        {children}
      </CallingAgentProvider>
    </QueryClientProvider>
  );
}

When you use the SDK's built-in client, SDK queries are isolated from your app's own query cache. For example, they won't appear in the app's React Query devtools and cannot be invalidated through the app's client. Sharing a client also lets SDK queries use your app's default query options.

When sharing a client, SDK query keys are namespaced under ["scalereal", ...] to avoid colliding with your application's own keys.


Error handling

All SDK configuration and API errors are surfaced as CallingAgentError. Use toCallingAgentError to safely convert any unknown thrown value before inspecting it:

import {
  CallingAgentError,
  toCallingAgentError,
} from "@scalereal/sr-voice-engine-react-sdk";

async function loadData() {
  try {
    await someSDKOperation();
  } catch (error) {
    const sdkError = toCallingAgentError(error);

    switch (sdkError.code) {
      case "missing-config":
        // Provider props were empty, or a hook was called outside the provider.
        console.error("SDK is not configured correctly.");
        break;
      case "unauthorized":
        // SecretKey was rejected — likely expired or invalid.
        console.error("Invalid SecretKey. Check your credentials.");
        break;
      case "forbidden":
        // Valid credential, but lacks permission for this resource.
        console.error("Access denied for this project resource.");
        break;
      case "not-found":
        console.error("The requested resource does not exist.");
        break;
      case "network":
        console.error("Could not reach the ScaleAI service. Check connectivity.");
        break;
      default:
        console.error("An unexpected error occurred:", sdkError.message);
    }
  }
}

Error code reference

CodeTrigger
missing-configprojectId or secretKey is empty after trimming, or an SDK hook is called outside CallingAgentProvider.
unauthorizedThe service rejected the SecretKey (HTTP 401).
forbiddenThe credential is valid but cannot access the requested resource (HTTP 403).
not-foundThe requested resource does not exist (HTTP 404).
networkThe service could not be reached (network failure or timeout).
unknownAn unclassified error or unexpected service response.

CallingAgentError exposes:

  • code — one of the values above
  • status — the HTTP status code, if applicable
  • message — a human-readable description

Related pages

  • Installation — Authenticate to AWS CodeArtifact and install the SDK.
  • Quick Start — Render a call history list in Vite or Next.js.
  • Call History — Data hooks that depend on the provider.
  • Components — UI components rendered inside the provider subtree.

Quick Start

Quick start guide on setting up the ScaleAI React SDK on your project.

Agents

Query, create, update, publish, version, and generate prompts for AI voice agents using the React SDK.

On this page

ImportUsageParametersprojectIdsecretKeyqueryClientchildrenuseCallingAgentRealistic example — project-scoped analytics bannerUse your own QueryClientError handlingError code referenceRelated pages