1. Go to this page and download the library: Download aisdk/openrouter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aisdk / openrouter example snippets
use AiSdk\Generate;
use AiSdk\OpenRouter;
$result = Generate::text()
->model(OpenRouter::model('openai/gpt-4o'))
->instructions('Write short, clear answers.')
->prompt('Explain closures in PHP.')
->run();
echo $result->text;
use AiSdk\Generate;
use AiSdk\OpenRouter;
$result = Generate::embedding(['Search query', 'Document text'])
->model(OpenRouter::model('openai/text-embedding-3-small'))
->dimensions(256)
->run();
$queryVector = $result->embeddings[0]->vector;
$documentVector = $result->embeddings[1]->vector;
use AiSdk\Generate;
use AiSdk\OpenRouter;
$result = Generate::image()
->model(OpenRouter::model('openai/gpt-image-1'))
->prompt('A clean app icon for a PHP AI SDK')
->size('1024x1024')
->run();
$result->output->save(__DIR__.'/icon.png');
use AiSdk\Generate;
use AiSdk\OpenRouter;
$result = Generate::speech()
->model(OpenRouter::model('microsoft/mai-voice-2'))
->input('Welcome to the release.')
->voice('en-US-Harper:MAI-Voice-2')
->format('mp3')
->run();
$result->output->save(__DIR__.'/speech.mp3');
use AiSdk\Content;
use AiSdk\Generate;
use AiSdk\OpenRouter;
$result = Generate::transcription(Content::audio(__DIR__.'/meeting.mp3'))
->model(OpenRouter::model('openai/whisper-large-v3'))
->run();
echo $result->output->text;
$result = Generate::video('A time-lapse of a flower blooming')
->model(OpenRouter::model('google/veo-3.1'))
->resolution('1080p')
->aspectRatio('16:9')
->run(timeout: 600);