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

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

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

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

2. Send it

uv run quickstart.py
You should see

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:

ErrorCauseFix
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

Going further