Groq API TypeScript SDK

1.1.2 · active · verified Sun Apr 19

The `groq-sdk` package provides the official TypeScript library for interacting with the Groq API, offering convenient access to Groq's REST API from both server-side TypeScript and JavaScript environments. It is currently at stable version `1.1.2`, released on 2026-03-25. The library exhibits a rapid release cadence, with frequent minor versions and patches, often coinciding with updates to the underlying Groq API. Key differentiators include comprehensive TypeScript support with precise type definitions for all request parameters and response fields, ensuring a robust and type-safe development experience. The SDK is generated using Stainless and includes extensive documentation via docstrings. It supports various methods for file uploads, including `File` instances, `fs.ReadStream`, `fetch` `Response` objects, and a dedicated `toFile` helper for diverse environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the Groq client with an API key from environment variables and sends a chat completion request to an LLM, including basic error handling for API-specific errors.

import Groq from 'groq-sdk';

const client = new Groq({
  apiKey: process.env['GROQ_API_KEY'] ?? '', // Ensure GROQ_API_KEY is set in your environment
});

async function main() {
  try {
    const chatCompletion = await client.chat.completions.create({
      messages: [
        { role: 'system', content: 'You are a helpful assistant.' },
        { role: 'user', content: 'Explain the importance of low latency LLMs' }
      ],
      model: 'llama3-8b-8192', // Replace with a valid Groq model, e.g., 'llama3-8b-8192'
      temperature: 0.7,
      max_tokens: 1024
    });

    console.log(`Completion ID: ${chatCompletion.id}`);
    console.log(`First choice message: ${chatCompletion.choices[0]?.message?.content}`);
  } catch (error) {
    if (error instanceof Groq.APIError) {
      console.error(`API Error: ${error.status} - ${error.name}: ${error.message}`);
      console.error('Headers:', error.headers);
    } else {
      console.error('An unexpected error occurred:', error);
    }
  }
}

main();

view raw JSON →