Skip to content

GPTScript Error: "Please provide your OpenAI API key" despite setting process.env.GPTSCRIPT_API_KEY #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KaziShahHamza opened this issue Mar 15, 2025 · 0 comments

Comments

@KaziShahHamza
Copy link

Description:

I am running a Node.js Express server that executes a GPTScript file (story.gpt) using @gptscript-ai/gptscript. However, when I make a request, I get the following error:

Error occurred during GPTScript execution: Error: prompt occurred when prompt was not allowed: Message: Please provide your OpenAI API key:
Fields: key
Sensitive: true

Despite setting process.env.GPTSCRIPT_API_KEY, GPTScript still asks for an OpenAI API key.


Code & Setup

1️⃣ package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "keywords": [],
  "license": "ISC",
  "author": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "dependencies": {
    "@gptscript-ai/gptscript": "^0.9.5",
    "cors": "^2.8.5",
    "express": "^4.21.2",
    "uniqid": "^5.4.0"
  },
  "devDependencies": {
    "nodemon": "^3.1.9"
  }
}

2️⃣ story.gpt

tools: sys.write, sys.read, sys.download, sys.find
tools: github.com/gptscript-ai/browser
args: url: Articles link
args: dir: directory to save the created files

1. Browse to the ${url} and read the page's contents.
2. Create a TL;DR text version for an Instagram reel or YouTube short. No emojis and no more than 100 words.
3. Split the created text into 3 parts and save the texts to "${dir}/story-${INDEX}.txt".

3️⃣ index.js (Relevant Code Snippet)

import express from "express";
import uniqid from "uniqid";
import fs from "fs";
import cors from "cors";
import { GPTScript } from "@gptscript-ai/gptscript";

const g = new GPTScript({ key: process.env.GPTSCRIPT_API_KEY });

const app = express();
app.use(cors());

app.get("/create-story", async (req, res) => {
  const url = req.query.url;
  if (!url) return res.status(400).json({ error: "url is required" });

  const dir = `./stories/${uniqid()}`;
  fs.mkdirSync(dir, { recursive: true });

  const opts = {
    input: `--url ${url} --dir ${dir}`,
    disableCache: true,
  };

  try {
    const run = await g.run("./story.gpt", opts);
    if (!run) return res.status(500).json({ error: "error running GPTScript" });

    const result = await run.text();
    if (!result) return res.status(500).json({ error: "no result" });

    return res.json(result);
  } catch (e) {
    console.error("Error running GPTScript:", e);
    return res.status(500).json({ error: "error occurred" });
  }
});

app.listen(8080, () => console.log("Listening on port 8080"));

Error Output

Received request with URL: https://www.astronomy.com/picture-of-the-day/photo/hide-and-seek/
Generated directory path: ./stories/16ym8g0m8aalonh
Directory created successfully
Running GPTScript with options: {
  input: '--url https://www.astronomy.com/picture-of-the-day/photo/hide-and-seek/ --dir ./stories/16ym8g0m8aalonh',
  disableCache: true
}
Error occurred during GPTScript execution: Error: prompt occurred when prompt was not allowed: 
Message: Please provide your OpenAI API key:
Fields: key
Sensitive: true
    at IncomingMessage.<anonymous> (file:///D:/VsCode/webDev%202.0/Shortify/server/node_modules/@gptscript-ai/gptscript/dist/gptscript.js:482:28)
    at IncomingMessage.emit (node:events:518:28)
    at emitErrorNT (node:internal/streams/destroy:170:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

What I've Tried:

  1. Checked if the API key is set properly:

    • Logged process.env.GPTSCRIPT_API_KEY to check if it exists.
    • Verified .env file contains GPTSCRIPT_API_KEY=your_api_key_here.
    • Restarted the server after modifying .env.
  2. Tried passing the key directly in code:

    const g = new GPTScript({ key: "your-api-key-here" });
    • This works but is insecure, so I want to use .env instead.
  3. Checked if dotenv is needed:

    • Added dotenv and required it at the top:
      import dotenv from "dotenv";
      dotenv.config();
    • Still, GPTScript does not recognize the key.
  4. Checked execution environment:

    • Running console.log(process.env.GPTSCRIPT_API_KEY) before initializing GPTScript returns undefined.

Environment Details:

  • Node.js Version: 22.14.4
  • @gptscript-ai/gptscript Version: 0.9.5
  • OS: Windows 10 / macOS / Linux

Expected Behavior:

GPTScript should recognize process.env.GPTSCRIPT_API_KEY and execute story.gpt without prompting for an API key.

Actual Behavior:

Despite setting the API key in .env, GPTScript still prompts for it, causing execution to fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant