How to Integrate Gemini AI into a Laravel Application for Smart Web Features
Artificial Intelligence is transforming modern web applications. In this quick guide, we will look at how to easily integrate "Gemini AI" into a "Laravel" application to build smart, automated features like text generati
Artificial Intelligence is transforming modern web applications. In this quick guide, we will look at how to easily integrate "Gemini AI" into a "Laravel" application to build smart, automated features like text generation, chatbots, and content assistants.
Prerequisites
- PHP 8.2+ & Composer installed
- A running Laravel project
- A Gemini API Key from Google AI Studio
Step 1: Install the Google Generative AI Package via Composer
Run the following command in your terminal to install the official or community PHP client for Gemini API:
\bash
composer require google/generative-ai-php
\\
(Note: You can also use Laravel's native HTTP client to call the Gemini REST API directly if preferred).
Step 2: Configure Your API Key
Add your Gemini API key to your .env\ file:
\env
GEMINI_API_KEY=your_actual_api_key_here
\\
Then, add a configuration entry in config/services.php\:
\php
'gemini' => [
'api_key' => env('GEMINI_API_KEY'),
],
\\
Step 3: Create a Controller to Handle AI Requests
Run the artisan command to create a controller:
\bash
php artisan make:controller AIController
\\
Inside your controller, write a method to send prompts to Gemini and return the response:
\`php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class AIController extends Controller
{
public function generateResponse(Request $request)
{
$prompt = $request->input('prompt');
$apiKey = config('services.gemini.api_key');
$response = Http::post("https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={$apiKey}", [
'contents' => [
[
'parts' => [
['text' => $prompt]
]
]
]
]);
return response()->json($response->json());
}
}
`\
Conclusion
By integrating Gemini AI into Laravel, you can elevate standard web apps into intelligent platforms.
Written by **Baitulhassan Shaikh, Founder & Full-Stack Developer at **ALFAIZ Web Agency. Specializing in custom Laravel apps, secure backend architectures, and AI-powered web solutions.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.