RepoGroqGroqpublished Feb 15, 2024seen 5d

groq/groq-typescript

TypeScript

Open original ↗

Captured source

source ↗
published Feb 15, 2024seen 5dcaptured 15hhttp 200method plain

groq/groq-typescript

Description: The official Node.js / Typescript library for the Groq API

Language: TypeScript

License: Apache-2.0

Stars: 251

Forks: 34

Open issues: 5

Created: 2024-02-15T07:14:43Z

Pushed: 2026-06-03T14:30:44Z

Default branch: main

Fork: no

Archived: no

README:

Groq TypeScript API Library

This library provides convenient access to the Groq REST API from server-side TypeScript or JavaScript.

The REST API documentation can be found on console.groq.com. The full API of this library can be found in [api.md](api.md).

It is generated with Stainless.

Installation

npm install groq-sdk

Usage

The full API of this library can be found in [api.md](api.md).

import Groq from 'groq-sdk';

const client = new Groq({
apiKey: process.env['GROQ_API_KEY'], // This is the default and can be omitted
});

const chatCompletion = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Explain the importance of low latency LLMs' }],
model: 'openai/gpt-oss-20b',
});

console.log(chatCompletion.id);

Request & Response types

This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:

import Groq from 'groq-sdk';

const client = new Groq({
apiKey: process.env['GROQ_API_KEY'], // This is the default and can be omitted
});

const params: Groq.Chat.CompletionCreateParams = {
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Explain the importance of low latency LLMs' },
],
model: 'openai/gpt-oss-20b',
};
const chatCompletion: Groq.Chat.ChatCompletion = await client.chat.completions.create(params);

Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.

File uploads

Request parameters that correspond to file uploads can be passed in many different forms:

  • File (or an object with the same structure)
  • a fetch Response (or an object with the same structure)
  • an fs.ReadStream
  • the return value of our toFile helper
import fs from 'fs';
import Groq, { toFile } from 'groq-sdk';

const client = new Groq();

// If you have access to Node `fs` we recommend using `fs.createReadStream()`:
await client.audio.transcriptions.create({
model: 'whisper-large-v3-turbo',
file: fs.createReadStream('/path/to/file'),
});

// Or if you have the web `File` API you can pass a `File` instance:
await client.audio.transcriptions.create({
model: 'whisper-large-v3-turbo',
file: new File(['my bytes'], 'file'),
});

// You can also pass a `fetch` `Response`:
await client.audio.transcriptions.create({
model: 'whisper-large-v3-turbo',
file: await fetch('https://somesite/file'),
});

// Finally, if none of the above are convenient, you can use our `toFile` helper:
await client.audio.transcriptions.create({
model: 'whisper-large-v3-turbo',
file: await toFile(Buffer.from('my bytes'), 'file'),
});
await client.audio.transcriptions.create({
model: 'whisper-large-v3-turbo',
file: await toFile(new Uint8Array([0, 1, 2]), 'file'),
});

Handling errors

When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of APIError will be thrown:

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: 'openai/gpt-oss-20b',
})
.catch(async (err) => {
if (err instanceof Groq.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});

Error codes are as follows:

| Status Code | Error Type | | ----------- | -------------------------- | | 400 | BadRequestError | | 401 | AuthenticationError | | 403 | PermissionDeniedError | | 404 | NotFoundError | | 422 | UnprocessableEntityError | | 429 | RateLimitError | | >=500 | InternalServerError | | N/A | APIConnectionError |

Retries

Certain errors will be automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default.

You can use the maxRetries option to configure or disable this:

// Configure the default for all requests:
const client = new Groq({
maxRetries: 0, // default is 2
});

// Or, configure per-request:
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: 'openai/gpt-oss-20b' }, {
maxRetries: 5,
});

Timeouts

Requests time out after 1 minute by default. You can configure this with a timeout option:

// Configure the default for all requests:
const client = new Groq({
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
});

// Override per-request:
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: 'openai/gpt-oss-20b' }, {
timeout: 5 * 1000,
});

On timeout, an APIConnectionTimeoutError is thrown.

Note that requests which time out will be [retried twice by default](#retries).

Advanced Usage

Accessing raw Response data (e.g., headers)

The "raw" Response returned by fetch() can be accessed through the .asResponse() method on the APIPromise type that all methods return. This method returns as soon as the headers for a successful response are received and does not consume the response body, so you are free to write custom parsing or streaming logic.

You can also use the .withResponse() method to get the raw Response along with the parsed data. Unlike .asResponse() this method consumes the body, returning once it is parsed.

const client = new Groq();

const response = 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: 'openai/gpt-oss-20b',
})
.asResponse();...

Excerpt shown — open the source for the full document.