Skip to content

Mistral AI API Key: Create, Secure, and Use

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.

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
  1. Sign in at console.mistral.ai.
  2. Click API Keys in the left sidebar.
  3. Click Create new key, give it a descriptive name (for example orders-portal-dev), and click Create.
  4. 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.

The API returns 402 on every request until billing is configured.

  1. Go to Billing in the console sidebar.
  2. Click Add Payment Method and enter your card details.
  3. Optionally set a spending limit to cap runaway usage.

Linux / macOS (current shell session):

Terminal window
export MISTRAL_API_KEY="your-key-here"

Windows PowerShell (current session):

Terminal window
$env:MISTRAL_API_KEY = "your-key-here"

.env file for local development (add .env to .gitignore):

MISTRAL_API_KEY=your-key-here

Load it in Python with python-dotenv:

from dotenv import load_dotenv
load_dotenv()

For production, use your platform’s secret store: AWS Secrets Manager, Azure Key Vault, GitHub Actions Secrets, or equivalent.

Install the SDK, then run a minimal request.

Terminal window
pip install mistralai

Save as check_key.py and run with python check_key.py (Python 3.9+):

from mistralai import Mistral
import 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: VERIFIED

Or use cURL directly:

Terminal window
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.

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 openai
import 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.

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.

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.

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.