By the end of this page you will have generated a text completion usingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openrouter/llms.txt
Use this file to discover all available pages before exploring further.
openai/gpt-4o and a raster image using openai/gpt-image-1 — both routed through OpenRouter from your own PHP script. The only prerequisite is a Composer-managed PHP 8.3+ project and an OpenRouter API key.
Install the package
Add
aisdk/openrouter to your project with Composer. This also installs aisdk/core and aisdk/openai-compatible automatically.Set your API key
Export your OpenRouter API key into the shell environment. The SDK picks it up from You can get a key from openrouter.ai/keys. Keys begin with the
OPENROUTER_API_KEY automatically — no additional configuration required.or- prefix.Generate your first text response
Create a file called Run the script:You should see a concise explanation of PHP closures printed to your terminal. The
generate_text.php and paste the following code. It builds a text-generation request against openai/gpt-4o, attaches a system instruction and a user prompt, then prints the model’s reply.$result->text property contains the raw string returned by the model.Generate your first image
Create a file called Run the script:The SDK fetches the image data from OpenRouter and the
generate_image.php. This example calls the image-generation endpoint using openai/gpt-image-1, requests a 1024 × 1024 output, and saves the result to disk.save() helper writes it to icon.png in the same directory. You can open the file immediately to verify the output.What’s Next
Text Generation
Learn how to pass system instructions, control temperature and token limits, and parse structured output from text models.
Streaming
Stream tokens as they are generated for lower time-to-first-token and real-time display in CLI tools and web apps.
Image Generation
Explore image model options including size, count, aspect ratio, and provider-specific parameters for models like
recraft/recraft-v4.1-vector.Authentication
Configure API keys, custom base URLs, and per-request HTTP headers for proxies, analytics, and multi-tenant setups.