{"id":30594155,"url":"https://github.com/anthropics/anthropic-sdk-php","last_synced_at":"2026-05-04T19:01:12.046Z","repository":{"id":311798972,"uuid":"1044588287","full_name":"anthropics/anthropic-sdk-php","owner":"anthropics","description":"Access to Anthropic's safety-first language model APIs in PHP","archived":false,"fork":false,"pushed_at":"2026-05-01T13:46:56.000Z","size":3435,"stargazers_count":141,"open_issues_count":10,"forks_count":39,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-05-01T15:30:03.027Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anthropics.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-25T23:19:47.000Z","updated_at":"2026-04-27T18:43:09.000Z","dependencies_parsed_at":"2025-08-26T23:38:05.718Z","dependency_job_id":"7a8bd699-85f3-436b-877b-70c5f466e443","html_url":"https://github.com/anthropics/anthropic-sdk-php","commit_stats":null,"previous_names":["anthropics/anthropic-sdk-php"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/anthropics/anthropic-sdk-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthropics%2Fanthropic-sdk-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthropics%2Fanthropic-sdk-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthropics%2Fanthropic-sdk-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthropics%2Fanthropic-sdk-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anthropics","download_url":"https://codeload.github.com/anthropics/anthropic-sdk-php/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthropics%2Fanthropic-sdk-php/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32620523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-04T10:08:07.713Z","status":"ssl_error","status_checked_at":"2026-05-04T10:08:02.005Z","response_time":58,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-29T20:00:35.756Z","updated_at":"2026-05-04T19:01:12.040Z","avatar_url":"https://github.com/anthropics.png","language":"PHP","funding_links":[],"categories":["🏢 Official Anthropic Resources","LLM Clients \u0026 Adapters"],"sub_categories":["🔧 SDKs \u0026 Development Tools"],"readme":"# Claude SDK for PHP\n\n[![Packagist Version](https://img.shields.io/packagist/v/anthropic-ai/sdk.svg)](https://packagist.org/packages/anthropic-ai/sdk)\n\nThe Claude SDK for PHP provides access to the [Claude API](https://docs.anthropic.com/en/api/) from PHP applications.\n\n## Documentation\n\nFull documentation is available at **[platform.claude.com/docs/en/api/sdks/php](https://platform.claude.com/docs/en/api/sdks/php)**.\n\n## Installation\n\n\u003c!-- x-release-please-start-version --\u003e\n\n```sh\ncomposer require \"anthropic-ai/sdk:^0.18.0\"\n```\n\n\u003c!-- x-release-please-end --\u003e\n\n## Getting started\n\n```php\n\u003c?php\n\nuse Anthropic\\Client;\n\n$client = new Client(\n  apiKey: getenv('ANTHROPIC_API_KEY') ?: 'my-anthropic-api-key'\n);\n\n$message = $client-\u003emessages-\u003ecreate(\n  maxTokens: 1024,\n  messages: [['role' =\u003e 'user', 'content' =\u003e 'Hello, Claude']],\n  model: 'claude-opus-4-6',\n);\n\nvar_dump($message-\u003econtent);\n```\n\n### Value Objects\n\nIt is recommended to use the static `with` constructor `Base64ImageSource::with(data: 'U3RhaW5sZXNzIHJvY2tz', ...)`\nand named parameters to initialize value objects.\n\nHowever, builders are also provided `(new Base64ImageSource)-\u003ewithData('U3RhaW5sZXNzIHJvY2tz')`.\n\n### Streaming\n\nWe provide support for streaming responses using Server-Sent Events (SSE).\n\n```php\n\u003c?php\n\nuse Anthropic\\Client;\n\n$client = new Client(\n  apiKey: getenv('ANTHROPIC_API_KEY') ?: 'my-anthropic-api-key'\n);\n\n$stream = $client-\u003emessages-\u003ecreateStream(\n  maxTokens: 1024,\n  messages: [['role' =\u003e 'user', 'content' =\u003e 'Hello, Claude']],\n  model: 'claude-sonnet-4-5-20250929',\n);\n\nforeach ($stream as $message) {\n  var_dump($message);\n}\n```\n\n### Structured Output\n\nYou can use PHP classes to define structured output schemas. The SDK will automatically convert your class to a JSON schema and parse responses back into typed objects.\n\n```php\n\u003c?php\n\nuse Anthropic\\Core\\Helpers\\StructuredOutputModel;\n\nclass Article extends StructuredOutputModel\n{\n    public string $title;\n    public string $summary;\n    /** @var string[] */\n    public array $tags;\n}\n\n$message = $client-\u003emessages-\u003ecreate(\n  maxTokens: 1024,\n  messages: [['role' =\u003e 'user', 'content' =\u003e 'Write an article about PHP']],\n  model: 'claude-sonnet-4-5-20250929',\n  outputConfig: ['format' =\u003e Article::class],\n);\n\n$article = $message-\u003econtent[0]-\u003eparsed; // Article instance\necho $article-\u003etitle;\n```\n\nSee [examples/messages_structured_output.php](examples/messages_structured_output.php) for more examples including constraints, nested models, and streaming.\n\n### Pagination\n\nList methods in the Anthropic API are paginated.\n\nThis library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:\n\n```php\n\u003c?php\n\nuse Anthropic\\Client;\n\n$client = new Client(\n  apiKey: getenv('ANTHROPIC_API_KEY') ?: 'my-anthropic-api-key'\n);\n\n$page = $client-\u003ebeta-\u003emessages-\u003ebatches-\u003elist(limit: 20);\n\nvar_dump($page);\n\n// fetch items from the current page\nforeach ($page-\u003egetItems() as $item) {\n  var_dump($item-\u003eid);\n}\n// make additional network requests to fetch items from all pages, including and after the current page\nforeach ($page-\u003epagingEachItem() as $item) {\n  var_dump($item-\u003eid);\n}\n```\n\n### Handling errors\n\nWhen 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` will be thrown:\n\n```php\n\u003c?php\n\nuse Anthropic\\Core\\Exceptions\\APIConnectionException;\nuse Anthropic\\Core\\Exceptions\\RateLimitException;\nuse Anthropic\\Core\\Exceptions\\APIStatusException;\n\ntry {\n  $message = $client-\u003emessages-\u003ecreate(\n    maxTokens: 1024,\n    messages: [['role' =\u003e 'user', 'content' =\u003e 'Hello, Claude']],\n    model: 'claude-sonnet-4-5-20250929',\n  );\n} catch (APIConnectionException $e) {\n  echo \"The server could not be reached\", PHP_EOL;\n  var_dump($e-\u003egetPrevious());\n} catch (RateLimitException $e) {\n  echo \"A 429 status code was received; we should back off a bit.\", PHP_EOL;\n} catch (APIStatusException $e) {\n  echo \"Another non-200-range status code was received\", PHP_EOL;\n  echo $e-\u003egetMessage();\n}\n```\n\nError codes are as follows:\n\n| Cause            | Error Type                     |\n| ---------------- | ------------------------------ |\n| HTTP 400         | `BadRequestException`          |\n| HTTP 401         | `AuthenticationException`      |\n| HTTP 403         | `PermissionDeniedException`    |\n| HTTP 404         | `NotFoundException`            |\n| HTTP 409         | `ConflictException`            |\n| HTTP 422         | `UnprocessableEntityException` |\n| HTTP 429         | `RateLimitException`           |\n| HTTP \u003e= 500      | `InternalServerException`      |\n| Other HTTP error | `APIStatusException`           |\n| Timeout          | `APITimeoutException`          |\n| Network error    | `APIConnectionException`       |\n\n### Retries\n\nCertain errors will be automatically retried 2 times by default, with a short exponential backoff.\n\nConnection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, \u003e=500 Internal errors, and timeouts will all be retried by default.\n\nYou can use the `maxRetries` option to configure or disable this:\n\n```php\n\u003c?php\n\nuse Anthropic\\Client;\n\n// Configure the default for all requests:\n$client = new Client(requestOptions: ['maxRetries' =\u003e 0]);\n\n// Or, configure per-request:\n$result = $client-\u003emessages-\u003ecreate(\n  maxTokens: 1024,\n  messages: [['role' =\u003e 'user', 'content' =\u003e 'Hello, Claude']],\n  model: 'claude-sonnet-4-5-20250929',\n  requestOptions: ['maxRetries' =\u003e 5],\n);\n```\n\n### File uploads\n\nRequest parameters that correspond to file uploads can be passed as a resource returned by `fopen()`, a string of file contents, or a `FileParam` instance.\n\n```php\n\u003c?php\n\nuse Anthropic\\Core\\FileParam;\n\n// Pass a string with filename and content type:\n$contents = file_get_contents('/path/to/file');\n// Pass a string with filename and content type:\n$fileMetadata = $client-\u003ebeta-\u003efiles-\u003eupload(\n  file: FileParam::fromString($contents, filename: '/path/to/file', contentType: '…'),\n);\n\n// Pass in only a string (where applicable)\n$fileMetadata = $client-\u003ebeta-\u003efiles-\u003eupload(file: '…');\n\n// Pass an open resource:\n$fd = fopen('/path/to/file', 'r');\ntry {\n  $fileMetadata = $client-\u003ebeta-\u003efiles-\u003eupload(\n    file: FileParam::fromResource($fd, filename: '/path/to/file', contentType: '…'),\n  );\n} finally {\n  fclose($fd);\n}\n```\n\n## Advanced concepts\n\n### Making custom or undocumented requests\n\n#### Undocumented properties\n\nYou can send undocumented parameters to any endpoint, and read undocumented response properties, like so:\n\nNote: the `extra*` parameters of the same name overrides the documented parameters.\n\n```php\n\u003c?php\n\n$message = $client-\u003emessages-\u003ecreate(\n  maxTokens: 1024,\n  messages: [['role' =\u003e 'user', 'content' =\u003e 'Hello, Claude']],\n  model: 'claude-sonnet-4-5-20250929',\n  requestOptions: [\n    'extraQueryParams' =\u003e ['my_query_parameter' =\u003e 'value'],\n    'extraBodyParams' =\u003e ['my_body_parameter' =\u003e 'value'],\n    'extraHeaders' =\u003e ['my-header' =\u003e 'value'],\n  ],\n);\n```\n\n#### Undocumented request params\n\nIf you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` under the `request_options:` parameter when making a request, as seen in the examples above.\n\n#### Undocumented endpoints\n\nTo make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using `client.request`, like so:\n\n```php\n\u003c?php\n\n$response = $client-\u003erequest(\n  method: \"post\",\n  path: '/undocumented/endpoint',\n  query: ['dog' =\u003e 'woof'],\n  headers: ['useful-header' =\u003e 'interesting-value'],\n  body: ['hello' =\u003e 'world']\n);\n```\n\n## Versioning\n\nThis package follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions. As the library is in initial development and has a major version of `0`, APIs may change at any time.\n\nThis package considers improvements to the (non-runtime) PHPDoc type definitions to be non-breaking changes.\n\n## Requirements\n\nPHP 8.1.0+\n\n## Contributing\n\nSee [CONTRIBUTING.md](./CONTRIBUTING.md).\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthropics%2Fanthropic-sdk-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanthropics%2Fanthropic-sdk-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthropics%2Fanthropic-sdk-php/lists"}