Skip to content

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

Open
@KaziShahHamza

Description

@KaziShahHamza

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions