Loading...
    • Developer Guide
    • API Reference
    • MCP
    • Resources
    • Release Notes
    Search...
    ⌘K
    Using the API
    API overviewBeta headersErrors
    OverviewPython SDKTypeScript SDKJava SDKGo SDKRuby SDKC# SDKPHP SDK
    Messages
    Create a Message
    Count tokens in a Message
    Models
    List Models
    Get a Model
    Beta
    Admin
    Completions
    Create a Text Completion
    Support & configuration
    Rate limitsService tiersVersionsIP addressesSupported regionsOpenAI SDK compatibility
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    Client SDKs

    PHP SDK

    Install and configure the Anthropic PHP SDK with value objects and builder patterns

    Was this page helpful?

    • Installation
    • Requirements
    • Usage
    • Value objects
    • Streaming
    • Error handling
    • Retries
    • Pagination
    • Advanced usage
    • Undocumented properties
    • Undocumented request params
    • Undocumented endpoints
    • Semantic versioning
    • Additional resources

    The Anthropic PHP library provides convenient access to the Anthropic REST API from any PHP 8.1.0+ application.

    The PHP SDK is currently in beta. APIs may change between versions.

    For API feature documentation with code examples, see the API reference. This page covers PHP-specific SDK features and configuration.

    Installation

    composer require "anthropic-ai/sdk"

    Requirements

    PHP 8.1.0 or higher.

    Usage

    This library uses named parameters to specify optional arguments. Parameters with a default value must be set by name.

    <?php
    
    use Anthropic\Client;
    
    $client = new Client(
      apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
    );
    
    $message = $client->messages->create(
      maxTokens: 1024,
      messages: [['role' => 'user', 'content' => 'Hello, Claude']],
      model: 'claude-opus-4-6',
    );
    
    var_dump($message->content);

    Value objects

    It is recommended to use the static with constructor Base64ImageSource::with(data: "U3RhaW5sZXNzIHJvY2tz", ...) and named parameters to initialize value objects.

    However, builders are also provided (new Base64ImageSource)->withData("U3RhaW5sZXNzIHJvY2tz").

    Streaming

    The SDK provides support for streaming responses using Server-Sent Events (SSE).

    <?php
    
    use Anthropic\Client;
    
    $client = new Client(
      apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
    );
    
    $stream = $client->messages->createStream(
      maxTokens: 1024,
      messages: [['role' => 'user', 'content' => 'Hello, Claude']],
      model: 'claude-opus-4-6',
    );
    
    foreach ($stream as $message) {
      var_dump($message);
    }

    Error handling

    When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of Anthropic\Core\Exceptions\APIException is thrown:

    <?php
    // ...
    use Anthropic\Core\Exceptions\APIConnectionException;
    use Anthropic\Core\Exceptions\APIStatusException;
    use Anthropic\Core\Exceptions\RateLimitException;
    
    // ...
    try {
      $message = $client->messages->create(
        maxTokens: 1024,
        messages: [['role' => 'user', 'content' => 'Hello, Claude']],
        model: 'claude-opus-4-6',
      );
    } catch (APIConnectionException $e) {
      echo "The server could not be reached", PHP_EOL;
      var_dump($e->getPrevious());
    } catch (RateLimitException $_) {
      echo "A 429 status code was received; we should back off a bit.", PHP_EOL;
    } catch (APIStatusException $e) {
      echo "Another non-200-range status code was received", PHP_EOL;
      echo $e->getMessage();
    }

    Error codes are as follows:

    CauseError Type
    HTTP 400BadRequestException
    HTTP 401AuthenticationException
    HTTP 403PermissionDeniedException
    HTTP 404NotFoundException
    HTTP 409ConflictException
    HTTP 422UnprocessableEntityException
    HTTP 429RateLimitException
    HTTP >= 500InternalServerException

    Retries

    Certain errors are automatically retried 2 times by default, with a short exponential backoff.

    Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts are all retried by default.

    You can use the maxRetries option to configure or disable this:

    <?php
    
    use Anthropic\Client;
    use Anthropic\RequestOptions;
    
    // Configure the default for all requests:
    $client = new Client(requestOptions: RequestOptions::with(maxRetries: 0));
    
    // Or, configure per-request:
    $result = $client->messages->create(
      maxTokens: 1024,
      messages: [['role' => 'user', 'content' => 'Hello, Claude']],
      model: 'claude-opus-4-6',
      requestOptions: RequestOptions::with(maxRetries: 5),
    );

    Pagination

    List methods in the Claude API are paginated.

    This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:

    <?php
    
    use Anthropic\Client;
    
    $client = new Client(
      apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
    );
    
    $page = $client->beta->messages->batches->list(limit: 20);
    
    var_dump($page);
    
    // fetch items from the current page
    foreach ($page->getItems() as $item) {
      var_dump($item->id);
    }
    // make additional network requests to fetch items from all pages, including and after the current page
    foreach ($page->pagingEachItem() as $item) {
      var_dump($item->id);
    }

    Advanced usage

    Undocumented properties

    You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:

    The extra* parameters of the same name override the documented parameters.

    <?php
    // ...
    use Anthropic\RequestOptions;
    
    // ...
    $message = $client->messages->create(
      maxTokens: 1024,
      messages: [['role' => 'user', 'content' => 'Hello, Claude']],
      model: 'claude-opus-4-6',
      requestOptions: RequestOptions::with(
        extraQueryParams: ['my_query_parameter' => 'value'],
        extraBodyParams: ['my_body_parameter' => 'value'],
        extraHeaders: ['my-header' => 'value'],
      ),
    );

    Undocumented request params

    If you want to explicitly send an extra param, you can do so with the extraQueryParams, extraBodyParams, and extraHeaders options under RequestOptions::with() when making a request, as seen in the example above.

    Undocumented endpoints

    To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using client->request, like so:

    <?php
    // ...
    $response = $client->request(
      method: "post",
      path: '/undocumented/endpoint',
      query: ['dog' => 'woof'],
      headers: ['useful-header' => 'interesting-value'],
      body: ['hello' => 'world']
    );

    Semantic versioning

    This package follows SemVer conventions. As the library is in initial development and has a major version of 0, APIs may change at any time.

    This package considers improvements to the (non-runtime) PHPDoc type definitions to be non-breaking changes.

    Additional resources

    • GitHub repository
    • Packagist
    • API reference
    • Streaming guide
    Other HTTP errorAPIStatusException
    TimeoutAPITimeoutException
    Network errorAPIConnectionException