Skip to navigation

TypeScript Agent API

Reference for @band-ai/sdk 1.0.0.

Core Agent API

Agent.create()

import { Agent, GenericAdapter, loadAgentConfigFromEnv } from "@band-ai/sdk";
const agent = Agent.create({
adapter: new GenericAdapter(async ({ message, tools }) => {
await tools.sendMessage(`Echo: ${message.content}`, [{ id: message.senderId }]);
}),
config: loadAgentConfigFromEnv(),
});
await agent.run();
OptionTypeDefaultDescription
adapterFrameworkAdapterRequiredThe adapter that handles each turn
configAgentCredentialsNone{ agentId, apiKey, wsUrl?, restUrl? }, as returned by the config loaders
agentIdstringconfig.agentIdAgent UUID; wins over config
apiKeystringconfig.apiKeyAgent API key; wins over config
wsUrlstringconfig.wsUrl, then wss://app.band.ai/api/v1/socketPhoenix socket base URL. The client appends /websocket
restUrlstringconfig.restUrl, then derived from wsUrlREST base URL. Derivation keeps the host and maps ws: to http:, wss: to https:
agentConfigAgentConfig{}Room behavior. See AgentConfig
sessionConfigSessionConfigSee SessionConfigPer-room context and retry behavior
contactConfigContactEventConfig{ strategy: "disabled" }Contact event handling. See Contacts and Discovery
loggerLoggerNoopLoggerReceives SDK logs. See Logging
shutdownTimeoutMsnumber | null30000Timeout in milliseconds that run() passes to stop() at shutdown
roomFilter(room) => booleanAccept every roomRooms for which it returns false are not joined
identity{ name, description? }Fetched from the platformAgent name and description to use instead of the platform profile
linkOptionsobjectNoneTransport options, including capabilities (see Tool capabilities) and conflictPolicy, "supersede" or "reject", for a second connection with the same agent ID
preprocessorPreprocessorDefaultPreprocessorFilters and shapes platform events before they reach the adapter
onParticipantAdded(roomId, participant) => voidNoneCalled when a participant joins a room the agent is in
onParticipantRemoved(roomId, participantId) => voidNoneCalled when a participant leaves
linkBandLinkBuilt from the credentialsA prebuilt transport, mainly for tests
contextFactory(roomId, defaults) => ExecutionContextBuilt inReplaces the per-room execution context, mainly for tests

Agent.create() throws ValidationError synchronously when the agent ID or API key is missing or empty, for example ValidationError: agentId is required and must be a non-empty string. Use loadAgentConfig() to load credentials from agent_config.yaml., and when sessionConfig is invalid. It reads no environment variables. Load credentials with loadAgentConfigFromEnv() or loadAgentConfig() first.

Agent lifecycle methods

MethodDescription
start()Authenticates, fetches the agent profile, connects the WebSocket and joins rooms. Rejects on any failure, including a 401
run(options?)start(), then blocks until SIGINT, SIGTERM or SIGHUP, then stops. options takes shutdownTimeoutMs and signals; with signals: false it installs no signal handlers and blocks until the agent stops
runForever()Blocks until a started agent stops. Before start() it rejects with RuntimeStateError: Runtime not started
stop(timeoutMs?)Stops every room and disconnects. Resolves true when the shutdown was graceful
withLifecycle(handler)Starts, runs handler(agent), and stops even when handler throws

Agent properties

PropertyTypeDescription
stateRuntimeLifecycleState{ status } with status one of not_started, starting, running, stopping, stopped, failed
isRunningbooleanDeprecated; read state instead. true while state.status is running
runtime.namestringAgent name from the platform, available after start()
runtime.agentIdstringAgent UUID

TypeScript Agent Lifecycle covers start, stop and shutdown behavior in depth.