Created
November 24, 2025 23:48
-
-
Save jermainee/e53c162dfa7cfdf36aa182ff7dbdeafc to your computer and use it in GitHub Desktop.
Simple Laravel Claude Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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