Skip to main content

Integration Patterns

This guide covers common patterns for integrating Erdo into different application architectures.

Next.js App Router

Server Component

Fetch data on the server:

Client Component with Streaming

Never expose your API key to the browser. Choose one of two secure streaming patterns below.
There are two ways to stream Erdo results to your frontend: Your backend creates a scoped, short-lived token. The frontend uses it to invoke directly.

Pattern 2: Proxy Streaming

Your backend proxies all requests. The API key never leaves your server.

Vercel AI SDK

For integrating Erdo with Vercel AI SDK, see the dedicated Vercel AI SDK guide. It covers:
  • Connecting Erdo’s MCP tools to any LLM via @ai-sdk/mcp
  • Rendering rich charts and tables with ErdoToolResult
  • The bridge pattern using client.getTools()

Express.js / Node.js

REST API Endpoint

SSE Streaming Endpoint

Proxying Through Your Backend

For B2B applications, you may want to proxy Erdo API requests through your own backend rather than having the client call api.erdo.ai directly. This approach:
  • Keeps your Erdo API key server-side only
  • Avoids CSP configuration for api.erdo.ai
  • Allows you to add custom authentication, logging, or rate limiting

Option 1: Custom Endpoint

Point the SDK to your own API endpoint:
Your backend then forwards requests to api.erdo.ai:

Option 2: Custom Data Fetcher

For more control over how data is fetched (e.g., using a typed API client), provide a custom dataFetcher:
When using a custom dataFetcher, the baseUrl is still used by the useThread hook. Only fetchDatasetContents calls are overridden.

Error Handling

Client-Side Error Boundary

Hook-Level Error Handling

Authentication

API Key (Server-Side)

Store your API key securely in environment variables:

Client-Side Authentication

Never expose API keys to the browser. Use ephemeral tokens or proxy streaming instead.
For client-side usage, you have two secure options:
  1. Ephemeral Tokens: Your backend creates a short-lived, scoped token using createToken(). The frontend uses this token to invoke directly. See Client Component with Streaming above.
  2. Proxy Streaming: Your backend proxies all requests to the Erdo API. The API key never leaves your server. See Proxying Through Your Backend below.

Scoped Tokens (B2B2C)

For B2B2C applications where your customers need to expose Erdo agents and datasets to their end users, use scoped tokens. Scoped tokens are:
  • Short-lived: Expire after 1-24 hours (configurable)
  • Scoped: Only grant access to specific bots and datasets
  • User-bound: Can be linked to your external user ID for thread management
This pattern is ideal for:
  • SaaS products embedding AI agents for their customers
  • Dashboards where end users should only access specific bots/datasets
  • Applications requiring fine-grained, temporary access control

Creating Scoped Tokens (Backend)

Create a scoped token from your backend using createToken():
Thread access options:
  • externalUserId (recommended): Threads created by the token holder are automatically accessible. The user can list, view, and continue their own threads across sessions.
  • threadIds: Grant access to specific pre-existing threads. Useful when you want to give a user access to threads they didn’t create (e.g., shared conversations).

Using Scoped Tokens (Frontend)

Pass the scoped token to ErdoProvider:

Using Threads with Scoped Tokens

When a token is created with externalUserId, users can create and manage persistent conversation threads:

Persisting External User IDs

To enable users to access their threads across sessions, store the external user ID in your database:
The external user ID is embedded in the token and never exposed to the client. Users cannot access other users’ threads.

Persisting Message History

For production applications, we recommend storing messages in your own database rather than fetching from Erdo each time. This gives you full control and avoids extra API calls:
Alternatively, you can fetch history from Erdo using client.getThreadMessages():

Building a Threads Sidebar

A common pattern is to show users their previous conversations in a sidebar. Here’s how to implement this:
Then use it with your chat component:

Token API Reference

CreateTokenParams:
TokenResponse:
Scoped tokens are more secure than API keys for client-side use because they:
  • Expire automatically
  • Only grant access to specific bots and datasets
  • Can be linked to external users for personalized thread access

Content Security Policy (CSP)

If your application uses Content Security Policy headers, you’ll need to allow connections to the Erdo API for the UI components to fetch data.

Required Directives

Add the following to your CSP configuration:
This allows the @erdoai/ui components to:
  • Fetch dataset contents for rendering charts and tables
  • Stream agent invocation results in real-time

Next.js Configuration

Nginx Configuration

Meta Tag (Fallback)

If you can’t configure server headers, use a meta tag:
If you’re proxying Erdo API requests through your own backend, you don’t need to add api.erdo.ai to your CSP—just ensure your proxy endpoint is allowed.