Skip to content

The Open Standard for Generative UI

feat(cli): prompt for OpenAI API key in interactive mode (#403)

AliaksandrNazaruk9495c5c2a2b1
diff --git a/packages/openui-cli/src/commands/create-chat-app.ts b/packages/openui-cli/src/commands/create-chat-app.ts
index f24978a68..556b340e6 100644
--- a/packages/openui-cli/src/commands/create-chat-app.ts
+++ b/packages/openui-cli/src/commands/create-chat-app.ts
@@ -109,20 +109,47 @@ export async function runCreateChatApp(options: CreateChatAppOptions): Promise<v
? "bun"
: "npm";
- console.info(getStartedMessage(name, devCmd, installSkill));
+ let apiKeyWritten = false;
+ if (!options.noInteractive) {
+ const { input } = await import("@inquirer/prompts");
+ const apiKey = (
+ await input({ message: "Enter your OpenAI API key (leave blank to skip):" })
+ ).trim();
+
+ if (apiKey) {
+ fs.writeFileSync(path.join(targetDir, ".env"), `OPENAI_API_KEY=${apiKey}\n`);
+ apiKeyWritten = true;
+ }
+ }
+
+ console.info(getStartedMessage(name, devCmd, installSkill, apiKeyWritten));
}
-const getStartedMessage = (name: string, devCmd: string, skillInstalled: boolean) =>
- `
-Done!
-Get started:
+const getStartedMessage = (
+ name: string,
+ devCmd: string,
+ skillInstalled: boolean,
+ apiKeyWritten: boolean,
+) => {
+ const envInstructions = apiKeyWritten
+ ? "✅ .env file created with your API key."
+ : `touch .env
-cd ${name}
+Add your API key to .env:
+OPENAI_API_KEY=sk-your-key-here`;
-touch .env
+ const skillMessage = skillInstalled
+ ? "The OpenUI agent skill was installed.\nAI coding assistants will use it to help you build with OpenUI."
+ : "";
-Add your API key to .env:
-OPENAI_API_KEY=sk-your-key-here
+ return `${skillMessage}
+
+Done!
+Get started:
+
+${envInstructions}
-${devCmd} run dev
-${skillInstalled ? "\nThe OpenUI agent skill was installed.\nAI coding assistants will use it to help you build with OpenUI.\n" : ""}`;
+> cd ${name}
+> ${devCmd} run dev
+`;
+};