Set up Mistral AI SDK and configure authentication credentials for chat completions, embeddings, and function calling.
Node.js (TypeScript/JavaScript)
# npm
npm install @mistralai/mistralai
# pnpm
pnpm add @mistralai/mistralai
# yarn
yarn add @mistralai/mistralai
Python
pip install mistralai
Environment Variables (Recommended)
# Set environment variable
export MISTRAL_API_KEY="your-api-key"
# Or create .env file
echo 'MISTRAL_API_KEY=your-api-key' >> .env
Using dotenv (Node.js)
npm install dotenv
import 'dotenv/config';
TypeScript
import Mistral from '@mistralai/mistralai';
const client = new Mistral({
apiKey: process.env.MISTRAL_API_KEY,
});
async function testConnection() {
try {
const models = await client.models.list();
console.log('Connection successful! Available models:');
models.data?.forEach(model => console.log(` - ${model.id}`));
} catch (error) {
console.error('Connection failed:', error);
}
}
testConnection();
Python
import os
from mistralai import Mistral
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
def test_connection():
try:
models = client.models.list()
print("Connection successful! Available models:")
for model in models.data:
print(f" - {model.id}")
except Exception as e:
print(f"Connection failed: {e}")
test_connection()
| Error | Cause | Solution |
|---|---|---|
| 401 Unauthorized | Invalid or missing API key | Verify key at console.mistral.ai |
| 429 Too Many Requests | Rate limit exceeded | Implement backoff, check quota |
| Network Error | Firewall or connectivity | Ensure HTTPS to api.mistral.ai allowed |
| Module Not Found | Installation failed | Run npm install or pip install again |
import Mistral from '@mistralai/mistralai';
const client = new Mistral({
apiKey: process.env.MISTRAL_API_KEY,
// Optional: custom timeout
timeout: 30000,
});
export default client;
import os
from mistralai import Mistral
client = Mistral(
api_key=os.environ.get("MISTRAL_API_KEY"),
# Optional: custom timeout
timeout=30.0,
)
function validateMistralApiKey(key: string): boolean {
// Mistral API keys are UUIDs or specific format
return key.length > 20 && !key.includes(' ');
}
After successful auth, proceed to mistral-hello-world for your first chat completion.