How to Integrate OpenAI API with Laravel (Complete Guide 2026)

Laravel is the most popular PHP framework, and adding OpenAI to it is straightforward with the official openai-php/client library. This guide covers installation, a service class, a chat controller, streaming, error handling, and testing.

Requirements

Laravel 10 or 11, PHP 8.1+, Composer, OpenAI API key

Step 1: Install