Hello!

BrainTech - software solutions, web design and applications.

Module 2 – First OpenAI API call Lesson

First API request

First API request

First API request matters because without a stable API layer FirmAssist cannot talk to the model reliably.

You now bootstrap FirmAssist: API key, folder structure, and the first PHP call that returns a model response.

In depth

Here you build the minimal production flow: configuration, HTTP call, response parsing, and safe error handling. If this layer is clean, later chat, OCR, and RAG features in FirmAssist can reuse it without duplicating code or improvising solutions.

Key points

  • Secrets stay on the server only.
  • The payload must be readable and valid.
  • HTTP status and body are checked separately.
  • Timing and error logs help later debugging.

Practical example

First PHP call to the AI provider.

$payload = [
    'model' => 'gpt-4.1-mini',
    'input' => 'Summarize this supplier email in one sentence.',
];

$ch = curl_init('https://api.openai.com/v1/responses');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('OPENAI_API_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR),
    CURLOPT_RETURNTRANSFER => true,
]);

$response = curl_exec($ch);
curl_close($ch);
echo $response;

Common mistake

The mistake is hiding every failure behind one generic message.

Summary

After this lesson you can build a stable AI endpoint that FirmAssist uses as the base for later modules.

Design Wireframe
High Fidelity Design
Design development
Design development
Design development
Research development