Skip to content

Instantly share code, notes, and snippets.

@jermainee
Created November 24, 2025 23:48
Show Gist options
  • Select an option

  • Save jermainee/e53c162dfa7cfdf36aa182ff7dbdeafc to your computer and use it in GitHub Desktop.

Select an option

Save jermainee/e53c162dfa7cfdf36aa182ff7dbdeafc to your computer and use it in GitHub Desktop.
Simple Laravel Claude Client
<?php
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
class AnthropicClient
{
private string $apiKey;
private string $baseUrl;
private string $version;
private string $model;
public function __construct()
{
$this->apiKey = config('services.anthropic.key');
$this->baseUrl = config('services.anthropic.url');
$this->version = config('services.anthropic.version');
$this->model = config('services.anthropic.model');
}
/**
* @throws ConnectionException
*/
public function message(string $prompt, int $maxTokens = 1000): ?string
{
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'x-api-key' => $this->apiKey,
'anthropic-version' => $this->version,
])->post($this->baseUrl, [
'model' => $this->model,
'max_tokens' => $maxTokens,
'messages' => [
'role' => 'user',
'content' => $prompt,
]
]);
if ($response->failed()) {
new \RuntimeException(
'Anthropic API error: ' . $response->body()
);
}
$response = $response->json();
return $response['content'][0]['text'] ?? null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment