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.

The Groq provider is configured through the GroqOptions class, which is constructed automatically when you call Groq::create(). Every option can be supplied either as a PHP array passed to Groq::create() or as an environment variable, so you can keep secrets out of your codebase and switch environments without touching application code.

Environment Variables

The simplest way to configure the provider is via environment variables. Set them once in your .env file or server environment, and the provider picks them up automatically — no code changes needed between environments.
VariableDescriptionDefault
GROQ_API_KEYYour Groq API key (begins with gsk-). Required — the provider will throw if this is absent and no apiKey was passed programmatically.(none — required)
GROQ_BASE_URLOverride the API base URL. Useful for proxies or local mock servers. Trailing slashes are stripped automatically.https://api.groq.com/openai/v1
.env
GROQ_API_KEY=gsk-your-api-key-here
GROQ_BASE_URL=https://api.groq.com/openai/v1

Programmatic Configuration

Pass a configuration array to Groq::create() when you need runtime control — for instance, when loading credentials from a secrets manager or when writing tests that inject a custom HTTP client.
Programmatic setup
use AiSdk\Groq;

$provider = Groq::create([
    'apiKey'  => 'gsk-your-api-key-here',
    'baseUrl' => 'https://api.groq.com/openai/v1',
    'headers' => [
        'X-Request-ID' => 'req-abc123',
        'X-Org-ID'     => 'org-xyz',
    ],
]);
Programmatic values take precedence over environment variables. If you pass apiKey directly, GROQ_API_KEY is ignored for that instance.

Configuration Parameters

apiKey
string
required
Your Groq API key. All keys issued by Groq begin with the prefix gsk-. When omitted from the array, the provider reads the GROQ_API_KEY environment variable. An exception is thrown at construction time if neither source provides a value.
baseUrl
string
default:"https://api.groq.com/openai/v1"
The base URL for all API requests. The provider strips any trailing slash before use. Override this when routing traffic through a proxy, a gateway, or a local mock server. Falls back to the GROQ_BASE_URL environment variable if the key is absent from the config array.
headers
array<string, string>
default:"[]"
An associative array of additional HTTP headers merged into every request alongside the Authorization header. Use this for tracing identifiers, organisation IDs, or any other per-request metadata your infrastructure requires. Keys and values must both be strings.
sdk
Sdk|null
default:"null"
An optional AiSdk\Support\Sdk instance. Inject a custom HTTP client here to intercept or mock outbound requests in tests. When null, the provider constructs its own default HTTP client.

Setting a Global Default

Groq::create() stores the resulting provider in a static $default property and returns it. Every subsequent call to Groq::model() resolves against that default — so you only need to call Groq::create() once, typically in your application bootstrap.
Bootstrap (e.g. AppServiceProvider)
use AiSdk\Groq;

// Called once at startup
Groq::create([
    'apiKey'  => $_ENV['GROQ_API_KEY'],
    'headers' => ['X-App-Version' => '2.1.0'],
]);
Anywhere in the application
use AiSdk\Generate;
use AiSdk\Groq;

// Groq::model() uses the default provider configured above
$result = Generate::text('Explain closures in PHP.')
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->run();

echo $result->text;
If you call Groq::create() again later, the static default is replaced. This is useful when you want to swap credentials mid-flight — for example, when running multi-tenant workloads with per-tenant API keys.

Resetting the Default (Useful in Tests)

The static Groq::reset() method sets $default back to null. The next call to Groq::model() or Groq::default() will then either trigger auto-creation from environment variables or throw if none are set.
PHPUnit tearDown
use AiSdk\Groq;

protected function tearDown(): void
{
    // Prevent provider state from leaking between test cases
    Groq::reset();
}

Full Configuration Example

The example below shows all four options used together. The sdk parameter receives a mock HTTP client so no real network calls are made during tests.
Full example with all options
use AiSdk\Groq;
use AiSdk\Support\Sdk;

$sdk = new Sdk(httpClient: $mockHttpClient);

$provider = Groq::create([
    'apiKey'  => 'gsk-test-key',
    'baseUrl' => 'http://localhost:8080/openai/v1',
    'headers' => [
        'X-Request-ID' => 'test-001',
    ],
    'sdk'     => $sdk,
]);

Build docs developers (and LLMs) love