- Models and inference
- Inference quickstarts
Quickstart: send your first API request
Quickstart: send your first API request
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
- A Mistral API key, exported as MISTRAL_API_KEY. To create one, see Create your first API key.
- For Python: Python 3.10 or later and uv.
- For TypeScript: Node.js and npm.
- For curl: nothing else.
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 Requests | You hit a rate limit. | Wait, then retry with exponential backoff. |
Going further
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
Extract a validated Python object from free text with a Pydantic schema, in about 5 minutes.
Quickstart: analyze an image
Send an image with a question to a Mistral model and get a description, in about 5 minutes.
Chat completions
Generate text with the Chat Completions API, send chat messages, handle multi-turn conversations, and use OpenAI compatibility.