Start a project
OpenAI 3 min read

Installing and Using the OpenAI SDK with Python and Laravel

Artificial Intelligence is now part of everyday apps — from chatbots and code helpers to image generators and transcription tools. OpenAI makes this possible through its APIs and models like GPT-4o, GPT-4o mini, DALL·E,…

openai_sdk_python_laravel Installing and Using the OpenAI SDK with Python and Laravel
// on this page
  1. 1. What is the OpenAI SDK?
  2. 2. Prerequisites
  3. 3. Installing the SDK
  4. Python
  5. Laravel
  6. 4. Using OpenAI with Python (Official SDK)
  7. 4.1 Setup
  8. 4.2 Chat with GPT
  9. 4.3 Generate Image (DALL·E)
  10. 4.4 Transcribe Audio (Whisper)
  11. 4.5 Embeddings (for search, recommendations)
  12. 5. Using OpenAI with Laravel
  13. Option A: Plain HTTP (built-in, simple)
  14. Option B: Community Package (openai-php/laravel)
  15. 6. Best Practices
  16. 7. What You Can Build
  17. 8. Conclusion

Artificial Intelligence is now part of everyday apps — from chatbots and code helpers to image generators and transcription tools. OpenAI makes this possible through its APIs and models like GPT-4o, GPT-4o mini, DALL·E, Whisper, and more.

To make integration easier:

  • Python developers get an official OpenAI SDK.
  • Laravel/PHP developers can use plain HTTP or the popular community package openai-php/laravel.

This blog will walk you step by step for both.


011. What is the OpenAI SDK?

An SDK (Software Development Kit) is a ready-made library that helps you interact with APIs without writing raw HTTP requests.

The official OpenAI SDK:

  • Simplifies API calls
  • Handles authentication
  • Provides structured responses
  • Supports all major features (chat, text, images, audio, embeddings, tools)

For Laravel, there is no official SDK yet, but HTTP and community libraries make integration easy.


022. Prerequisites

Before you start:


033. Installing the SDK

Python

pip install --upgrade openai

Laravel

Option A: Use Laravel’s built-in HTTP client (no extra install).
Option B: Install community package:

composer require openai-php/laravel
php artisan openai:install

044. Using OpenAI with Python (Official SDK)

4.1 Setup

Store your API key securely.

Mac/Linux:

export OPENAI_API_KEY="sk-yourkey"

Windows (PowerShell):

setx OPENAI_API_KEY "sk-yourkey"

Then in Python:

from openai import OpenAI
client = OpenAI()  # reads API key from environment

4.2 Chat with GPT

resp = client.responses.create(
    model="gpt-4o-mini",
    input="Write a 2-line joke about AI in Hinglish"
)
print(resp.output_text)

4.3 Generate Image (DALL·E)

img = client.images.generate(
    model="gpt-image-1",
    prompt="Cartoon of a robot cooking chai"
)
print(img.data[0].url)

4.4 Transcribe Audio (Whisper)

with open("meeting.mp3", "rb") as f:
    tx = client.audio.transcriptions.create(
        model="gpt-4o-mini-transcribe",
        file=f
    )
print(tx.text)

4.5 Embeddings (for search, recommendations)

emb = client.embeddings.create(
    model="text-embedding-3-small",
    input="Artificial Intelligence for healthcare"
)
print(len(emb.data[0].embedding))

// skip the reading, get the result
I'll build this for your business and handle what's technical.One call, a written scope, and the work delivered in weekly milestones.
Start a project →

055. Using OpenAI with Laravel

Laravel does not have an official SDK, but you can use:


Option A: Plain HTTP (built-in, simple)

Controller Example

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class AiController extends Controller
{
    public function reply(Request $request)
    {
        $text = $request->input('q', 'Say hi in Hinglish');

        $resp = Http::withToken(config('services.openai.key'))
            ->post('https://api.openai.com/v1/responses', [
                'model' => 'gpt-4o-mini',
                'input' => $text,
            ])
            ->throw()
            ->json();

        $output = data_get($resp, 'output_text') ?? data_get($resp, 'output.0.content.0.text');

        return response()->json(['answer' => $output]);
    }
}

Config

// config/services.php
'openai' => [
    'key' => env('OPENAI_API_KEY'),
],

Route

Route::post('/ai/reply', [AiController::class, 'reply']);

Option B: Community Package (openai-php/laravel)

Install

composer require openai-php/laravel
php artisan openai:install

Usage

use OpenAI\Laravel\Facades\OpenAI;

class ChatController extends Controller
{
    public function chat(Request $req)
    {
        $q = $req->input('q', 'Summarise Laravel in 1 line');
        $r = OpenAI::responses()->create([
            'model' => 'gpt-4o-mini',
            'input' => $q,
        ]);
        return response()->json(['answer' => $r->outputText]);
    }
}

Image Example

$img = OpenAI::images()->generate([
    'model' => 'gpt-image-1',
    'prompt' => 'Logo of M in gradient style',
]);

$url = $img->data[0]->url;

066. Best Practices

  • Never hardcode API keys → keep in .env
  • Add timeouts & retries for production
  • Monitor usage in the OpenAI dashboard
  • Use gpt-4o-mini for cheaper, faster tasks
  • Use fakes/mocks for testing in Laravel

077. What You Can Build

  • Chatbots (support, personal assistant)
  • Blog and content generators
  • Logo or image generator apps
  • Transcription tools for meetings/podcasts
  • Semantic search with embeddings

088. Conclusion

  • If you are a Python developer, use the official OpenAI SDK for a smooth experience.
  • If you are a Laravel/PHP developer, start with plain HTTP or use the trusted community package openai-php/laravel.

Both ways make it simple to bring AI into your apps — whether it’s text, chat, images, or audio.

👉 Want to go beyond installation? Read our Ultimate Guide to OpenAI API for a complete breakdown of models, endpoints, pricing, and best practices.

// comments

Leave a comment

// your email is never published

Reading about it is good.
Having it built is better.

Booking platforms, business systems, online stores and AI assistants — scoped on one call, built in weekly milestones.

// you pay as each milestone is delivered
share