JD | 🇳🇱
JD | 🇳🇱2h ago

Convex agents, convex/chat.ts: process.env.OPENROUTER_API_KEY is undefined

I have this code in chat.ts, following a Convex Agents tutorial:
import { Agent } from "@convex-dev/agent";
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
import { components } from "./_generated/api";
import { action, mutation } from "./_generated/server";
import { v } from "convex/values";

const openrouter = createOpenRouter({
apiKey: process.env.OPENROUTER_API_KEY,
});

const DEMO_USER_ID = "demo-user";

const chatAgent = new Agent(components.agent, {
name: "chat-agent",
languageModel: openrouter.languageModel("gemini-2.5-flash"),
chat: openrouter.chat("gemini-2.5-flash"),
instructions: "You are a helpful assistant. Be concise and friendly in your responses.",
maxSteps: 10,
})

export const createThread = mutation({
args: {},
handler: async (ctx) => {
const { threadId } = await chatAgent.createThread(ctx, {
userId: DEMO_USER_ID,
});
return threadId;
}
});

export const sendMessageToAgent = action({
args: {
threadId: v.string(),
prompt: v.string(),
},
handler: async (ctx, args) => {
const { thread } = await chatAgent.continueThread(ctx, {
threadId: args.threadId,
});

const result = await thread.generateText({ prompt: args.prompt });

return result;
}
});
import { Agent } from "@convex-dev/agent";
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
import { components } from "./_generated/api";
import { action, mutation } from "./_generated/server";
import { v } from "convex/values";

const openrouter = createOpenRouter({
apiKey: process.env.OPENROUTER_API_KEY,
});

const DEMO_USER_ID = "demo-user";

const chatAgent = new Agent(components.agent, {
name: "chat-agent",
languageModel: openrouter.languageModel("gemini-2.5-flash"),
chat: openrouter.chat("gemini-2.5-flash"),
instructions: "You are a helpful assistant. Be concise and friendly in your responses.",
maxSteps: 10,
})

export const createThread = mutation({
args: {},
handler: async (ctx) => {
const { threadId } = await chatAgent.createThread(ctx, {
userId: DEMO_USER_ID,
});
return threadId;
}
});

export const sendMessageToAgent = action({
args: {
threadId: v.string(),
prompt: v.string(),
},
handler: async (ctx, args) => {
const { thread } = await chatAgent.continueThread(ctx, {
threadId: args.threadId,
});

const result = await thread.generateText({ prompt: args.prompt });

return result;
}
});
I have this in .env.local:
# Deployment used by `npx convex dev`
CONVEX_DEPLOYMENT=anonymous:anonymous-japp

CONVEX_URL=http://127.0.0.1:3210
SITE_URL=http://localhost:8081

OPENROUTER_API_KEY=<secret>

JWT_PRIVATE_KEY=<secret>
JWKS='<secret>'
# Deployment used by `npx convex dev`
CONVEX_DEPLOYMENT=anonymous:anonymous-japp

CONVEX_URL=http://127.0.0.1:3210
SITE_URL=http://localhost:8081

OPENROUTER_API_KEY=<secret>

JWT_PRIVATE_KEY=<secret>
JWKS='<secret>'
Despite that, I keep getting
CONVEX A(chat:sendMessageToAgent)] [Request ID: 7abed25820c29ad1] Server Error
Uncaught AI_LoadAPIKeyError: OpenRouter API key is missing. Pass it using the 'apiKey' parameter or the OPENROUTER_API_KEY environment variable.
CONVEX A(chat:sendMessageToAgent)] [Request ID: 7abed25820c29ad1] Server Error
Uncaught AI_LoadAPIKeyError: OpenRouter API key is missing. Pass it using the 'apiKey' parameter or the OPENROUTER_API_KEY environment variable.
3 Replies
Convex Bot
Convex Bot2h ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ian
ian2h ago
the environment variables read from your convex/ folder come from your deployment's (server) environment variables, not .env.local. run npx convex dashboard or go to dashboard.convex,.dev/deployment/settings - and you can set / list your env variables from the CLI with npx convex env list and npx convex env set OPENROUTER_API_KEY <your key>
JD | 🇳🇱
JD | 🇳🇱OP2h ago
Aah gotcha -- thank you! I added it in my dashboard and that worked 🙏

Did you find this page helpful?