PHPackages                             hojjatjh/openrouter-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. hojjatjh/openrouter-php

ActiveLibrary

hojjatjh/openrouter-php
=======================

A clean, dependency-free PHP client for the OpenRouter API. Chat, streaming, tool calling, structured outputs and more.

v0.1.0(yesterday)00MITPHP &gt;=8.1

Since Jul 19Compare

[ Source](https://github.com/hojjatjh/openrouter-php)[ Packagist](https://packagist.org/packages/hojjatjh/openrouter-php)[ RSS](/packages/hojjatjh-openrouter-php/feed)WikiDiscussions Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

OpenRouter PHP
==============

[](#openrouter-php)

**A modern, fully-typed PHP client for the [OpenRouter](https://openrouter.ai) API.**

One elegant interface to hundreds of LLMs — chat, streaming, tool calling, structured outputs, conversation memory, a knowledge base, automatic retries, and a fully test-friendly architecture. No framework required.

[English](#-english) • [فارسی](#-%D9%81%D8%A7%D8%B1%D8%B3%DB%8C)

[![Latest Version](https://camo.githubusercontent.com/d60eb1df23ab3f6a1a0df2fec1b61144d74040d511e717ad8d23fa5be4f9286c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686f6a6a61746a682f6f70656e726f757465722d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hojjatjh/openrouter-php)[![Total Downloads](https://camo.githubusercontent.com/d26807fa19a1be4a5a87f56349528031cd27c22986af01e418cef9791502796b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f6a6a61746a682f6f70656e726f757465722d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hojjatjh/openrouter-php)[![PHP Version](https://camo.githubusercontent.com/12eeced4923220521bc558f3193c85cf56374c752d4fa7d1ccb88f5e12714cb3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f686f6a6a61746a682f6f70656e726f757465722d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hojjatjh/openrouter-php)[![License](https://camo.githubusercontent.com/bf2497a6441af6f7d1fc26b5c1112abd4e0c2914f6c9e3523ba185fd59a78950/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f686f6a6a61746a682f6f70656e726f757465722d7068702e7376673f7374796c653d666c61742d737175617265)](LICENSE)

---

🇬🇧 English
----------

[](#-english)

OpenRouter gives you a single, OpenAI-compatible gateway to hundreds of models from many providers. **OpenRouter PHP** wraps that gateway in a clean, dependency-free, strongly-typed PHP package so you can ship AI features in minutes instead of hand-rolling cURL calls.

### 📑 Table of Contents

[](#-table-of-contents)

- [Why This Library](#-why-this-library)
- [Features](#-features)
- [Requirements](#-requirements)
- [Installation](#-installation)
- [Quick Start](#-quick-start)
- [Configuration](#-configuration)
- [Usage](#-usage)
    - [Chat](#-chat)
    - [Message Helpers](#-message-helpers)
    - [Streaming](#-streaming)
    - [Tool Calling](#-tool-calling)
    - [Structured Outputs](#-structured-outputs)
    - [Conversation Memory](#-conversation-memory)
    - [Knowledge Base](#-knowledge-base)
    - [Model Fallback and Routing](#-model-fallback-and-routing)
    - [Automatic Retries](#-automatic-retries)
    - [Helper Endpoints](#-helper-endpoints)
- [Error Handling](#-error-handling)
- [Testing](#-testing)
- [Contributing](#-contributing)
- [License](#-license)

### 💡 Why This Library

[](#-why-this-library)

- **One API, every model.** Switch between OpenAI, Anthropic, Google, Meta, Mistral and more by changing a single string.
- **Batteries included.** Streaming, tools, JSON schema, memory, retries and a knowledge base are built in — not left as an exercise.
- **Typed and predictable.** Rich response objects and a dedicated exception per HTTP status mean fewer surprises in production.
- **Testable by design.** The network layer sits behind a `Transport` interface, so your tests never touch the internet.
- **Zero dependencies.** Pure PHP plus `ext-curl` — drop it into any project or framework.

### ✨ Features

[](#-features)

- 💬 **Chat completions** — simple string prompts or full message arrays
- 🌊 **Streaming** — token-by-token responses through a callback (SSE)
- 🛠️ **Tool / function calling** — define tools and drive the tool-call loop
- 📐 **Structured outputs** — force valid JSON or a strict JSON Schema
- 🧠 **Conversation memory** — stateful chats, with optional **persistent** storage on disk
- 📚 **Knowledge base** — ground answers on your own data
- 🔁 **Automatic retries** — exponential backoff with jitter for transient failures
- 📊 **Helper endpoints** — list models, check credits, inspect generation stats and cost
- 🧯 **Rich exceptions** — a precise exception per HTTP error code
- 🧪 **Test-friendly** — a `Transport` interface lets you mock the network entirely
- 🪶 **Zero dependencies** — pure PHP + `ext-curl`, framework-agnostic

### 📦 Requirements

[](#-requirements)

- PHP **8.1** or higher
- `ext-curl`, `ext-json`

### ⬇️ Installation

[](#️-installation)

Install the package via [Composer](https://getcomposer.org):

```
composer require hojjatjh/openrouter-php
```

### 🚀 Quick Start

[](#-quick-start)

Create a client with your API key and send your first prompt:

```
