Laravel 11 Integrate ChatGPT API Example Tutorial




Laravel 11 Integrate ChatGPT API Example Tutorial

Hi, Dev

In this article, I will guide you through the process of integrating the Chat GPT API into a Laravel 11 application.

What is ChatGPT/OpenAI?

ChatGPT is an advanced artificial intelligence developed by OpenAI that can understand and generate text similar to a human. It can engage in conversations, answer questions, and assist with writing tasks, making it feel like you're talking to a highly knowledgeable robot. People use ChatGPT for entertainment, learning, and professional purposes. OpenAI, the company behind ChatGPT, creates cutting-edge technology to make computers more useful and helpful for everyone.

In this example, we will use the `openai-php/laravel` Composer package to interact with the OpenAI API. We will build a simple form where users can enter a title or idea. Then, we will use the ChatGPT API to generate a list of domain names based on that input. Essentially, we'll create a straightforward webpage where users can input a title, and ChatGPT will suggest relevant domain names.

Let's now proceed to the step-by-step example of integrating OpenAI API in Laravel 11.

Step for Laravel 11 Integrate ChatGPT / OpenAI API Example

Step 1: Install Laravel 11

Step 2: Install openai-php/laravel Package

Step 3: Create OpenAI Account

Step 4: Create Route

Step 5: Create Controller

Step 6: Create Blade File

Run Laravel App

Step 1: Install Laravel 11

To begin, we must obtain the latest version of the Laravel application by executing the command stated below. Open your terminal or command prompt and enter the given command.

composer create-project laravel/laravel example-app

Step 2: Install openai-php/laravel Package

In this step, we need to install openai-php/laravel composer package to use OpenAI API. so let's run bellow command:

composer require openai-php/laravel

Now, we will publish configuration file using the following command:

php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"

Step 3: Create OpenAI Account

First you need to create Account on OpenAI website.

1. Go to you can register there you will get free access $18 credit for next three months.

2. After that go to and generate the API token.

You can follow the below screenshots:



This screenshot, you need to copy API key and add on your .env file as like the below:


Then add your API KEY to .env file:



Step 4: Create Route

now we will create one route for calling our example, so let's add new route to web.php file as bellow:



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ChatGPTController;

Route::get('/chat-gpt', [ChatGPTController::class, 'index'])->name('chat-gpt.index');

Step 5: Create Controller

in this step, we will create ChatGPTController and write payment logic, so let's add new route to web.php file as bellow:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Arr;

use OpenAI\Laravel\Facades\OpenAI;

class ChatGPTController extends Controller



* Write code on Method


* @return response()


public function index(Request $request)


$result = '';

if ($request->filled('title')) {

$messages = [

['role' => 'user', 'content' => 'suggest me 5 domain names from "'.$request->title.'" topic. simply give me domain names list with 1. 2. 3. 4. 5. '],


$result = OpenAI::chat()->create([

'model' => 'gpt-3.5-turbo',

'messages' => $messages,


$result = Arr::get($result, 'choices.0.message')['content'] ?? '';


return view('chatGPT', compact('result'));



Step 6: Create Blade File

here, we need to create chatGPT.blade.php file and update following code on it.


<!DOCTYPE html>



<title>Laravel 11 Integrate ChatGPT API Example Tutorial -</title>

<link href="" rel="stylesheet" crossorigin="anonymous">



<div class="container">

<div class="card mt-5">

<h3 class="card-header p-3">Laravel 11 Integrate ChatGPT API Example Tutorial -</h3>

<div class="card-body">

<form method="GET" action="{{ route('chat-gpt.index') }}">

<div class="form-group">

<label><strong>Give me your title, I will provide you domains list.</strong></label>

<input type="text" name="title" class="form-control" />


<div class="form-group mt-2">

<button type="submit" class="btn btn-success">Submit</button>




<div class="mt-5">


{!! nl2br($result) !!}








Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:


Now, you will see the following output:


Now, it's done...

I hope it can help you...

#Laravel 11