Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file added .gitattributes
Empty file.
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is used to automatically assign reviewers to PRs
# For more information see: https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners

* @anthropics/sdk
52 changes: 52 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CI
on:
push:
branches-ignore:
- 'generated'
- 'codegen/**'
- 'integrated/**'
- 'stl-preview-head/**'
- 'stl-preview-base/**'
pull_request:
branches-ignore:
- 'stl-preview-head/**'
- 'stl-preview-base/**'

jobs:
lint:
timeout-minutes: 10
name: lint
runs-on: ${{ github.repository == 'stainless-sdks/anthropic-php' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork

steps:
- uses: actions/checkout@v4

- name: Set up PHP
uses: 'shivammathur/setup-php@v2'
with:
php-version: '8.3'

- name: Run Bootstrap
run: ./scripts/bootstrap

- name: Run lints
run: ./scripts/lint
test:
timeout-minutes: 10
name: test
runs-on: ${{ github.repository == 'stainless-sdks/anthropic-php' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
steps:
- uses: actions/checkout@v4

- name: Set up PHP
uses: 'shivammathur/setup-php@v2'
with:
php-version: '8.3'

- name: Run bootstrap
run: ./scripts/bootstrap

- name: Run tests
run: ./scripts/test
21 changes: 21 additions & 0 deletions .github/workflows/publish-packagist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish Packagist
on:
workflow_dispatch:
push:
branches:
- main

jobs:
publish:
name: publish
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Publish to Packagist
run: |-
curl --fail-with-body -X POST -H 'Content-Type: application/json' "https://packagist.org/api/update-package?username=${PACKAGIST_USERNAME}&apiToken=${PACKAGIST_SAFE_KEY}" -d '{"repository":"https://www.github.com/anthropics/anthropic-sdk-php"}'
env:
PACKAGIST_USERNAME: ${{ secrets.ANTHROPIC_PACKAGIST_USERNAME || secrets.PACKAGIST_USERNAME }}
PACKAGIST_SAFE_KEY: ${{ secrets.ANTHROPIC_PACKAGIST_SAFE_KEY || secrets.PACKAGIST_SAFE_KEY }}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.swo
*.swp
.idea/
.php-cs-fixer.cache
.php-cs-fixer.php
.phpdoc/
.phpunit.cache
composer.lock
phpunit.xml
playground/
vendor/
17 changes: 17 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

return (new Config())
->setParallelConfig(ParallelConfigFactory::detect())
->setFinder(Finder::create()->in([__DIR__.'/src', __DIR__.'/tests']))
->setRules([
'@PhpCsFixer' => true,
'phpdoc_align' => false,
'new_with_parentheses' => ['named_class' => false],
'ordered_types' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
])
;
6 changes: 6 additions & 0 deletions .phpactor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"indexer.exclude_patterns": ["vendor"],
"language_server_completion.trim_leading_dollar": true,
"language_server_php_cs_fixer.enabled": false,
"language_server_phpstan.enabled": true
}
3 changes: 3 additions & 0 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
".": "0.1.0"
}
4 changes: 4 additions & 0 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
configured_endpoints: 23
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic%2Fanthropic-0f40955413f5d04b30ecd6c2f5002ce36a85c320cc671911ff70902fed6e342c.yml
openapi_spec_hash: 92a37231c475ddfec0093718cd24f7ab
config_hash: 21a1f78a64f751cc59c36f88095511cc
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

## 0.1.0 (2025-08-26)

Full Changelog: [v0.0.1...v0.1.0](https://github.com/anthropics/anthropic-sdk-php/compare/v0.0.1...v0.1.0)

### Features

* ensure `-&gt;toArray()` benefits from structural typing ([0758217](https://github.com/anthropics/anthropic-sdk-php/commit/0758217c9f3c5222a572a421dd53cd6c250599a8))


### Chores

* **doc:** small improvement to pagination example ([57afba6](https://github.com/anthropics/anthropic-sdk-php/commit/57afba64fd45f08491f8d42a034837715704333c))
* sync repo ([d6cb59a](https://github.com/anthropics/anthropic-sdk-php/commit/d6cb59a225f573ddd6275381cd4b7401a3c8f4cd))
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copyright 2023 Anthropic, PBC.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

244 changes: 244 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
# Anthropic PHP API library

> [!NOTE]
> The Anthropic PHP API Library is currently in **beta** and we're excited for you to experiment with it!
>
> This library has not yet been exhaustively tested in production environments and may be missing some features you'd expect in a stable release. As we continue development, there may be breaking changes that require updates to your code.
>
> **We'd love your feedback!** Please share any suggestions, bug reports, feature requests, or general thoughts by [filing an issue](https://www.github.com/anthropics/anthropic-sdk-php/issues/new).

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

## Documentation

The REST API documentation can be found on [docs.anthropic.com](https://docs.anthropic.com/claude/reference/).

## Installation

<!-- x-release-please-start-version -->

```
composer require "anthropic-ai/sdk 0.1.0"
```

<!-- x-release-please-end -->

## Usage

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

```php
<?php

use Anthropic\Client;
use Anthropic\Messages\MessageParam;

$client = new Client(
apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
);

$message = $client->messages->create(
maxTokens: 1024,
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
model: "claude-sonnet-4-20250514",
);

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

We provide support for streaming responses using Server-Sent Events (SSE).

```php
<?php

use Anthropic\Client;
use Anthropic\Messages\MessageParam;

$client = new Client(
apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
);

$stream = $client->messages->createStream(
maxTokens: 1024,
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
model: "claude-sonnet-4-20250514",
);

foreach ($stream as $message) {
var_dump($message);
}
```

### Pagination

List methods in the Anthropic API are paginated.

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

```php
<?php

use Anthropic\Client;

$client = new Client(
apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
);

$page = $client->beta->messages->batches->list();

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);
}
```

### Handling errors

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\Errors\APIError` will be thrown:

```php
<?php

use Anthropic\Core\Errors\APIConnectionError;
use Anthropic\Messages\MessageParam;

try {
$message = $client->messages->create(
maxTokens: 1024,
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
model: "claude-sonnet-4-20250514",
);
} catch (APIConnectionError $e) {
echo "The server could not be reached", PHP_EOL;
var_dump($e->getPrevious());
} catch (RateLimitError $_) {
echo "A 429 status code was received; we should back off a bit.", PHP_EOL;
} catch (APIStatusError $e) {
echo "Another non-200-range status code was received", PHP_EOL;
echo $e->getMessage();
}
```

Error codes are as follows:

| Cause | Error Type |
| ---------------- | -------------------------- |
| HTTP 400 | `BadRequestError` |
| HTTP 401 | `AuthenticationError` |
| HTTP 403 | `PermissionDeniedError` |
| HTTP 404 | `NotFoundError` |
| HTTP 409 | `ConflictError` |
| HTTP 422 | `UnprocessableEntityError` |
| HTTP 429 | `RateLimitError` |
| HTTP >= 500 | `InternalServerError` |
| Other HTTP error | `APIStatusError` |
| Timeout | `APITimeoutError` |
| Network error | `APIConnectionError` |

### Retries

Certain errors will be 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 will all be retried by default.

You can use the `max_retries` option to configure or disable this:

```php
<?php

use Anthropic\Client;
use Anthropic\RequestOptions;
use Anthropic\Messages\MessageParam;

// Configure the default for all requests:
$client = new Client(maxRetries: 0);

// Or, configure per-request:

$result = $client->messages->create(
maxTokens: 1024,
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
model: "claude-sonnet-4-20250514",
new RequestOptions(maxRetries: 5),
);
```

## Advanced concepts

### Making custom or undocumented requests

#### Undocumented properties

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

Note: the `extra_` parameters of the same name overrides the documented parameters.

```php
<?php

use Anthropic\RequestOptions;
use Anthropic\Messages\MessageParam;

$message = $client->messages->create(
maxTokens: 1024,
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
model: "claude-sonnet-4-20250514",
new RequestOptions(
extraQueryParams: ["my_query_parameter" => "value"],
extraBodyParams: ["my_body_parameter" => "value"],
extraHeaders: ["my-header" => "value"],
),
);

var_dump($message["my_undocumented_property"]);
```

#### Undocumented request params

If 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.

#### 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
<?php

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

## Versioning

This 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.

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

## Requirements

PHP 8.1.0 or higher.

## Contributing

See [the contributing documentation](https://github.com/anthropics/anthropic-sdk-php/tree/main/CONTRIBUTING.md).
Loading