level7up/openrouter-client

OpenRouter API client for Laravel

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/level7up/openrouter-client

2.0 2025-05-15 13:32 UTC

This package is auto-updated.

Last update: 2025-12-15 14:46:14 UTC


README

A simple Laravel wrapper for the OpenRouter API to easily perform chat completions using models like openai/gpt-4o.

๐Ÿงฐ Features

  • Easy integration with Laravel via Service Provider
  • Customizable via .env or config/openrouter.php
  • Uses Guzzle for HTTP requests
  • Supports max_tokens for response control

๐Ÿš€ Installation

Require the package via Composer:

composer require level7up/openrouter-client

๐Ÿ›  Configuration

Publish the configuration file:

php artisan vendor:publish --tag=config

Add your credentials to .env:

OPENROUTER_API_KEY=your_openrouter_api_key
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
OPENROUTER_REFERER=https://your-site.com        # optional
OPENROUTER_SITE_TITLE=Your Site Name            # optional

๐Ÿงช Usage

Inject the client into your controller or service:

use Level7up\OpenRouter\OpenRouterClient;

class ChatController extends Controller
{
    public function ask(OpenRouterClient $client)
    {
        $response = $client->getCompletion('Tell me a joke.', 50);
        return response()->json(['reply' => $response]);
    }
}

โš™๏ธ Configuration File

After publishing, you can modify config/openrouter.php:

return [
    'api_key'    => env('OPENROUTER_API_KEY'),
    'base_url'   => env('OPENROUTER_BASE_URL', 'https://openrouter.ai/api/v1'),
    'referer'    => env('OPENROUTER_REFERER'),
    'site_title' => env('OPENROUTER_SITE_TITLE'),
];

๐Ÿ“š API Reference

getCompletion(string $prompt, int $maxTokens = 100): ?string

Sends a prompt to the OpenRouter API and returns the response string.

โœ… Requirements

  • PHP 8.0+
  • Laravel 8+
  • Guzzle 7+

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.

๐Ÿง  About

Maintained by Level7up. Contributions and issues are welcome!