Skip to main content

Function Signature

function registerNavaiExpressRoutes(
  app: Express,
  options?: RegisterNavaiExpressRoutesOptions
): void
Source: packages/voice-backend/src/index.ts:253

Description

Registers all Navai backend routes with your Express application. This includes:
  • Client secret endpoint for OpenAI Realtime authentication
  • Function listing endpoint for tool discovery
  • Function execution endpoint for running backend tools

Parameters

app
Express
required
Your Express application instance
options
RegisterNavaiExpressRoutesOptions
Configuration options for route registration

Return Value

void
void
This function does not return a value. It registers routes directly on the Express app.

Registered Routes

POST /navai/realtime/client-secret

Creates an ephemeral client secret for OpenAI Realtime API. Request Body:
{
  "model": "gpt-realtime",
  "voice": "marin",
  "instructions": "You are a helpful assistant.",
  "language": "Spanish",
  "voiceAccent": "neutral Latin American Spanish",
  "voiceTone": "friendly and professional",
  "apiKey": "sk-..."
}
Response:
{
  "value": "ek_...",
  "expires_at": 1730000000
}

GET /navai/functions

Lists all available backend functions. Response:
{
  "items": [
    {
      "name": "secret_password",
      "description": "Call exported function default.",
      "source": "src/ai/functions-modules/security.ts#default"
    }
  ],
  "warnings": []
}

POST /navai/functions/execute

Executes a backend function by name. Request Body:
{
  "function_name": "secret_password",
  "payload": {
    "args": ["abc"]
  }
}
Success Response:
{
  "ok": true,
  "function_name": "secret_password",
  "source": "src/ai/functions-modules/security.ts#default",
  "result": "..."
}
Error Response (Unknown Function):
{
  "error": "Unknown or disallowed function.",
  "available_functions": ["secret_password", "other_function"]
}

Function Discovery Patterns

The functionsFolders option accepts several pattern formats:
PatternDescriptionExample
FolderMatch all files in foldersrc/ai/functions-modules
RecursiveMatch all files recursivelysrc/ai/functions-modules/...
WildcardMatch using glob patternssrc/features/*/voice-functions
Explicit FileMatch specific filesrc/ai/functions-modules/secret.ts
CSV ListMatch multiple patternssrc/ai/functions-modules,...

Runtime Behavior

Important: The function runtime is lazy-loaded once and cached in-memory.
  • First call to list/execute functions builds the registry
  • After initial load, file changes are NOT auto-reloaded
  • Restart the process to reload function definitions

Usage Examples

Basic Setup

import express from "express";
import { registerNavaiExpressRoutes } from "@navai/voice-backend";

const app = express();
app.use(express.json());

registerNavaiExpressRoutes(app);

app.listen(3000);

Custom Configuration

import express from "express";
import { registerNavaiExpressRoutes } from "@navai/voice-backend";

const app = express();
app.use(express.json());

registerNavaiExpressRoutes(app, {
  backendOptions: {
    openaiApiKey: process.env.OPENAI_API_KEY,
    defaultModel: "gpt-realtime",
    defaultVoice: "marin",
    defaultInstructions: "You are a helpful assistant.",
    clientSecretTtlSeconds: 600,
    allowApiKeyFromRequest: false
  },
  functionsBaseDir: process.cwd(),
  functionsFolders: "src/ai/functions-modules"
});

app.listen(3000);

Custom Route Paths

import express from "express";
import { registerNavaiExpressRoutes } from "@navai/voice-backend";

const app = express();
app.use(express.json());

registerNavaiExpressRoutes(app, {
  clientSecretPath: "/api/auth/realtime-token",
  functionsListPath: "/api/tools",
  functionsExecutePath: "/api/tools/run"
});

app.listen(3000);

Disable Function Routes

import express from "express";
import { registerNavaiExpressRoutes } from "@navai/voice-backend";

const app = express();
app.use(express.json());

// Only register client secret route
registerNavaiExpressRoutes(app, {
  includeFunctionsRoutes: false
});

app.listen(3000);

Multiple Function Folders

import express from "express";
import { registerNavaiExpressRoutes } from "@navai/voice-backend";

const app = express();
app.use(express.json());

registerNavaiExpressRoutes(app, {
  functionsFolders: "src/ai/functions-modules,...,src/features/*/tools",
  includeExtensions: ["ts", "js"],
  exclude: ["**/node_modules/**", "**/dist/**", "**/*.test.ts"]
});

app.listen(3000);

Build docs developers (and LLMs) love