Mistral AI API Key: Create, Secure, and Use
What you’ll build
Section titled “What you’ll build”By the end of this guide you will have a Mistral AI API key stored in an environment variable, billing configured, and a working Python and cURL call that returns a Mistral chat completion. Mistral’s API follows the OpenAI chat completion format, so the same client can call both providers with minimal changes.
Prerequisites
Section titled “Prerequisites”| Requirement | Notes |
|---|---|
| Mistral AI account | Sign up at console.mistral.ai |
| Verified email | Required before creating keys |
| Payment method | Credit card required for paid-tier access |
| Python 3.9+ | To run the verification samples |
Step 1 — Create the API key
Section titled “Step 1 — Create the API key”- Sign in at console.mistral.ai.
- Click API Keys in the left sidebar.
- Click Create new key, give it a descriptive name (for example
orders-portal-dev), and click Create. - Copy the full key immediately — it is shown only once. If you close the dialog without copying, delete the key and create a new one.
Step 2 — Add a payment method
Section titled “Step 2 — Add a payment method”The API returns 402 on every request until billing is configured.
- Go to Billing in the console sidebar.
- Click Add Payment Method and enter your card details.
- Optionally set a spending limit to cap runaway usage.
Step 3 — Store the key securely
Section titled “Step 3 — Store the key securely”Linux / macOS (current shell session):
export MISTRAL_API_KEY="your-key-here"Windows PowerShell (current session):
$env:MISTRAL_API_KEY = "your-key-here".env file for local development (add .env to .gitignore):
MISTRAL_API_KEY=your-key-hereLoad it in Python with python-dotenv:
from dotenv import load_dotenvload_dotenv()For production, use your platform’s secret store: AWS Secrets Manager, Azure Key Vault, GitHub Actions Secrets, or equivalent.
Step 4 — Verify the key works
Section titled “Step 4 — Verify the key works”Install the SDK, then run a minimal request.
pip install mistralaiSave as check_key.py and run with python check_key.py (Python 3.9+):
from mistralai import Mistralimport os
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
response = client.chat.complete( model="mistral-small-latest", messages=[{"role": "user", "content": "Reply with the word VERIFIED."}],)
print(response.choices[0].message.content)# Expected output: VERIFIEDOr use cURL directly:
curl https://api.mistral.ai/v1/chat/completions \ -H "Authorization: Bearer $MISTRAL_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "mistral-small-latest", "messages": [{"role": "user", "content": "Reply with the word VERIFIED."}] }'A successful response returns HTTP 200 with choices[0].message.content containing your text.
OpenAI-compatible usage
Section titled “OpenAI-compatible usage”Mistral’s API accepts the same request format as OpenAI’s chat completions endpoint. If your code already uses the OpenAI Python client, you can point it at Mistral by changing the base URL:
import openaiimport os
client = openai.OpenAI( api_key=os.environ["MISTRAL_API_KEY"], base_url="https://api.mistral.ai/v1",)
response = client.chat.completions.create( model="mistral-small-latest", messages=[{"role": "user", "content": "Reply with the word VERIFIED."}],)print(response.choices[0].message.content)This is useful when switching between providers during evaluation, but use the official mistralai SDK for production code to get Mistral-specific features and reliable error handling.
Available models
Section titled “Available models”| Model | Best for |
|---|---|
mistral-large-latest |
Complex reasoning, structured output, function calling |
mistral-small-latest |
Fast, cost-effective general-purpose tasks |
codestral-latest |
Code generation and completion |
mistral-embed |
Semantic search and RAG embeddings |
mistral-moderation-latest |
Content classification and moderation |
Model names, tiers, and pricing change. Check docs.mistral.ai/getting-started/models for the current list.
Managing keys
Section titled “Managing keys”View keys: Console › API Keys shows each key’s name, masked value, creation date, and last-used timestamp.
Revoke a key: Click the trash icon and confirm. Any application using that key immediately receives 401 Unauthorized.
Rotation policy: Create a replacement key first, roll it out to your applications, verify everything works, then delete the old one to avoid downtime.
Troubleshooting
Section titled “Troubleshooting”| Error | Likely cause | Fix |
|---|---|---|
401 Unauthorized |
Key missing, wrong value, or revoked | Confirm the Authorization: Bearer header. Re-check the key value in the console. |
402 Payment required |
No payment method on file | Add a card in Console › Billing. |
429 Too many requests |
Rate limit exceeded | Add exponential backoff. See rate limits documentation. |
404 Model not found |
Model name incorrect or not available | Check current model IDs at docs.mistral.ai. |
Next steps
Section titled “Next steps”- LLM model selection guide — choosing between Mistral, OpenAI, Anthropic, and Google
- RAG vs agentic RAG — patterns for retrieval-augmented generation using embedding models
- API Tokens overview — comparison of all supported providers
Official references
Section titled “Official references”- Mistral AI Console — key management and billing
- Mistral API documentation — versioned REST and SDK reference
- Models list — current model IDs and capabilities
- Python SDK —
mistralaipackage on PyPI