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

Step 1: Get your API key

Step 1: Get your API key

  1. Open StudioAPI keys.
  2. Click Create new key.
  3. Give the key a name (for example, quickstart) and click Create.
  4. Copy the key to your clipboard. The key appears only once; if you lose it, generate a new one.
  5. 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 mistralai
Step 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.py
Verify

Verify

The terminal prints a short description of Mistral AI. If it doesn't, check the error table below.

ErrorCauseFix
401 UnauthorizedAPI key is incorrect or not setRun echo $MISTRAL_API_KEY to confirm the variable is set
402 Payment RequiredNo payment method on accountAdd one at Admin PanelSubscriptionsBilling
429 Too Many RequestsRate limit hitWait and retry with exponential backoff
What's next

What's next