PHPackages                             ermirshehaj/openai-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. [API Development](/categories/api)
4. /
5. ermirshehaj/openai-php

ActiveLibrary[API Development](/categories/api)

ermirshehaj/openai-php
======================

OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API

128PHP

Since Apr 22Pushed 1y agoCompare

[ Source](https://github.com/ermiri/openai-php)[ Packagist](https://packagist.org/packages/ermirshehaj/openai-php)[ RSS](/packages/ermirshehaj-openai-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

 [![GitHub Workflow Status (master)](https://camo.githubusercontent.com/9b5cd89503ff72dcb851fc486957d3c4bd976569a67b9bb1ce306599c1cb6fdd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6f70656e61692d7068702f6c61726176656c2f74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d726f756e642d737175617265)](https://github.com/openai-php/laravel/actions) [![Total Downloads](https://camo.githubusercontent.com/6a1c4ae8216497114ded6bd858a32b6aefaf8325523573ca2fc4460f165bd4f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f70656e61692d7068702f6c61726176656c)](https://packagist.org/packages/openai-php/laravel) [![Latest Version](https://camo.githubusercontent.com/e2809c4e8588860cffc1dc375ae9a90d8a3eb725dee58afaeb42ffd11ff9bc7b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f70656e61692d7068702f6c61726176656c)](https://packagist.org/packages/openai-php/laravel) [![License](https://camo.githubusercontent.com/a23c4458f94b4da93c4cc295173f74f0fea13a50262954fb99f8d91658ccae82/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f70656e61692d7068702f6c61726176656c)](https://packagist.org/packages/openai-php/laravel)

---

**OpenAI PHP** for Laravel is a community-maintained PHP API client that allows you to interact with the [Open AI API](https://platform.openai.com/docs/api-reference/introduction). If you or your business relies on this package, it's important to support the developers who have contributed their time and effort to create and maintain this valuable tool:

- Nuno Maduro: **[github.com/sponsors/nunomaduro](https://github.com/sponsors/nunomaduro)**
- Sandro Gehri: **[github.com/sponsors/gehrisandro](https://github.com/sponsors/gehrisandro)**

> **Note:** This repository contains the integration code of the **OpenAI PHP** for Laravel. If you want to use the **OpenAI PHP** client in a framework-agnostic way, take a look at the [openai-php/client](https://github.com/openai-php/client) repository.

Get Started
-----------

[](#get-started)

> **Requires [PHP 8.1+](https://php.net/releases/)**

First, install OpenAI via the [Composer](https://getcomposer.org/) package manager:

```
composer require openai-php/laravel
```

Next, execute the install command:

```
php artisan openai:install
```

This will create a `config/openai.php` configuration file in your project, which you can modify to your needs using environment variables. Blank environment variables for the OpenAI API key and organization id are already appended to your `.env` file.

```
OPENAI_API_KEY=sk-...
OPENAI_ORGANIZATION=org-...
```

Finally, you may use the `OpenAI` facade to access the OpenAI API:

```
use OpenAI\Laravel\Facades\OpenAI;

$result = OpenAI::chat()->create([
    'model' => 'gpt-4o-mini',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

echo $result->choices[0]->message->content; // Hello! How can I assist you today?

```

Configuration
-------------

[](#configuration)

Configuration is done via environment variables or directly in the configuration file (`config/openai.php`).

### OpenAI API Key and Organization

[](#openai-api-key-and-organization)

Specify your OpenAI API Key and organization. This will be used to authenticate with the OpenAI API - you can find your API key and organization on your OpenAI dashboard, at .

```
OPENAI_API_KEY=
OPENAI_ORGANIZATION=
```

### OpenAI Project

[](#openai-project)

For implementations that require a project ID, you can specify the OpenAI project ID in your environment variables.

```
OPENAI_PROJECT=proj_...
```

### OpenAI API Base URL

[](#openai-api-base-url)

The base URL for the OpenAI API. By default, this is set to `api.openai.com/v1`.

```
OPENAI_BASE_URL=
```

### Request Timeout

[](#request-timeout)

The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds.

```
OPENAI_REQUEST_TIMEOUT=
```

Usage
-----

[](#usage)

For usage examples, take a look at the [openai-php/client](https://github.com/openai-php/client) repository.

Testing
-------

[](#testing)

The `OpenAI` facade comes with a `fake()` method that allows you to fake the API responses.

The fake responses are returned in the order they are provided to the `fake()` method.

All responses are having a `fake()` method that allows you to easily create a response object by only providing the parameters relevant for your test case.

```
use OpenAI\Laravel\Facades\OpenAI;
use OpenAI\Responses\Completions\CreateResponse;

OpenAI::fake([
    CreateResponse::fake([
        'choices' => [
            [
                'text' => 'awesome!',
            ],
        ],
    ]),
]);

$completion = OpenAI::completions()->create([
    'model' => 'gpt-4o-mini',
    'prompt' => 'PHP is ',
]);

expect($completion['choices'][0]['text'])->toBe('awesome!');
```

After the requests have been sent there are various methods to ensure that the expected requests were sent:

```
// assert completion create request was sent
OpenAI::assertSent(Completions::class, function (string $method, array $parameters): bool {
    return $method === 'create' &&
        $parameters['model'] === 'gpt-4o-mini' &&
        $parameters['prompt'] === 'PHP is ';
});
```

For more testing examples, take a look at the [openai-php/client](https://github.com/openai-php/client#testing) repository.

---

OpenAI PHP for Laravel is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/98a677cf9f03b06c890523ed57bf5b31edc9d59890da0f3fa47c277a11b95ded?d=identicon)[ermiri](/maintainers/ermiri)

---

Top Contributors

[![nunomaduro](https://avatars.githubusercontent.com/u/5457236?v=4)](https://github.com/nunomaduro "nunomaduro (53 commits)")[![gehrisandro](https://avatars.githubusercontent.com/u/25097194?v=4)](https://github.com/gehrisandro "gehrisandro (37 commits)")[![iBotPeaches](https://avatars.githubusercontent.com/u/611784?v=4)](https://github.com/iBotPeaches "iBotPeaches (4 commits)")[![ermiri](https://avatars.githubusercontent.com/u/5099870?v=4)](https://github.com/ermiri "ermiri (4 commits)")[![cosmastech](https://avatars.githubusercontent.com/u/42181698?v=4)](https://github.com/cosmastech "cosmastech (3 commits)")[![peterramsing](https://avatars.githubusercontent.com/u/3523268?v=4)](https://github.com/peterramsing "peterramsing (2 commits)")[![pb30](https://avatars.githubusercontent.com/u/259602?v=4)](https://github.com/pb30 "pb30 (2 commits)")[![xenon87](https://avatars.githubusercontent.com/u/116573?v=4)](https://github.com/xenon87 "xenon87 (2 commits)")[![trippo](https://avatars.githubusercontent.com/u/497169?v=4)](https://github.com/trippo "trippo (1 commits)")[![krishnahimself](https://avatars.githubusercontent.com/u/19906424?v=4)](https://github.com/krishnahimself "krishnahimself (1 commits)")[![BadJacky](https://avatars.githubusercontent.com/u/113529280?v=4)](https://github.com/BadJacky "BadJacky (1 commits)")[![butschster](https://avatars.githubusercontent.com/u/773481?v=4)](https://github.com/butschster "butschster (1 commits)")[![jpsilvaa](https://avatars.githubusercontent.com/u/17861763?v=4)](https://github.com/jpsilvaa "jpsilvaa (1 commits)")[![askdkc](https://avatars.githubusercontent.com/u/7894265?v=4)](https://github.com/askdkc "askdkc (1 commits)")[![marlonbasten](https://avatars.githubusercontent.com/u/31259698?v=4)](https://github.com/marlonbasten "marlonbasten (1 commits)")[![megoxv](https://avatars.githubusercontent.com/u/87904671?v=4)](https://github.com/megoxv "megoxv (1 commits)")[![peterfox](https://avatars.githubusercontent.com/u/1716506?v=4)](https://github.com/peterfox "peterfox (1 commits)")

### Embed Badge

![Health badge](/badges/ermirshehaj-openai-php/health.svg)

```
[![Health](https://phpackages.com/badges/ermirshehaj-openai-php/health.svg)](https://phpackages.com/packages/ermirshehaj-openai-php)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
