Sign up OpenAI

Go https://platform.openai.com?utm_source=chatgpt.com

follow the instructions, give a name and project name, choose plan, API Key Name, get a API Key.

Once the API Key generated, you have to write it down immediately. this is the unique chance you can see the Secret Key.

Setup environment

Install OpenAI SDK and Environment Variable management tools

</> Bash
# Install OpenAI SDK
pip install openai
# install environment variable management tool
pip install python-dotenv


<python>
%pip install openai
%pip install python-dotenv

then we can call LLM

from openai import OpenAI

client = OpenAI(api_key="your_api_key")

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "Hello"}
    ]
)

print(response.choices[0].message.content)

create a .env file, a general text file, its files name is “.env“,under the same project folder.

then, we can load the key this way

</> Python

from dotenv import load_dotenv
from openai import OpenAI
import os


# load openAI api key
load_dotenv()
my_api_key = os.getenv("OPENAI_API_KEY")


# A OpenAI LLM instant
client = OpenAI(
  api_key = my_api_key
)

Using OpenRouter

OpenRouter is an AI gateway/platform that lets you access many different LLMs (Large Language Models) through one API.

Traditional WayOpenRouter Way
OpenAI API → GPT modelsOpenRouter API → GPT + Claude + Gemini + DeepSeek + Llama + many others
Need separate accounts/API keysOne API key
Different API endpointsOne endpoint
Different billing systemsOne billing system

Why People Use It

  • Try Many Models
    For example:
model="openai/gpt-5"

# Later change to:
model="anthropic/claude-opus"

# or
model="deepseek/deepseek-chat"

without changing much code.

  • Lower Cost
  • One API Key
    Instead of: OpenAI Key, Anthropic Key, Google Key, DeepSeek Key, you only manage OpenRouter Key

Go https://openrouter.ai/ to open a OpenRouter account, and get Key.

using openRouter

from openai import OpenAI

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key="sk-or-xxxxxxxx"
)

response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain RAG simply"}
    ]
)

print(response.choices[0].message.content)

what’s the different?
OpenAI:

client = OpenAI(
  api_key = my openAI api_key
)

OpenRouter:

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key="my openRouter API_key"
)