- How do I integrate Anthropic API into a Laravel app with minimal setup?
- Use the `mozex/anthropic-laravel` package for a seamless Laravel integration. Install it via Composer, publish the config file, and use the `Anthropic` facade to send messages or stream responses with just a few lines of code. The package handles service container binding and configuration automatically.
- Does this package support Laravel’s HTTP client (Guzzle) out of the box?
- Yes, the package is PSR-18 compliant, so it works natively with Laravel’s built-in HTTP client (Guzzle) or any other PSR-18-compliant client. The Laravel wrapper further simplifies this by integrating with Laravel’s service container, allowing you to use the `HttpClient` facade directly.
- Can I use tool use features like function calling with this SDK?
- Absolutely. The SDK supports Anthropic’s tool use features, including function calling and server tools. You’ll need to implement callbacks for tool responses, which can be handled asynchronously using Laravel’s queues or middleware to avoid blocking requests. The SDK provides structured responses to simplify this process.
- What Laravel versions and PHP requirements does this package support?
- The package requires PHP 8.2+, which aligns with Laravel 9+ (PHP 8.1+) or Laravel 10+. If you’re using an older Laravel version, you may need to upgrade to meet the PHP requirements. The SDK is designed for modern Laravel applications, ensuring compatibility with its latest features.
- How do I handle streaming responses in Laravel, like real-time chat updates?
- The SDK’s streaming API integrates well with Laravel’s event system or queues. You can process streamed chunks in real-time or buffer them for later use (e.g., storing in Redis). For example, you can dispatch events for each chunk or use Laravel’s queue system to handle long-running streams asynchronously.
- Is there a way to mock the Anthropic API for testing in Laravel?
- Yes, the package includes a `ClientFake` for unit testing, allowing you to mock API responses without hitting the real endpoint. Alternatively, you can use Laravel’s HTTP mocking tools like Pest’s `mock()` to simulate API calls. This makes it easy to test your application logic independently of the external API.
- How should I manage API keys securely in a Laravel application?
- Store your Anthropic API key in Laravel’s `.env` file or use Laravel Vault for enhanced security. The SDK reads the key from the `ANTHROPIC_API_KEY` environment variable by default, but you can also configure it via the Laravel wrapper’s config file or service container bindings.
- Does the package handle rate limiting, or do I need to implement it manually?
- The SDK includes rate limit metadata in responses, but you’ll need to implement application-level caching or throttling if required. Laravel’s Cache facade or queue-based throttling can help manage rate limits effectively, especially for high-volume usage. The SDK itself doesn’t enforce rate limits but provides the data to handle them.
- Can I use this SDK with other HTTP clients besides Guzzle, like Symfony’s HTTP Client?
- Yes, the package is PSR-18 compliant, so it works with any HTTP client that adheres to the PSR-18 standard, including Symfony’s HTTP Client. This flexibility allows you to choose the client that best fits your Laravel application’s architecture or existing dependencies.
- Are there any alternatives to this package for Laravel, or is this the best choice?
- While there are other PHP SDKs for the Anthropic API, `mozex/anthropic-php` stands out for its Laravel-specific wrapper, PSR-18 compliance, and comprehensive feature support, including beta features like the Files API. The Laravel wrapper reduces boilerplate and integrates seamlessly with Laravel’s ecosystem, making it a top choice for Laravel developers.