| Install | |
|---|---|
composer require mozex/anthropic-laravel |
Anthropic Laravel is a community-maintained PHP API client that allows you to interact with the Anthropic API.
Note: This repository contains the integration code of the Anthropic PHP for Laravel. If you want to use the Anthropic PHP client in a framework-agnostic way, take a look at the mozex/anthropic-php repository.
With the official Anthropic SDK and Laravel's own AI SDK available, you might wonder which to use. Here's how they compare:
| Anthropic Laravel | Laravel AI SDK | Official Anthropic SDK | |
|---|---|---|---|
| Anthropic API coverage | Full — messages, streaming, tool use, vision, batches, models, extended thinking, token counting | Unified API across providers — covers core features | Full |
| Multi-provider support | Anthropic only | OpenAI, Anthropic, Gemini, Groq, xAI | Anthropic only |
| Laravel integration | Facade, config publishing, service provider, testing fakes | Native — agents, queuing, conversation memory | None — framework-agnostic |
| Laravel version support | 11+ | 12+ | Any (no Laravel dependency) |
| PHP version | 8.2+ | 8.3+ | 8.1+ |
| New Anthropic features | Same-day support | Follows unified release cycle | Same-day support |
Both packages can coexist — use Laravel AI SDK for multi-provider features and Anthropic Laravel for deep Anthropic integration.
I maintain this package along with several other open-source PHP packages used by thousands of developers every day.
If my packages save you time or help your business, consider sponsoring my work on GitHub Sponsors. Your support lets me keep these packages updated, respond to issues quickly, and ship new features.
Business sponsors get logo placement in package READMEs. See sponsorship tiers →
Requires PHP 8.2+
First, install Anthropic via the Composer package manager:
composer require mozex/anthropic-laravel
Next, execute the install command:
php artisan anthropic:install
This will create a config/anthropic.php configuration file in your project, which you can modify to your needs
using environment variables.
Blank environment variable for the Anthropic API key is already appended to your .env file.
ANTHROPIC_API_KEY=sk-...
Finally, you may use the Anthropic facade to access the Anthropic API:
use Anthropic\Laravel\Facades\Anthropic;
$result = Anthropic::messages()->create([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
echo $result->content[0]->text; // Hello! How can I assist you today?
Configuration is done via environment variables or directly in the configuration file (config/anthropic.php).
Specify your Anthropic API Key. This will be used to authenticate with the Anthropic API - you can find your API key on your Anthropic dashboard, at https://console.anthropic.com/settings/keys.
ANTHROPIC_API_KEY=
The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds.
ANTHROPIC_REQUEST_TIMEOUT=
For detailed usage examples, take a look at the mozex/anthropic-php repository.
The following resources are available through the Anthropic facade:
use Anthropic\Laravel\Facades\Anthropic;
// Messages (primary API)
Anthropic::messages()->create([...]);
Anthropic::messages()->createStreamed([...]);
Anthropic::messages()->countTokens([...]);
// Models
Anthropic::models()->list();
Anthropic::models()->retrieve('claude-sonnet-4-6');
// Message Batches
Anthropic::batches()->create([...]);
Anthropic::batches()->retrieve('msgbatch_...');
Anthropic::batches()->list();
Anthropic::batches()->cancel('msgbatch_...');
Anthropic::batches()->delete('msgbatch_...');
Anthropic::batches()->results('msgbatch_...');
// Legacy Completions
Anthropic::completions()->create([...]);
The Anthropic facade comes with a fake() method that allows you to fake the API responses.
The fake responses are returned in the order they are provided to the fake() method.
All responses have a fake() method that allows you to easily create a response object by only providing the parameters relevant for your test case.
use Anthropic\Laravel\Facades\Anthropic;
use Anthropic\Resources\Messages;
use Anthropic\Responses\Messages\CreateResponse;
Anthropic::fake([
CreateResponse::fake([
'id' => 'msg_test',
]),
]);
$result = Anthropic::messages()->create([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
expect($result)->id->toBe('msg_test');
After the requests have been sent there are various methods to ensure that the expected requests were sent:
// assert a messages create request was sent
Anthropic::assertSent(Messages::class, function (string $method, array $parameters): bool {
return $method === 'create' &&
$parameters['model'] === 'claude-sonnet-4-6';
});
You can also assert on specific resources:
Anthropic::messages()->assertSent(function (string $method, array $parameters): bool {
return $method === 'create';
});
Other available assertion methods:
// assert that nothing was sent
Anthropic::assertNothingSent();
// assert that a specific resource was not called
Anthropic::assertNotSent(Messages::class);
For more testing examples, take a look at the mozex/anthropic-php repository.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.