packages/examples/rest-api/README.md
This directory contains REST API examples for elizaOS using TypeScript web frameworks.
All examples use the canonical elizaOS implementation pattern:
runtime.messageService.handleMessage(runtime, messageMemory, callback)
Every example follows the same core pattern:
await runtime.initialize()messageService.handleMessage() to process the messageimport {
AgentRuntime,
ChannelType,
createCharacter,
createMessageMemory,
stringToUuid,
} from "@elizaos/core";
import { openaiPlugin } from "@elizaos/plugin-openai";
import sqlPlugin from "@elizaos/plugin-sql";
// Create runtime
const runtime = new AgentRuntime({
character: createCharacter({ name: "Eliza", bio: "A helpful AI assistant." }),
plugins: [sqlPlugin, openaiPlugin],
});
await runtime.initialize();
// Handle a message
const messageMemory = createMessageMemory({
id: uuidv4(),
entityId: userId,
roomId: stringToUuid("room"),
content: { text: "Hello!", source: "api", channelType: ChannelType.API },
});
await runtime.messageService?.handleMessage(runtime, messageMemory, async (content) => {
console.log("Response:", content.text);
return [];
});
| Framework | Language | Directory | Full Runtime |
|---|---|---|---|
| Express | TypeScript | express/ | Yes |
| Hono | TypeScript | hono/ | Yes |
| Elysia | TypeScript | elysia/ | Yes |
All examples expose the same REST API:
GET /Returns information about the agent.
curl http://localhost:3000/
GET /healthHealth check endpoint.
curl http://localhost:3000/health
POST /chatSend a message to the agent.
curl -X POST http://localhost:3000/chat \
-H "Content-Type: application/json" \
-d '{"message": "Hello, how are you?"}'
Response:
{
"response": "Hello! I'm doing well. How can I help you today?",
"character": "Eliza",
"userId": "generated-uuid"
}
POST /chat/stream (TypeScript only)Send a message and receive a streaming response via Server-Sent Events.
curl -X POST http://localhost:3000/chat/stream \
-H "Content-Type: application/json" \
-d '{"message": "Tell me a story"}'
cd express # or hono, elysia
bun install
OPENAI_API_KEY=your-key bun run start
| Variable | Description | Required |
|---|---|---|
OPENAI_API_KEY | OpenAI API key | Yes |
PORT | Server port (default: 3000) | No |
CHARACTER_NAME | Agent name | No |
CHARACTER_BIO | Agent bio/description | No |
DO NOT do this:
// ❌ WRONG - Never call plugin functions directly
import { generateElizaResponse } from "@elizaos/plugin-eliza-classic";
const response = generateElizaResponse(message);
DO this instead:
// ✅ CORRECT - Always use the runtime's message service
await runtime.messageService?.handleMessage(runtime, messageMemory, callback);
The message service:
shouldRespond classifier to decide whether to reply