SDK

AI21 offers Python and TypeScript libraries that simplify the process of using our LLM’s API.

Get an API key

Before you can start using the SDK, you'll need to obtain your API key from AI21 Studio.

REST URLCorresponding create methodMethod
/studio/v1/library/answerclient.library.answer.create(...)RAG Engine contextual answer
/studio/v1/chat/completionsclient.chat.completions.create(...)Jamba Large chat/completion

Python SDK

Python library GitHub repository
Example:

from ai21 import AI21

client = AI21(api_key=os.getenv('AI21_API_KEY'))  # or pass it in directly

response = client.chat.completions.create(
    model='jamba-large',
    messages=[{'role': 'user', 'content': 'Hello, please provide a brief explanation of what AI21 does, in 100 words or less"'}]
)

print(response)

TypeScript SDK

TypeScript library GitHub repository .
Example:

import { AI21 } from 'ai21';

const client = new AI21({
  apiKey: process.env.AI21_API_KEY, // or pass it in directly
});

const response = await client.chat.completions.create({
  model: 'jamba-large',
  messages: [{ role: 'user', content: 'Hello, please provide a brief explanation of what AI21 does, in 100 words or less"' }],
});

console.log(response);