This quickstart gets you from zero to a working Grok API call in under five minutes. You will install the package, configure your API key, generate a text completion withDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/xai/llms.txt
Use this file to discover all available pages before exploring further.
grok-4.3, and create an image — all using the same fluent builder API.
Install the package
Add
aisdk/xai to your project with Composer. This also installs aisdk/core and aisdk/openai-compatible automatically as required dependencies:Set your API key
Export your xAI API key as an environment variable. The provider reads
XAI_API_KEY automatically, so you will not need to hard-code the key in your source files:Generate text
Use
Generate::text() with XAI::model() to send a prompt to Grok and print the response. The ->instructions() call sets a system-level message, and ->run() executes the request synchronously:What’s Next
Now that your first Grok calls are working, explore these guides to go further:Text Generation
Learn how to use system instructions, control temperature and token limits, and work with the full text result object.
Streaming
Stream tokens back to the client as they are generated, with support for partial results and cancellation.
Image Generation
Control image count, aspect ratio, resolution, and provider-specific options when generating images with Grok.
Reasoning
Use
Reasoning::effort() to let Grok models think before answering, improving accuracy on complex or multi-step tasks.