.env · generator · OpenAI

OpenAI .env Generator

Most AI apps use OpenAI + at least one vector DB. This generator produces a .env covering OpenAI, Anthropic, and Pinecone — the default AI stack for 2026.

.env · OpenAI · 5 keys
Customize →
OPENAI_API_KEY=sk-
ANTHROPIC_API_KEY=sk-ant-
OPENAI_MODEL=gpt-4o-mini
PINECONE_API_KEY=
PINECONE_INDEX=

Secrets regenerate on every page load. Want to combine stacks? Use the full generator.

How to use this .env in OpenAI

  1. 1.Generate OpenAI keys at platform.openai.com/api-keys — use a scoped key per project.
  2. 2.Generate Anthropic keys at console.anthropic.com → API Keys.
  3. 3.Paste everything into .env.local. OPENAI_MODEL is a runtime knob, not a key — you'll swap this often.
  4. 4.For Next.js, keep these server-only — prefixing with NEXT_PUBLIC_ would expose them in every bundle.

Gotchas

  • ⚠OpenAI keys are billed per-request — set per-project spend limits in the dashboard.
  • ⚠Never use OPENAI_API_KEY directly in client code. Proxy via an API route.
  • ⚠Rate-limit your endpoint so a leaked key doesn't drain your budget in minutes.

Common keys explained

OPENAI_API_KEY

sk-... — used by the OpenAI SDK. Server-only.

OPENAI_MODEL

gpt-4o-mini, gpt-4o, etc. Optional but handy for A/B tests.

ANTHROPIC_API_KEY

sk-ant-... — Anthropic SDK.

PINECONE_API_KEY

Pinecone vector DB key.

PINECONE_INDEX

Pinecone index name for your embeddings.

Loading and validating the .env in OpenAI

lib/ai.ts
import OpenAI from "openai";
import Anthropic from "@anthropic-ai/sdk";

// Both SDKs read their key from the environment by default:
const openai = new OpenAI();        // uses OPENAI_API_KEY
const anthropic = new Anthropic();  // uses ANTHROPIC_API_KEY

// Keep the model name in env so you can switch without a deploy
const model = process.env.OPENAI_MODEL ?? "gpt-4o-mini";

export async function summarize(text: string) {
  const res = await openai.chat.completions.create({
    model,
    messages: [{ role: "user", content: `Summarize: ${text}` }],
  });
  return res.choices[0].message.content;
}

OpenAI: env vars in development, staging and production

AI API keys are billed per token, so a leaked key costs money within minutes. Bots scan public GitHub for sk- and sk-ant- patterns. Use a separate key per environment and per service, set monthly spend limits in each provider's console, and revoke dev keys when a laptop is replaced.

Never call these APIs directly from the browser with your key. Route requests through your backend or an edge function that adds the key server-side and applies rate limits per user.

OpenAI: .env errors and fixes

401 Incorrect API key provided

The key was revoked, belongs to another organization or project, or has a trailing space or newline from copy-paste. Recreate it and paste it without quotes.

429 You exceeded your current quota

This is a billing limit, not a rate limit. Add credits or raise the project's limit in the provider console. Retrying will not help.

OpenAIError: The OPENAI_API_KEY environment variable is missing or empty

The SDK was created before dotenv loaded, or in client-side code. Load env first and keep the client on the server.

OpenAI .env FAQ

Can I use one API key for development and production?

You can, but you should not. Separate keys let you set separate spend limits and revoke a leaked dev key without breaking production.

Where do vector database keys (Pinecone, Qdrant) go?

In the same server-side .env. They give full read/write access to your index, so treat them like database passwords.

How do I check if my AI key leaked?

Run your .env through the leak checker linked below, search your git history (git log -p | grep sk-), and check the provider's usage dashboard for traffic you don't recognize.

Related tools

Other stacks