OpenAI API key setup for Python and Node

Published 2026-10-04 · Updated 2026-10-24

Current OpenAI keys start with sk-proj-, the older bare sk- format still works on old accounts but everything new is project-scoped. Here’s the two-minute setup.

Export the key

export OPENAI_API_KEY="sk-proj-..."

Add that line to ~/.zshrc or ~/.bashrc to keep it between sessions. In a project, use a .env file and a loader like python-dotenv, and make sure .env is in your .gitignore.

Python

pip install openai
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY automatically

resp = client.chat.completions.create(
    model="gpt-6.1-mini",
    messages=[{"role": "user", "content": "Say hello"}],
)
print(resp.choices[0].message.content)

If you’d rather pass the key explicitly: OpenAI(api_key=os.environ["OPENAI_API_KEY"]).

Node

npm install openai
import OpenAI from "openai";

const client = new OpenAI(); // reads process.env.OPENAI_API_KEY

const resp = await client.chat.completions.create({
  model: "gpt-6.1-mini",
  messages: [{ role: "user", content: "Say hello" }],
});
console.log(resp.choices[0].message.content);

Quick sanity check with curl

curl https://api.openai.com/v1/models \
  -H "Authorization: Bearer $OPENAI_API_KEY"

A JSON list of models means the key is live. 401 means it’s wrong or revoked; 429 means you’re over quota.

Mistakes that bite everyone

Once the models call returns 200, the rest is ordinary API work, swap the model id, stream if you need it, and watch the usage dashboard if you care about spend.