mistralM

Docs

sideBar

Quickstart: send your first API request

Install the Mistral SDK and get your first answer from a Mistral model in Python, TypeScript, or curl, in about 5 minutes.
flagThis step takes ~5 minutes to complete.

In this quickstart, you send a chat completion request to a Mistral model and print its answer, in Python, TypeScript, or curl. It takes about 5 minutes. For everything a chat completion request accepts, see Chat completions.

Before you begin

Set up your environment

Create a project and install the Mistral SDK:

uv init first-request --python 3.12 && cd first-request
uv add mistralai

1. Write the request

The request names a model and sends one user message:

Create quickstart.py:

import os

from mistralai.client import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

response = client.chat.complete(
    model="mistral-small-latest",
    messages=[
        {"role": "user", "content": "In one sentence, what is an API key?"},
    ],
)

print(response.choices[0].message.content)

2. Send it

uv run quickstart.py

You should see

A one-sentence answer, printed in your terminal. The wording varies between runs:

An API key is a unique identifier used to authenticate and authorize access to an API.

With curl, the answer is the content field of choices[0].message in the JSON response.

If the request fails, check the error:

Error

Cause

Fix

401 with {"detail":"Invalid API Key"}MISTRAL_API_KEY is missing, incomplete, or revoked.Run echo $MISTRAL_API_KEY in the same terminal, then export the full key again.
429 Too Many RequestsYou hit a rate limit.Wait, then retry with exponential backoff.

Going further

Quickstart: call your own functions
illustrationRocket

Quickstart: call your own functions

Let a model call a Python function of yours and answer with its result, in about 10 minutes.

Quickstart: get structured output
illustrationPuzzle

Quickstart: get structured output

Extract a validated Python object from free text with a Pydantic schema, in about 5 minutes.

Quickstart: analyze an image
illustrationImage

Quickstart: analyze an image

Send an image with a question to a Mistral model and get a description, in about 5 minutes.

Chat completions
illustrationChatText

Chat completions

Generate text with the Chat Completions API, send chat messages, handle multi-turn conversations, and use OpenAI compatibility.