The Vapi PHP library provides convenient access to the Vapi API from PHP.
This SDK requires PHP ^8.1.
composer require vapi/vapi
Instantiate and use the client with the following:
<?php
use Vapi\VapiClient;
use Vapi\Calls\Requests\CreateCallDto;
$client = new VapiClient(token: '<YOUR_TOKEN>');
$call = $client->calls->create(
new CreateCallDto([
'name' => 'My Call',
'assistantId' => 'your-assistant-id',
])
);
When the API returns a non-success status code (4xx or 5xx response), an exception will be thrown.
use Vapi\Exceptions\VapiApiException;
use Vapi\Exceptions\VapiException;
try {
$call = $client->calls->create(...);
} catch (VapiApiException $e) {
echo 'API Exception occurred: ' . $e->getMessage() . "\n";
echo 'Status Code: ' . $e->getCode() . "\n";
echo 'Response Body: ' . $e->getBody() . "\n";
}
This SDK is built to work with any HTTP client that implements Guzzle's ClientInterface
.
By default, if no client is provided, the SDK will use Guzzle's default HTTP client.
However, you can pass your own client that adheres to ClientInterface
:
use Vapi\VapiClient;
// Create a custom Guzzle client with specific configuration.
$customClient = new \GuzzleHttp\Client([
'timeout' => 5.0,
]);
// Pass the custom client when creating an instance of the class.
$client = new VapiClient(
token: '<YOUR_TOKEN>',
options: [
'client' => $customClient
]
);
// You can also utilize the same technique to leverage advanced customizations to the client such as adding middleware
$handlerStack = \GuzzleHttp\HandlerStack::create();
$handlerStack->push(MyCustomMiddleware::create());
$customClient = new \GuzzleHttp\Client(['handler' => $handlerStack]);
// Pass the custom client when creating an instance of the class.
$client = new VapiClient(
token: '<YOUR_TOKEN>',
options: [
'client' => $customClient
]
);
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
A request is deemed retryable when any of the following HTTP status codes is returned:
Use the maxRetries
request option to configure this behavior.
$call = $client->calls->create(
new CreateCallDto([...]),
options: [
'maxRetries' => 0 // Override maxRetries at the request level
]
);
Use the timeout
option to configure request timeouts.
$call = $client->calls->create(
new CreateCallDto([...]),
options: [
'timeout' => 3.0 // Override timeout to 3 seconds
]
);
While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!