packages/docs/examples-gallery/supabase.mdx
Deploy AI agents to Supabase's Deno-based edge functions.
cd examples/supabase
supabase start
supabase functions deploy eliza-chat
| Language | Directory | Features |
|---|---|---|
| TypeScript (Deno) | examples/supabase/functions/eliza-chat/ | Native Deno |
| Rust WASM | examples/supabase/functions/eliza-chat-wasm/ | WASM runtime |
// functions/eliza-chat/index.ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { AgentRuntime, ModelType } from "@elizaos/core";
import { openaiPlugin } from "@elizaos/plugin-openai";
let runtime: AgentRuntime | null = null;
async function getRuntime() {
if (runtime) return runtime;
runtime = new AgentRuntime({
character: { name: "Eliza", bio: "A helpful AI assistant." },
plugins: [openaiPlugin],
});
await runtime.initialize();
return runtime;
}
serve(async (req) => {
// Handle CORS
if (req.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "Content-Type",
},
});
}
const runtime = await getRuntime();
const { message } = await req.json();
const response = await runtime.useModel(ModelType.TEXT_LARGE, {
prompt: message,
});
return new Response(JSON.stringify({ response: String(response) }), {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
});
supabase functions serve eliza-chat --env-file .env.local
curl -X POST http://localhost:54321/functions/v1/eliza-chat \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-d '{"message": "Hello!"}'
Create .env.local:
OPENAI_API_KEY=your-key
Set secrets for production:
supabase secrets set OPENAI_API_KEY=your-key
For better performance, use the Rust WASM version:
// functions/eliza-chat-wasm/index.ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import init, { WasmAgentRuntime } from "./lib/elizaos_bg.wasm";
await init();
const runtime = await new WasmAgentRuntime(
JSON.stringify({
name: "Eliza",
bio: "A helpful AI assistant.",
}),
);
await runtime.initialize();
serve(async (req) => {
const { message } = await req.json();
const response = await runtime.use_model("TEXT_LARGE", message);
return new Response(JSON.stringify({ response }));
});
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
const supabase = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
);
// Store conversation
await supabase.from("conversations").insert({
user_id: userId,
message: userMessage,
response: aiResponse,
});