What Is an API Key?
DefinitionAn API key is a secret password that tells a company's API that the request comes from you. Without it, the API does not answer.
One key for each company. You get it from the company's website, in your account.
Never share it. Anyone who has your key can use your account.
Keep it out of your code. Put it in a
.env file, and keep that file out of Git.The .env File
You keep each key in a file named .env, and your code reads it from there.
MISTRAL_API_KEY=your key GROQ_API_KEY=your key
.env is a plain text file in your project folder. Write the name, then
=, then your key. No spaces.import os from dotenv import load_dotenv load_dotenv() client = Mistral(api_key=os.getenv("MISTRAL_API_KEY"))
load_dotenv() reads the file.
os.getenv(...) gives the key that has that name.
python-dotenv is the package that gives load_dotenv:
pip install python-dotenvDefault Key Names
LangChain finds your key by itself when you use its default name in the .env file.
| Company | Name in the .env file |
|---|---|
| OpenAI | OPENAI_API_KEY |
| Anthropic | ANTHROPIC_API_KEY |
| Google Gemini | GOOGLE_API_KEY or GEMINI_API_KEY |
| Groq | GROQ_API_KEY |
| Mistral AI | MISTRAL_API_KEY |
With LangChain, write the default name in
.env and call load_dotenv() once. LangChain reads the key from that name. You do not pass the key in your code.A different name? Then LangChain cannot find the key. Use the default name, or pass the key yourself with the
api_key setting.Free Plan Limits
Free plans have limits. When you pass one, the API answers with an error and you must wait.
| Company | Model | Requests per minute | Tokens per minute | Per day |
|---|---|---|---|---|
| Groq | openai/gpt-oss-20b | 30 | 8,000 | 1,000 requests and 2,00,000 tokens |
| Mistral | open-mistral-nemo | 2 | 40,000 | - |
Free plan numbers only. They change, so check your own limits on the provider's site: Groq limits ↗ and Mistral limits ↗.
A limit belongs to the account. Everyone who shares one API key shares one limit. Give each student their own key.
Common Key Errors
Three errors you will meet. Each one tells you what to fix.
| Error | What it means | What to do |
|---|---|---|
401 Invalid API Key | The API did not accept the key. | Check the name in .env, and copy the key again. |
403 | This model is not on your plan. Mistral says: not available in your subscription tier. | Pick a model that your plan allows. |
429 | You passed a limit of the free plan. | Wait a minute and try again. |
To check that your key is found, run this in the project folder. It prints
loaded: True and the length of the key, never the key itself.python -c "import os; from dotenv import load_dotenv; load_dotenv(); k=os.getenv('MISTRAL_API_KEY'); print('loaded:', bool(k), '| length:', len(k or ''))"