Laravel Daily's

How to Integrate Anthropic with Laravel to Build a REST API with PHP in 5 Minutes

hero image

Building fast is the only way to stay competitive. In the 2026 landscape, every modern application needs an AI brain. Laravel, the premier php web framework, makes connecting that brain to your backend effortless.

Anthropic's Claude provides the intelligence. Laravel provides the structure. Together, they allow you to build a production-ready REST API with PHP in less time than it takes to brew a coffee.

This guide focuses on the "how" without the fluff. We will use the latest Laravel 13 features to ship an AI-powered endpoint.

The Modern PHP Stack: Clean and Fast

Laravel has evolved into more than just a framework. It is a full-stack ecosystem. When you build a REST API with PHP, you aren't just writing code; you are leveraging best-in-class php developer tools like Forge, Cloud, and the Laravel AI SDK.

Our goal is simple: Create a POST endpoint that accepts a message and returns a response from Claude.

Prerequisites

Step 1: Initialize Your Project

Start by creating a new Laravel project if you haven't already.

laravel new ai-api --api
cd ai-api

Laravel 13's --api flag pre-configures everything for a stateless REST API. This includes the necessary routing files and middleware.

An isometric 3D visualization of the Laravel ecosystem connecting to a brain-like cloud icon representing AI.

Step 2: Connect Anthropic

We will use the recommended integration for Laravel. The mozex/anthropic-laravel package is the standard for 2026. It mirrors the official Python SDK's simplicity while staying true to Laravel's elegant syntax.

Install it via Composer:

composer require mozex/anthropic-laravel

Publish the configuration file to your project:

php artisan anthropic:install

This command adds the necessary variables to your .env file. Open your .env and paste your key:

ANTHROPIC_API_KEY=sk-ant-your-key-here

Step 3: Crafting the Service Logic

Clean architecture matters. Don't dump your API calls directly into the controller. We'll create a dedicated service to handle the conversation logic with Claude.

Create app/Services/ClaudeService.php:

<?php

namespace App\Services;

use Anthropic\Laravel\Facades\Anthropic;

class ClaudeService
{
    public function ask(string $prompt): string
    {
        $response = Anthropic::messages()->create([
            'model' => 'claude-3-5-sonnet-20240620',
            'max_tokens' => 1024,
            'messages' => [
                ['role' => 'user', 'content' => $prompt],
            ],
        ]);

        return $response['content'][0]['text'] ?? 'No response';
    }
}

This class encapsulates the communication. It uses the Anthropic facade for a clean, readable developer experience.

Step 4: The REST API Controller

Now, we need an entry point for our users. We'll use a single-action controller for simplicity.

php artisan make:controller Api/ChatController --invokable

Open app/Http/Controllers/Api/ChatController.php and implement the logic:

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\ClaudeService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class ChatController extends Controller
{
    public function __invoke(Request $request, ClaudeService $claude): JsonResponse
    {
        $data = $request->validate([
            'prompt' => 'required|string|max:5000',
        ]);

        $reply = $claude->ask($data['prompt']);

        return response()->json([
            'status' => 'success',
            'data' => [
                'reply' => $reply,
            ],
        ]);
    }
}

Laravel automatically injects the ClaudeService via the service container. This is why Laravel is the top choice for developers who value productivity.

A computer screen showing a clean JSON response from a REST API with vibrant PHP and Laravel logos.

Step 5: Defining the Route

The final piece of the puzzle is the route. Open routes/api.php.

use App\Http\Controllers\Api\ChatController;
use Illuminate\Support\Facades\Route;

Route::post('/v1/chat', ChatController::class);

Your API is now live.

Testing Your New API

You can test this immediately using any HTTP client. Here is a simple curl command:

curl -X POST http://localhost/api/v1/chat \
     -H "Content-Type: application/json" \
     -d '{"prompt": "Explain Laravel in one sentence."}'

You should receive a clean JSON response containing Claude's reply.

Going Further: Laravel AI SDK and Boost

While this 5-minute setup gets you running, the Laravel ecosystem offers more. The upcoming Laravel AI SDK provides unified interfaces for multiple models. You can swap Anthropic for OpenAI or a local Llama model with a single config change.

If performance is your bottleneck, Laravel Octane keeps your application in memory, reducing boot times and allowing you to handle thousands of concurrent AI requests.

A developer sitting at a desk with stylized app windows showing

The AI-First Developer

Integrating AI into your workflow is no longer optional. Laravel makes the technical hurdles disappear, allowing you to focus on the product experience.

Whether you are building a simple chatbot or a complex recommendation engine, these patterns stay the same. Validated inputs, clean services, and JSON responses.

We'd love to hear what you build next. Join the Laravel community and start shipping.

Previous
Does Classic PHP Authentication Really Matter in 2026? (Meet Passkeys)
Next
Why the New Laravel AI SDK Will Change the Way You Build Apps in 2026