Node.js Server that Calls ChatGPT (OpenAI API)

Demo Project

Run the OpenAI Node Demo (Remote Linux + VS Code)

You already know how to:

  • connect to the server with VS Code Remote
  • use git
  • run Node projects

This project adds one new idea:
a secret API key must live on the server, not in the browser.
We store that secret in a file named .env.


Part A — Get the code on the server

1) Open VS Code on the remote server

  1. VS Code → Command Palette (Cmd/Ctrl+Shift+P)
  2. “Remote-SSH: Connect to Host…”
  3. Connect to:
YOUR_USERNAME@10.192.145.179

2) Open a terminal (on the server)

VS Code menu: Terminal → New Terminal

3) Clone the repo (on the server)

In that terminal:

cd ~
git clone https://github.com/jimskon/openai-node-chatgpt-demo.git
cd openai-node-chatgpt-demo

Check you’re in the right folder:

ls

You should see package.json and server.js.


Part B — Install dependencies

From inside openai-node-chatgpt-demo/:

npm install

This downloads the libraries the project needs.


Part C — API keys and .env (the important new part)

1) What is an API key?

An API key is like a password that lets a program use OpenAI.
If someone gets your key, they can spend your credits.

2) Why we do NOT put it in browser code

Anything in browser JavaScript can be seen by anyone using DevTools.
So the browser must never contain the OpenAI key.

3) What is a .env file?

A .env file is a simple text file that stores secrets for the server.

  • It lives next to server.js
  • It is read only when the server starts
  • It should not be uploaded to GitHub

Part D — Create your .env file (on the server)

Inside the project folder, create a file named .env:

nano .env

Paste these two lines (replace the key):

OPENAI_API_KEY=sk-PASTE_YOUR_KEY_HERE
OPENAI_MODEL=gpt-4o-mini

Save and exit nano:

  • Save: Ctrl+O then Enter
  • Exit: Ctrl+X

Where do you get the OpenAI API key? (Required)

To run this project, you must create your own OpenAI API key.
This key lets the server send requests to OpenAI on your behalf.

Step 1: Create an OpenAI account

  1. Open a browser and go to:
https://platform.openai.com/
  1. Sign in or create an account using your email or Google login.

Step 2: Go to the API Keys page

Once logged in:

  1. Click your profile icon (top-right)
  2. Choose View API keys
    • or go directly to:
https://platform.openai.com/api-keys

Step 3: Create a new API key

  1. Click Create new secret key
  2. Copy the key immediately
    (You will not be able to see it again.)

The key will look something like:

sk-xxxxxxxxxxxxxxxxxxxxxxxx

⚠️ Treat this key like a password.


Step 4: Enable billing / credits (important)

The API will not work unless your account has credits.

  1. Go to:
https://platform.openai.com/account/billing
  1. Add a small amount of prepaid credit (e.g., $5–$10)

Notes:

  • OpenAI does not guarantee free credits
  • If you see “insufficient_quota” errors, this is why

Step 5: Put the key in your .env file (server only)

In your project folder, edit .env:

OPENAI_API_KEY=sk-your-real-key-here
OPENAI_MODEL=gpt-4o-mini

Rules:

  • Never put the key in browser JavaScript
  • Never commit .env to GitHub
  • Never post your key in chat or screenshots

Step 6: Restart the server

Any time you change .env, you must restart:

Ctrl+C
npm start

If something goes wrong

Common error messages and what they mean:

Error messageMeaning
401 UnauthorizedAPI key missing or wrong
insufficient_quotaNo credits / billing not enabled
Invalid API keyKey copied incorrectly

Why we are doing this (one sentence)

This project demonstrates how real applications safely use AI APIs without exposing secrets to users.

Do not post the key in Slack. Do not commit it to GitHub.


Part E — Start the server

First you must install dotenv:
npm install dotenv

Run:

PORT=41xx npm start

Leave it running.

If it’s working, you’ll see something like “listening on port 3000”.


Part F — Open the web page in your browser

http://10.192.145.179:41xx

Open that in your laptop browser.


Part G — Test it

  1. Type a prompt in the webpage
  2. Submit
  3. You should see a response

If it breaks: the top 3 fixes

1) “Unauthorized” / “No API key”

Your .env file is missing or wrong.

Fix:

  • Stop server: Ctrl+C
  • Edit .env: nano .env
  • Restart: npm start

2) You edited .env but nothing changed

Node reads .env only when it starts.

Fix:

  • Stop server: Ctrl+C
  • Restart: npm start

3) The page won’t load

Use VS Code port forwarding (Part F). That avoids network/firewall issues.

Scroll to Top