Quickstart: create your first API key

In this quickstart, you create an API key in Studio›Studio ↗, store it in an environment variable, and send a test request with it. It takes about 5 minutes. The other Studio quickstarts start from this key.

Before you begin

Before you begin

1. Create the key

1. Create the key

  1. Open the Studio console and go to API Keys (direct link).
  2. Select New key.

Select New key on the API Keys page

  1. Enter a Name that identifies the key, for example quickstart.
  2. Set an Expiration date.
  3. Choose a Connector access scope:
    • Shared connectors only: access only Connectors shared with the Workspace, not your private Connectors.
    • Private and shared connectors: access both your private Connectors and the Connectors shared with the Workspace.

Configure the API key with a name, an expiration date, and a connector access scope

Warning

The full key appears only once. You can't retrieve it after you close the confirmation dialog.

  1. Create the key, then copy it and store it in a password manager or a secrets vault.

2. Store the key in an environment variable

2. Store the key in an environment variable

The Mistral SDKs and the examples in these docs read the key from the MISTRAL_API_KEY environment variable. Set it in your terminal:

export MISTRAL_API_KEY="your-api-key"

The variable lasts for the current terminal session.

3. Send a test request

3. Send a test request

Send a chat completion request with the key:

curl https://api.mistral.ai/v1/chat/completions \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistral-small-latest",
    "messages": [{"role": "user", "content": "Hello, Mistral!"}]
  }'
You should see

You should see

A JSON response with the model's reply in choices[0].message.content. The reply text varies between runs. The response also includes id, created, and usage.

{
  "object": "chat.completion",
  "model": "mistral-small-latest",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "Hello! How can I assist you today?"
      }
    }
  ]
}

If you get {"detail":"Invalid API Key"} with HTTP status 401, check that MISTRAL_API_KEY holds the full key, in the terminal where you run curl.

Tip

Set an expiration date on every key and rotate keys regularly: create a new key, switch your code to it, then delete the old one. Delete a key as soon as you stop using it or suspect it has leaked. If an admin manages API keys for your organization, see API keys in the Admin docs.

Going further

Going further