Ai Engineering Core / Build First LLM Product

Setting Up OpenAI api key and .env

📘 OpenAI API Access for LLM Development

At this stage of the course, the focus shifts from running local models to setting up access to OpenAI’s API, which allows you to use some of the most powerful frontier AI models in your projects.

However, this step is optional.

  • You can continue the course using free/open-source models.

  • OpenAI is recommended because of model quality and ease of use.

  • But it is not mandatory.


🧠 Understanding a Common Confusion: ChatGPT vs OpenAI API

This is one of the biggest misconceptions beginners run into. Many people assume:

"I already pay for ChatGPT — so why can't I use the API?"

The answer is simple: they are two completely separate products.

🤖 ChatGPT — The Consumer App

This is the product built for everyday users. You've probably used it here:

It's designed for personal use with a simple subscription plan — just open it and start chatting.

⚙️ OpenAI API — The Developer Platform

This is the product built for developers and builders. You use it when creating:

  • 🤖 Chatbots

  • 🛠️ Applications & SaaS products

  • 🔄 Automation systems

  • 🧩 AI-powered workflows

Instead of a subscription, it runs on a pay-as-you-go model — you only pay for what you use, and you access it programmatically through code.

🚕 Simple Analogy

ChatGPT - Using Uber as a passenger

OpenAI API - Building your own taxi app using Uber's infrastructure


🤝 OpenAI Platform Setup

  1. Visit OpenAI platform and Sign In

  2. Click Settings icon and Then click Billing

  3. Add Billing details

    1. Click Add to credit balance

    2. If no card attached already it will ask for card details

    3. If card already attached, enter the amount (5$ or 10$) and proceed with the payment

  4. And after successful transaction click API keys

    1. Click Create new secret key

    2. Enter Name and Select Project

    3. Click Create secret key

    d. After api key is generated, Click copy and then Click Done


🔒 .env File Setup

❓ What Is a .env File?

A .env file stores secret configuration values.

Examples:

  • API keys

  • database passwords

  • access tokens

Keep secrets separate from source code.

🔐 Setting Up Your OpenAI API Key

  1. In the root of your project, create a new file called .env

  2. Add the following variable inside it:

OPENAI_API_KEY=your-api-key-here

Replace your-api-key-here with your actual API key from OpenAI

⚠️ Important: Never share your .env file or commit it to GitHub. Add .env to your .gitignore to keep your API key safe and private. 🔒


❌ Common Mistakes to Avoid

  • Using a card that blocks international transactions

  • Assuming payment failure is always OpenAI’s issue

  • Not copying the API key immediately after creation

  • Sharing or exposing the API key publicly

  • Naming the file incorrectly (.env.txt instead of .env)

  • Placing .env in the wrong folder (must be project root)

  • Misspelling OPENAI_API_KEY

  • Adding extra spaces or modifying the API key accidentally

  • Forgetting to save the .env file

  • Hardcoding API keys in source code

  • Uploading .env to GitHub/public repositories