Send your first API request
Create an API key, install the SDK, and send a chat completion request.
- Install the Python or TypeScript SDK
- Send a request to a Mistral model
- Print the model's response
Everything here is the foundation for the agent and RAG quickstarts that follow.
Time to complete: ~5 minutes
Prerequisites
Prerequisites
- Python 3.9+ or Node.js 18+ installed on your machine.
- A Mistral AI account. Create account ↗
- A Mistral API key. Studio is enabled in Free mode by default, with no credit card required. Follow the Activate Studio and generate an API key quickstart if you don't have one yet.
Step 1: Get your API key
Step 1: Get your API key
- Open Studio›API keys ↗.
- Click Create new key.
- Give the key a name (for example,
quickstart) and click Create. - Copy the key to your clipboard. The key appears only once; if you lose it, generate a new one.
- Set the key as an environment variable in your terminal:
export MISTRAL_API_KEY="your_api_key_here"Step 2: Install the SDK
Step 2: Install the SDK
pip install mistralaiStep 3: Send your first request
Step 3: Send your first request
Create a file and add the following code:
# quickstart.py
import os
from mistralai import Mistral
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "user", "content": "What is Mistral AI?"}
],
)
print(response.choices[0].message.content)Step 4: Run it
Step 4: Run it
python quickstart.pyVerify
Verify
The terminal prints a short description of Mistral AI. If it doesn't, check the error table below.
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | API key is incorrect or not set | Run echo $MISTRAL_API_KEY to confirm the variable is set |
402 Payment Required | No payment method on account | Add one at Admin›Subscriptions › Billing ↗ |
429 Too Many Requests | Rate limit hit | Wait and retry with exponential backoff |
What's next