Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/groq/llms.txt

Use this file to discover all available pages before exploring further.

GroqOptions (namespace AiSdk\Groq) is an immutable value object that carries all configuration for a GroqProvider instance. Every field is declared readonly, so the object acts as a snapshot of provider settings from the moment it is constructed. You can build a GroqOptions directly via its constructor or — more commonly — via the static fromArray() factory, which also reads GROQ_API_KEY and GROQ_BASE_URL from the environment as fallbacks.

Constants

ConstantValueDescription
GroqOptions::DEFAULT_BASE_URL'https://api.groq.com/openai/v1'The production Groq REST API endpoint used when no base URL is specified.
GroqOptions::PROVIDER_NAME'groq'The canonical provider identifier used internally across the SDK for capability lookup, request tagging, and error messages.

Constructor

new GroqOptions(...)

Builds an options object from explicit values. All parameters except apiKey have sensible defaults.
Constructing GroqOptions directly
use AiSdk\Groq\GroqOptions;

$options = new GroqOptions(
    apiKey:  'gsk-...',
    baseUrl: 'https://api.groq.com/openai/v1',
    headers: ['X-Trace-ID' => 'abc123'],
);
apiKey
string
required
Your Groq API key. The key is sent as a Bearer token in the Authorization header on every request. It must be non-empty; an empty string will cause GroqOptions::fromArray() to throw before the object is constructed.
baseUrl
string
default:"https://api.groq.com/openai/v1"
The base URL prepended to all API paths. Defaults to GroqOptions::DEFAULT_BASE_URL. Useful when routing through an OpenAI-compatible proxy or a local mock server. Trailing slashes are stripped by fromArray() but not by the constructor — prefer fromArray() to normalise the value.
headers
array<string, string>
default:"[]"
Extra HTTP headers merged into every outbound request via authHeaders(). The Authorization header is always appended automatically and must not be included here.
sdk
Sdk|null
default:"null"
An optional AiSdk\Support\Sdk instance that controls the HTTP transport layer. Pass a custom Sdk to inject a test double or pre-configured HTTP client. When null, the default transport is used.

Static factory

GroqOptions::fromArray()

Builds a GroqOptions from a plain associative array, falling back to environment variables for apiKey and baseUrl. This is the factory used internally by Groq::create().
Building from an array with env-var fallback
use AiSdk\Groq\GroqOptions;

// All values read from GROQ_API_KEY and GROQ_BASE_URL env vars:
$options = GroqOptions::fromArray();

// Or override specific keys programmatically:
$options = GroqOptions::fromArray([
    'apiKey'  => 'gsk-...',
    'headers' => ['X-Custom-Header' => 'value'],
]);
config
array<string, mixed>
default:"[]"
An associative array mirroring the constructor parameters. Accepted keys: apiKey, baseUrl, headers, sdk. Unknown keys are silently ignored.
returns
GroqOptions
A fully constructed, immutable GroqOptions instance.

Environment variable fallback

fromArray() applies the following resolution order for each setting:
SettingResolution order
apiKey$config['apiKey']GROQ_API_KEY env var → exception if both missing
baseUrl$config['baseUrl']GROQ_BASE_URL env var → DEFAULT_BASE_URL
headers$config['headers'][]
sdk$config['sdk'] (must be a Sdk instance) → null
Setting environment variables
export GROQ_API_KEY=gsk-...
export GROQ_BASE_URL=https://api.groq.com/openai/v1  # optional
If apiKey is absent from both $config and the environment, fromArray() throws an exception immediately. Set GROQ_API_KEY in your environment or always pass apiKey explicitly.

Instance method

authHeaders()

Returns the complete set of HTTP headers to attach to every API request, combining the Authorization bearer token with any extra headers supplied at construction time.
Inspecting auth headers
use AiSdk\Groq\GroqOptions;

$options = new GroqOptions(
    apiKey:  'gsk-my-key',
    headers: ['X-Request-ID' => '42'],
);

print_r($options->authHeaders());
// [
//   'Authorization' => 'Bearer gsk-my-key',
//   'X-Request-ID'  => '42',
// ]
returns
array<string, string>
An associative array of HTTP header name → value pairs. Authorization is always the first entry; additional headers from the constructor follow in the order they were provided.
authHeaders() is called by GroqTextModel automatically before every HTTP request. You do not need to call it yourself unless you are building a custom transport or debugging outbound headers.

Complete configuration example

Full configuration with all options
use AiSdk\Groq;
use AiSdk\Groq\GroqOptions;
use AiSdk\Groq\GroqProvider;

// Via the Groq facade (recommended for most applications):
Groq::create([
    'apiKey'  => 'gsk-...',
    'baseUrl' => 'https://api.groq.com/openai/v1',
    'headers' => [
        'X-Trace-ID'   => 'req-abc',
        'X-App-Version' => '1.2.3',
    ],
]);

// Via GroqOptions directly (useful when managing multiple providers):
$options  = GroqOptions::fromArray(['apiKey' => 'gsk-...']);
$provider = new GroqProvider($options);

Build docs developers (and LLMs) love