tibor-src / grok-sdk-laravel
Unofficial Laravel integration for tibor-src/grok-sdk-php.
Requires
- php: ^8.2.0
- illuminate/support: ^12.56.0 || ^13.23.0
- tibor-src/grok-sdk-php: ^0.2.13
Requires (Dev)
- larastan/larastan: ^3.10.0
- laravel/pint: ^1.30.3
- orchestra/testbench: ^10.8.0 || ^11.3.0
- pestphp/pest: ^3.8.5 || ^4.7.5
- pestphp/pest-plugin-type-coverage: ^3.0 || ^4.0
Suggests
None
Provides
None
Conflicts
- laravel/framework: <12.56.0 || >=14.0.0
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 03:10:09 UTC
README
Unofficial Laravel package for tibor-src/grok-sdk-php.
This package succeeds tibor-src/xai-sdk-laravel. See UPGRADE.md for the composer name, namespace, service provider, facade, and config mapping.
This package is not published or maintained by xAI. It registers that PHP client's Grok class in the Laravel container. SpaceXSI and SpaceXAI facades and container aliases are not defined. Request and response behavior comes from tibor-src/grok-sdk-php.
Install
composer require tibor-src/grok-sdk-laravel
Requires PHP 8.2 or newer, Laravel 12 or 13, and tibor-src/grok-sdk-php 0.2.13 or newer. Laravel 11 is not supported. Set XAI_API_KEY or GROK_API_KEY in the environment (XAI_API_KEY is used when both are set), then publish the config if you want to override the client's constructor options:
php artisan vendor:publish --tag=grok-config
The published config/grok.php reads XAI_API_KEY, then GROK_API_KEY if XAI_API_KEY is unset or empty. Leave the other values null to keep the PHP client's defaults. When api_key is empty, the PHP client still reads those two variables itself, with the same order.
Usage
use TiborSrc\GrokSdkPhp\Grok; $client = app(Grok::class); $response = $client->responses->create([ 'model' => 'grok-4.7', 'input' => 'Explain why the sky is blue in one sentence.', ]); echo $response->toText();
The client sends store as false unless you opt in. That differs from the API wire default. With storage disabled, it requests encrypted reasoning content so $response->toInput() can preserve context between turns. A create call that omits stream is sent as a stream and returned as the finished response. Pass 'stream' => false for one JSON response, or 'stream' => true for a ResponseStream.
TiborSrc\GrokSdkLaravel\Facades\Grok resolves that same container binding. Resources are public properties on the client, so call them on the resolved instance: Grok::getFacadeRoot()->responses->create(...).
config/grok.php passes timeout and idle_timeout through to the PHP client. Null keeps the client's default. 0, a negative value, or anything longer than 24 hours becomes 24 hours. The same 24-hour cap applies to videos->wait() and batches->wait(): a timeout or interval longer than 24 hours means 24 hours. A wait() timeout of 0 or less still expires at once, and an interval of 0 or less still polls again without a pause.
retry_before_output still defaults to off. When it is true, the PHP client retries a dropped connection before the first response byte. That includes a streamed responses->create() whose connection drops after the 200 headers and before the first byte of the event stream.
Resources
The binding is the PHP client. It exposes:
responses(create,compact,get,delete,inputItems->list)models(list,get, pluslanguage,image, andvideo)images(generate,edit)videos(generate,edit,extend,get,wait)files(upload,list,get,delete,content,createPublicUrl,revokePublicUrl)batches(create,list,get,cancel,results,wait,requests)voice(speak,transcribe,list,get,custom,clientSecrets)tokenizer(encode)account(apiKey)
grok-sdk-php 0.2.13
The resolved client uses tibor-src/grok-sdk-php 0.2.13 (@grok/sdk 0.2.13, also published as @xai-official/sdk). This package requires ^0.2.13.
- README examples use the
Grokclient.SpaceXSIandSpaceXAIare not aliases in this package or intibor-src/grok-sdk-php. - A request sends
user-agent: grok-sdk/0.2.13 (php)unlessdefault_headersalready setsuser-agent. xai-sdk-versionisphp/0.2.13on every request. A value set indefault_headersis replaced with that.
use TiborSrc\GrokSdkLaravel\Facades\Grok; use TiborSrc\GrokSdkPhp\Grok as GrokClient; $client = app(GrokClient::class); echo Grok::getFacadeRoot() === $client ? 'same client' : 'different';
Releases of this Laravel integration through 0.2.13 were published as tibor-src/xai-sdk-laravel. The notes below describe PHP client behavior that is still available on the resolved Grok instance.
grok-sdk-php 0.2.12
The resolved client includes tibor-src/grok-sdk-php behavior from 0.2.12 (@xai-official/sdk 0.2.12).
TiborSrc\GrokSdkPhp\Grokis the client class. The container binds one client under that class.TiborSrc\GrokSdkLaravel\Facades\Grokresolves that same instance.config/grok.phpreadsXAI_API_KEY, thenGROK_API_KEYifXAI_API_KEYis unset or empty. Whenapi_keyis empty, the PHP client reads those two variables in the same order.XAI_API_KEYis used when both are set. A non-emptyapi_keystill overrides both. The missing-key error names both variables.- A request sends
user-agent: grok-sdk/0.2.13 (php)unlessdefault_headersalready setsuser-agent. xai-sdk-versionisphp/0.2.13on every request. A value set indefault_headersis replaced with that.
use TiborSrc\GrokSdkLaravel\Facades\Grok; use TiborSrc\GrokSdkPhp\Grok as GrokClient; $client = app(GrokClient::class); echo Grok::getFacadeRoot() === $client ? 'same client' : 'different';
grok-sdk-php 0.2.11.1
The resolved client includes the installable 0.2.11 port (@xai-official/sdk 0.2.11). Packagist locked published 0.2.11 to an earlier commit, so that PHP line used 0.2.11.1.
voice->transcribe()takesredactandshow_redacted_text.redactis a list of groups (pii,pci,phi) or single types such asssn, each sent as its ownredactfield. Withshow_redacted_textset totrue, each redacted word has the original words inredacted_text.TiborSrc\GrokSdkPhp\KNOWN_TRANSCRIPTION_REDACTIONSlists the groups this release names; any other string is still sent.TiborSrc\GrokSdkPhp\KNOWN_MODEL_IDSno longer listsgrok-build-0.1. It reached end of life on November 19, 2026.responses->create()still sends whatever model id you pass. The API no longer serves this model.- Resource methods that take an id throw a
TypeErrorfor an empty id,".", or".."instead of sending the request. responses->create()withoutstream, and$stream->done(), keep the completed response when the connection fails after the terminal event. Later events are not delivered.
use TiborSrc\GrokSdkPhp\Http\Blob; use TiborSrc\GrokSdkPhp\Grok; $redacted = app(Grok::class)->voice->transcribe([ 'file' => new Blob(file_get_contents('./call.mp3'), 'audio/mpeg'), 'language' => 'en', 'redact' => ['pii', 'pci'], 'show_redacted_text' => true, ]); echo $redacted->text;
grok-sdk-php 0.2.10
The resolved client includes 0.2.10 PHP client behavior. Config, the facade, and the service provider are unchanged from this package's 0.2.13 naming.
grok-sdk-php 0.2.9
The resolved client includes 0.2.9 PHP client behavior.
- Streamed responses keep one event's
event:anddata:lines together when the server ends lines with\r\n, including when a chunk splits that line break.\rfollowed by\r\n, and\nfollowed by\r\n, still end an event. Bare\rand\nline endings are unchanged. TiborSrc\GrokSdkPhp\KNOWN_IMAGE_MODEL_IDSlistsgrok-imagine-imageandgrok-imagine-image-2.0.grok-imagine-image-qualitywas retired on November 2, 2026.images->generate()still sends whatever model id you pass. Usegrok-imagine-image-2.0for new work.
use TiborSrc\GrokSdkPhp\Grok; $image = app(Grok::class)->images->generate([ 'model' => 'grok-imagine-image-2.0', 'prompt' => 'A red cube on a white table', ]);
grok-sdk-php 0.2.8
The resolved client forwards request bodies to tibor-src/grok-sdk-php. This package does not validate aspect ratios, guidelines, model ids, or transcription formats.
responses->create()acceptsguidelines. Each entry is['type' => 'policy', 'category' => ..., 'behavior' => ...].categoryisnsfw,violence,profanity, orcyber.behaviorisdeclinefornsfw,violence, andprofanity, andallowforcyberonly. The API rejects other pairs with400, and it rejects the oldsexual,self_harm,illicit, andoffer_supportvalues with422.cybermust be enabled for the API key. Each category may appear at most once.$response->watermarkedand$stream->watermarkedare true when the response has the headerx-watermark: true. Watermarking is on by default for supported models from November 7, 2026.incomplete_details.reasoncan becontent_filter.temperatureandtop_pare sent as given. The API samplestemperatureabove 1 at 1, andtop_pabove the model's default at that default.voice->transcribe()acceptsaudio_formatwebm,avi, andmov. ABlobwithout a name is sent asaudio.webm,audio.avi, oraudio.movwhen its MIME type or first bytes say so.videos->generate()acceptsaspect_ratio21:9and5:2, along with1:1,16:9,9:16,4:3,3:4,3:2, and2:3.voice->clientSecrets->create()accepts the realtime models inTiborSrc\GrokSdkPhp\KNOWN_REALTIME_MODEL_IDS:grok-voice-latest(which points togrok-voice-3.0),grok-voice-3.0,grok-voice-2.0, andgrok-voice-think-fast-2.0. A session that asks forgrok-voice-think-fast-2.0reportsgrok-voice-2.0.- Live search is removed. The API returns HTTP 410 for
search_parametersand rejects a tool withtypelive_search. UsewebSearch()andxSearch().web_search_optionsis the chat-completions form of that removal; this client has no chat-completions resource. - A request may include up to 1000 tools.
xSearch()allowed_x_handlesandexcluded_x_handleseach take up to 20 handles. The client does not enforce either limit.
use TiborSrc\GrokSdkPhp\Grok; use function TiborSrc\GrokSdkPhp\webSearch; use function TiborSrc\GrokSdkPhp\xSearch; $response = app(Grok::class)->responses->create([ 'model' => 'grok-4.7', 'input' => 'Hello', 'guidelines' => [ ['type' => 'policy', 'category' => 'violence', 'behavior' => 'decline'], ['type' => 'policy', 'category' => 'cyber', 'behavior' => 'allow'], ], 'tools' => [webSearch(), xSearch()], ]);
Tests
composer test
That runs Pint, PHPStan at the maximum level, Pest's type coverage at 100%, and the test suite. Tests mock the PHP client's HTTP layer and do not call the live API.
License
Apache License 2.0. See LICENSE and NOTICE.